adapter

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 30, 2021 License: MIT Imports: 5 Imported by: 0

README

Language License

Introduction

A cache adapter based on GoFrame master-slave redis client

Keywords

cache adapter redis GoFrame

Quick Start

package main

import (
	"github.com/hetiansu5/msredis"
	"github.com/hetiansu5/adapter"
)

masterName := "cache" // master redis config name
slaveName := "cache_slave" // slave redis config name, can be more
group := msredis.Group(masterName, slaveName)
cache := adapter.NewRedis(msredis.group)
cache.Get(ctx, key)

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRedis

func NewRedis(redisGroup *msredis.RedisGroup) gcache.Adapter

newAdapterMemory creates and returns a new memory cache object.

Types

type Redis

type Redis struct {
	// contains filtered or unexported fields
}

Redis is the gcache adapter implements using Redis server.

func (*Redis) Clear

func (c *Redis) Clear(ctx context.Context) error

Clear clears all data of the cache. Note that this function is sensitive and should be carefully used.

func (*Redis) Close

func (c *Redis) Close(ctx context.Context) error

Close closes the cache.

func (*Redis) Contains

func (c *Redis) Contains(ctx context.Context, key interface{}) (bool, error)

Contains returns true if <key> exists in the cache, or else returns false.

func (*Redis) Data

func (c *Redis) Data(ctx context.Context) (map[interface{}]interface{}, error)

Data returns a copy of all key-value pairs in the cache as map type.

func (*Redis) Get

func (c *Redis) Get(ctx context.Context, key interface{}) (interface{}, error)

Get retrieves and returns the associated value of given <key>. It returns nil if it does not exist or its value is nil.

func (*Redis) GetExpire

func (c *Redis) GetExpire(ctx context.Context, key interface{}) (time.Duration, error)

GetExpire retrieves and returns the expiration of <key> in the cache.

It returns 0 if the <key> does not expire. It returns -1 if the <key> does not exist in the cache.

func (*Redis) GetOrSet

func (c *Redis) GetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (interface{}, error)

GetOrSet retrieves and returns the value of <key>, or sets <key>-<value> pair and returns <value> if <key> does not exist in the cache. The key-value pair expires after <duration>.

It does not expire if <duration> == 0. It deletes the <key> if <duration> < 0 or given <value> is nil, but it does nothing if <value> is a function and the function result is nil.

func (*Redis) GetOrSetFunc

func (c *Redis) GetOrSetFunc(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error)

GetOrSetFunc retrieves and returns the value of <key>, or sets <key> with result of function <f> and returns its result if <key> does not exist in the cache. The key-value pair expires after <duration>.

It does not expire if <duration> == 0. It deletes the <key> if <duration> < 0 or given <value> is nil, but it does nothing if <value> is a function and the function result is nil.

func (*Redis) GetOrSetFuncLock

func (c *Redis) GetOrSetFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error)

GetOrSetFuncLock retrieves and returns the value of <key>, or sets <key> with result of function <f> and returns its result if <key> does not exist in the cache. The key-value pair expires after <duration>.

It does not expire if <duration> == 0. It does nothing if function <f> returns nil.

Note that the function <f> should be executed within writing mutex lock for concurrent safety purpose.

func (*Redis) Keys

func (c *Redis) Keys(ctx context.Context) ([]interface{}, error)

Keys returns all keys in the cache as slice.

func (*Redis) Remove

func (c *Redis) Remove(ctx context.Context, keys ...interface{}) (value interface{}, err error)

Remove deletes the one or more keys from cache, and returns its value. If multiple keys are given, it returns the value of the deleted last item.

func (*Redis) Set

func (c *Redis) Set(ctx context.Context, key interface{}, value interface{}, duration time.Duration) error

Set sets cache with <key>-<value> pair, which is expired after <duration>. It does not expire if <duration> == 0. It deletes the <key> if <duration> < 0 or given <value> is nil.

func (*Redis) SetIfNotExist

func (c *Redis) SetIfNotExist(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (bool, error)

SetIfNotExist sets cache with <key>-<value> pair which is expired after <duration> if <key> does not exist in the cache. It returns true the <key> dose not exist in the cache and it sets <value> successfully to the cache, or else it returns false.

The parameter <value> can be type of <func() interface{}>, but it dose nothing if its result is nil.

It does not expire if <duration> == 0. It deletes the <key> if <duration> < 0 or given <value> is nil.

func (*Redis) Sets

func (c *Redis) Sets(ctx context.Context, data map[interface{}]interface{}, duration time.Duration) error

Sets batch sets cache with key-value pairs by <data>, which is expired after <duration>.

It does not expire if <duration> == 0. It deletes the keys of <data> if <duration> < 0 or given <value> is nil.

func (*Redis) Size

func (c *Redis) Size(ctx context.Context) (size int, err error)

Size returns the size of the cache.

func (*Redis) Update

func (c *Redis) Update(ctx context.Context, key interface{}, value interface{}) (oldValue interface{}, exist bool, err error)

Update updates the value of <key> without changing its expiration and returns the old value. The returned value <exist> is false if the <key> does not exist in the cache.

It deletes the <key> if given <value> is nil. It does nothing if <key> does not exist in the cache.

func (*Redis) UpdateExpire

func (c *Redis) UpdateExpire(ctx context.Context, key interface{}, duration time.Duration) (oldDuration time.Duration, err error)

UpdateExpire updates the expiration of <key> and returns the old expiration duration value.

It returns -1 if the <key> does not exist in the cache. It deletes the <key> if <duration> < 0.

func (*Redis) Values

func (c *Redis) Values(ctx context.Context) ([]interface{}, error)

Values returns all values in the cache as slice.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL