update cache

This commit is contained in:
564104865
2021-11-04 20:55:01 +08:00
parent b6f6ab17f9
commit 357084e788
2 changed files with 103 additions and 290 deletions

View File

@ -1,198 +0,0 @@
package gcache
import (
"context"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/database/gredis"
"time"
)
// Redis is the gcache adapter implements using Redis server.
type Redis struct {
redis *gredis.Redis
}
// NewRedis creates and returns a new redis memory cache object.
func NewRedis(redis *gredis.Redis) Adapter {
return &Redis{
redis: redis,
}
}
// Set sets cache with `key`-`value` pair, 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 (r Redis) Set(ctx context.Context, key interface{}, value interface{}, duration time.Duration) error {
var err error
if value == nil || duration < 0 {
_, err = r.redis.Do(ctx, "DEL", key)
} else {
if duration == 0 {
_, err = r.redis.Do(ctx, "SET", key, value)
} else {
_, err = r.redis.Do(ctx, "SETEX", key, uint64(duration.Seconds()), value)
}
}
return err
}
// SetMap batch sets cache with key-value pairs by `data` map, 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 (r Redis) SetMap(ctx context.Context, data map[interface{}]interface{}, duration time.Duration) error {
panic("implement me")
}
// 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` does not exist in the
// cache, and it sets `value` successfully to the cache, or else it returns false.
//
// It does not expire if `duration` == 0.
// It deletes the `key` if `duration` < 0 or given `value` is nil.
func (r Redis) SetIfNotExist(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (ok bool, err error) {
panic("implement me")
}
// SetIfNotExistFunc sets `key` with result of function `f` and returns true
// if `key` does not exist in the cache, or else it does nothing and returns false if `key` already exists.
//
// The parameter `value` can be type of `func() interface{}`, but it does 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 (r Redis) SetIfNotExistFunc(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (ok bool, err error) {
panic("implement me")
}
// SetIfNotExistFuncLock sets `key` with result of function `f` and returns true
// if `key` does not exist in the cache, or else it does nothing and returns false if `key` already exists.
//
// It does not expire if `duration` == 0.
// It deletes the `key` if `duration` < 0 or given `value` is nil.
//
// Note that it differs from function `SetIfNotExistFunc` is that the function `f` is executed within
// writing mutex lock for concurrent safety purpose.
func (r Redis) SetIfNotExistFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (ok bool, err error) {
panic("implement me")
}
// Get retrieves and returns the associated value of given `key`.
// It returns nil if it does not exist, or its value is nil, or it's expired.
// If you would like to check if the `key` exists in the cache, it's better using function Contains.
func (r Redis) Get(ctx context.Context, key interface{}) (*gvar.Var, error) {
v, err := r.redis.Do(ctx, "GET", key)
if err != nil {
return nil, err
}
return v, nil
}
// 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 (r Redis) GetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (result *gvar.Var, err error) {
panic("implement me")
}
// 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 (r Redis) GetOrSetFunc(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (result *gvar.Var, err error) {
panic("implement me")
}
// 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 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.
//
// Note that it differs from function `GetOrSetFunc` is that the function `f` is executed within
// writing mutex lock for concurrent safety purpose.
func (r Redis) GetOrSetFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (result *gvar.Var, err error) {
panic("implement me")
}
// Contains checks and returns true if `key` exists in the cache, or else returns false.
func (r Redis) Contains(ctx context.Context, key interface{}) (bool, error) {
panic("implement me")
}
// Size returns the number of items in the cache.
func (r Redis) Size(ctx context.Context) (size int, err error) {
panic("implement me")
}
// Data returns a copy of all key-value pairs in the cache as map type.
// Note that this function may lead lots of memory usage, you can implement this function
// if necessary.
func (r Redis) Data(ctx context.Context) (data map[interface{}]interface{}, err error) {
panic("implement me")
}
// Keys returns all keys in the cache as slice.
func (r Redis) Keys(ctx context.Context) (keys []interface{}, err error) {
panic("implement me")
}
// Values returns all values in the cache as slice.
func (r Redis) Values(ctx context.Context) (values []interface{}, err error) {
panic("implement me")
}
// 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 (r Redis) Update(ctx context.Context, key interface{}, value interface{}) (oldValue *gvar.Var, exist bool, err error) {
panic("implement me")
}
// UpdateExpire updates the expiration of `key` and returns the old expiration duration value.
//
// It returns -1 and does nothing if the `key` does not exist in the cache.
// It deletes the `key` if `duration` < 0.
func (r Redis) UpdateExpire(ctx context.Context, key interface{}, duration time.Duration) (oldDuration time.Duration, err error) {
return defaultCache.UpdateExpire(ctx, key, duration)
}
// GetExpire retrieves and returns the expiration of `key` in the cache.
//
// Note that,
// It returns 0 if the `key` does not expire.
// It returns -1 if the `key` does not exist in the cache.
func (r Redis) GetExpire(ctx context.Context, key interface{}) (time.Duration, error) {
panic("implement me")
}
// Remove deletes one or more keys from cache, and returns its value.
// If multiple keys are given, it returns the value of the last deleted item.
func (r Redis) Remove(ctx context.Context, keys ...interface{}) (lastValue *gvar.Var, err error) {
panic("implement me")
}
// Clear clears all data of the cache.
// Note that this function is sensitive and should be carefully used.
func (r Redis) Clear(ctx context.Context) error {
panic("implement me")
}
// Close closes the cache if necessary.
func (r Redis) Close(ctx context.Context) error {
panic("implement me")
}

View File

@ -9,30 +9,30 @@ import (
func ExampleNew() {
//Create a cache object,
//Of course, you can also easily use the gcache package method directly
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New()
//Set cache without expiration
// Set cache without expiration
c.Set(ctx, "k1", "v1", 0)
//Get cache
// Get cache
v, _ := c.Get(ctx, "k1")
fmt.Println(v)
//Get cache size
// Get cache size
n, _ := c.Size(ctx)
fmt.Println(n)
//Does the specified key name exist in the cache
// Does the specified key name exist in the cache
b, _ := c.Contains(ctx, "k1")
fmt.Println(b)
//Delete and return the deleted key value
// Delete and return the deleted key value
fmt.Println(c.Remove(ctx, "k1"))
// Close the cache object and let the GC reclaim resources
//
c.Close(ctx)
// Output:
@ -44,14 +44,14 @@ func ExampleNew() {
func ExampleCache_Set() {
//Create a cache object,
//Of course, you can also easily use the gcache package method directly
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New()
//Set cache without expiration
// Set cache without expiration
c.Set(ctx, "k1", g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}, 0)
//Get cache
// Get cache
fmt.Println(c.Get(ctx, "k1"))
// Output:
@ -60,48 +60,48 @@ func ExampleCache_Set() {
func ExampleCache_SetAdapters() {
//Create a cache object,
//Of course, you can also easily use the gcache package method directly
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New()
//SetAdapter changes the adapter for this cache. Be very note that, this setting function is not concurrent-safe,
//which means you should not call this setting function concurrently in multiple goroutines.
adapter := gcache.NewRedis(g.Redis())
// SetAdapter changes the adapter for this cache. Be very note that, this setting function is not concurrent-safe,
// which means you should not call this setting function concurrently in multiple goroutines.
adapter := gcache.New()
c.SetAdapter(adapter)
//Set cache
// Set cache
c.Set(ctx, "k1", g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}, 0)
// Reverse makes array with elements in reverse order.
fmt.Println(c.Get(ctx, "k1"))
// May Output:
// Output:
// [1,2,3,4,5,6,7,8,9] <nil>
}
func ExampleCache_SetIfNotExist() {
//Create a cache object,
//Of course, you can also easily use the gcache package method directly
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New()
//Write when the key name does not exist, and set the expiration time to 1000 milliseconds
// Write when the key name does not exist, and set the expiration time to 1000 milliseconds
k1, err := c.SetIfNotExist(ctx, "k1", "v1", 1000*time.Millisecond)
fmt.Println(k1, err)
//Returns false when the key name already exists
// Returns false when the key name already exists
k2, err := c.SetIfNotExist(ctx, "k1", "v2", 1000*time.Millisecond)
fmt.Println(k2, err)
//Print the current list of key values
// Print the current list of key values
keys1, _ := c.Keys(ctx)
fmt.Println(keys1)
//It does not expire if `duration` == 0. It deletes the `key` if `duration` < 0 or given `value` is nil.
// It does not expire if `duration` == 0. It deletes the `key` if `duration` < 0 or given `value` is nil.
c.SetIfNotExist(ctx, "k1", 0, -10000)
// Wait 1 second for K1: V1 to expire automatically
time.Sleep(time.Second)
time.Sleep(1200 * time.Millisecond)
// Print the current key value pair again and find that K1: V1 has expired
keys2, _ := c.Keys(ctx)
@ -116,22 +116,22 @@ func ExampleCache_SetIfNotExist() {
func ExampleCache_SetMap() {
//Create a cache object,
//Of course, you can also easily use the gcache package method directly
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New()
//map[interface{}]interface{}
// map[interface{}]interface{}
data := g.MapAnyAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
}
//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.
// 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.
c.SetMap(ctx, data, 1000*time.Millisecond)
//Gets the specified key value
// Gets the specified key value
v1, _ := c.Get(ctx, "k1")
v2, _ := c.Get(ctx, "k2")
v3, _ := c.Get(ctx, "k3")
@ -144,8 +144,8 @@ func ExampleCache_SetMap() {
func ExampleCache_Size() {
//Create a cache object,
//Of course, you can also easily use the gcache package method directly
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New()
// Add 10 elements without expiration
@ -153,7 +153,7 @@ func ExampleCache_Size() {
c.Set(ctx, i, i, 0)
}
//Size returns the number of items in the cache.
// Size returns the number of items in the cache.
n, _ := c.Size(ctx)
fmt.Println(n)
@ -163,15 +163,15 @@ func ExampleCache_Size() {
func ExampleCache_Update() {
//Create a cache object,
//Of course, you can also easily use the gcache package method directly
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New()
//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.
// 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.
c.SetMap(ctx, g.MapAnyAny{"k1": "v1", "k2": "v2", "k3": "v3"}, 0)
//Print the current key value pair
// Print the current key value pair
k1, _ := c.Get(ctx, "k1")
fmt.Println(k1)
k2, _ := c.Get(ctx, "k2")
@ -179,12 +179,12 @@ func ExampleCache_Update() {
k3, _ := c.Get(ctx, "k3")
fmt.Println(k3)
//Update updates the value of `key` without changing its expiration and returns the old value.
// Update updates the value of `key` without changing its expiration and returns the old value.
re, exist, _ := c.Update(ctx, "k1", "v11")
fmt.Println(re, exist)
//The returned value `exist` is false if the `key` does not exist in the cache.
//It does nothing if `key` does not exist in the cache.
// The returned value `exist` is false if the `key` does not exist in the cache.
// It does nothing if `key` does not exist in the cache.
re1, exist1, _ := c.Update(ctx, "k4", "v44")
fmt.Println(re1, exist1)
@ -194,6 +194,7 @@ func ExampleCache_Update() {
fmt.Println(kup2)
kup3, _ := c.Get(ctx, "k3")
fmt.Println(kup3)
// Output:
// v1
// v2
@ -207,20 +208,21 @@ func ExampleCache_Update() {
func ExampleCache_UpdateExpire() {
//Create a cache object,
//Of course, you can also easily use the gcache package method directly
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New()
c.Set(ctx, "k1", "v1", 1000*time.Millisecond)
expire, _ := c.GetExpire(ctx, "k1")
fmt.Println(expire)
//UpdateExpire updates the expiration of `key` and returns the old expiration duration value.
//It returns -1 and does nothing if the `key` does not exist in the cache.
// UpdateExpire updates the expiration of `key` and returns the old expiration duration value.
// It returns -1 and does nothing if the `key` does not exist in the cache.
c.UpdateExpire(ctx, "k1", 500*time.Millisecond)
expire1, _ := c.GetExpire(ctx, "k1")
fmt.Println(expire1)
// Output:
// 1s
// 500ms
@ -228,30 +230,30 @@ func ExampleCache_UpdateExpire() {
func ExampleCache_Values() {
//Create a cache object,
//Of course, you can also easily use the gcache package method directly
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New()
//Write value
// Write value
c.Set(ctx, "k1", g.Map{"k1": "v1", "k2": "v2"}, 0)
//c.Set(ctx, "k2", "Here is Value2", 0)
//c.Set(ctx, "k3", 111, 0)
// c.Set(ctx, "k2", "Here is Value2", 0)
// c.Set(ctx, "k3", 111, 0)
//Values returns all values in the cache as slice.
// Values returns all values in the cache as slice.
data, _ := c.Values(ctx)
fmt.Println(data)
// may Output:
// May Output:
// [map[k1:v1 k2:v2]]
}
func ExampleCache_Close() {
//Create a cache object,
//Of course, you can also easily use the gcache package method directly
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New()
//Set Cache
// Set Cache
c.Set(ctx, "k1", "v", 0)
data, _ := c.Get(ctx, "k1")
fmt.Println(data)
@ -271,14 +273,14 @@ func ExampleCache_Close() {
func ExampleCache_Contains() {
//Create a cache object,
//Of course, you can also easily use the gcache package method directly
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New()
//Set Cache
// Set Cache
c.Set(ctx, "k", "v", 0)
//Contains returns true if `key` exists in the cache, or else returns false.
// Contains returns true if `key` exists in the cache, or else returns false.
// return true
data, _ := c.Contains(ctx, "k")
fmt.Println(data)
@ -295,14 +297,15 @@ func ExampleCache_Contains() {
func ExampleCache_Data() {
//Create a cache object,
//Of course, you can also easily use the gcache package method directly
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New()
c.SetMap(ctx, g.MapAnyAny{"k1": "v1", "k2": "v2"}, 0)
c.Set(ctx, "k5", "v5", 0)
//Data returns a copy of all key-value pairs in the cache as map type. Note that this function may lead lots of memory usage, you can implement this function if necessary.
// Get retrieves and returns the associated value of given `key`.
// It returns nil if it does not exist, its value is nil or it's expired.
data, _ := c.Get(ctx, "k1")
fmt.Println(data)
@ -320,14 +323,15 @@ func ExampleCache_Data() {
func ExampleCache_Get() {
//Create a cache object,
//Of course, you can also easily use the gcache package method directly
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New()
//Set Cache Object
// Set Cache Object
c.Set(ctx, "k1", "v1", 0)
//Get retrieves and returns the associated value of given `key`. It returns nil if it does not exist, its value is nil or it's expired.
// Get retrieves and returns the associated value of given `key`.
// It returns nil if it does not exist, its value is nil or it's expired.
data, _ := c.Get(ctx, "k1")
fmt.Println(data)
@ -337,17 +341,17 @@ func ExampleCache_Get() {
func ExampleCache_GetExpire() {
//Create a cache object,
//Of course, you can also easily use the gcache package method directly
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New()
// Set cache without expiration
c.Set(ctx, "k", "v", 10000*time.Millisecond)
//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.
data, _ := c.GetExpire(ctx, "k")
fmt.Println(data)
// 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.
expire, _ := c.GetExpire(ctx, "k")
fmt.Println(expire)
// Output:
// 10s
@ -355,11 +359,12 @@ func ExampleCache_GetExpire() {
func ExampleCache_GetOrSet() {
//Create a cache object,
//Of course, you can also easily use the gcache package method directly
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New()
//GetOrSet retrieves and returns the value of `key`, or sets `key`-`value` pair and returns `value` if `key` does not exist in the cache.
// GetOrSet retrieves and returns the value of `key`, or sets `key`-`value` pair and returns `value`
// if `key` does not exist in the cache.
data, _ := c.GetOrSet(ctx, "k", "v", 10000*time.Millisecond)
fmt.Println(data)
@ -374,18 +379,18 @@ func ExampleCache_GetOrSet() {
func ExampleCache_GetOrSetFunc() {
//Create a cache object,
//Of course, you can also easily use the gcache package method directly
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New()
//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.
// 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.
c.GetOrSetFunc(ctx, 1, func() (interface{}, error) {
return 111, nil
}, 10000*time.Millisecond)
v, _ := c.Get(ctx, 1)
fmt.Println(v)
//but it does nothing if `value` is a function and the function result is nil.
c.GetOrSetFunc(ctx, 2, func() (interface{}, error) {
return nil, nil
}, 10000*time.Millisecond)
@ -398,8 +403,9 @@ func ExampleCache_GetOrSetFunc() {
}
func ExampleCache_GetOrSetFuncLock() {
//Create a cache object,
//Of course, you can also easily use the gcache package method directly
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New()
// Modify locking Note that the function `f` should be executed within writing mutex lock for concurrent safety purpose.
@ -440,13 +446,14 @@ func ExampleCache_GetOrSetFuncLock() {
}
func ExampleCache_Keys() {
//Create a cache object,
//Of course, you can also easily use the gcache package method directly
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New()
c.SetMap(ctx, g.MapAnyAny{"k1": "v1", "k2": "v2"}, 0)
//Print the current list of key values
// Print the current list of key values
keys1, _ := c.Keys(ctx)
fmt.Println(keys1)
@ -456,13 +463,15 @@ func ExampleCache_Keys() {
}
func ExampleCache_Remove() {
//Create a cache object,
//Of course, you can also easily use the gcache package method directly
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New()
c.SetMap(ctx, g.MapAnyAny{"k1": "v1", "k2": "v2"}, 0)
//Remove deletes one or more keys from cache, and returns its value. If multiple keys are given, it returns the value of the last deleted item.
// Remove deletes one or more keys from cache, and returns its value.
// If multiple keys are given, it returns the value of the last deleted item.
c.Remove(ctx, "k1")
data, _ := c.Data(ctx)
@ -473,13 +482,15 @@ func ExampleCache_Remove() {
}
func ExampleCache_Removes() {
//Create a cache object,
//Of course, you can also easily use the gcache package method directly
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New()
c.SetMap(ctx, g.MapAnyAny{"k1": "v1", "k2": "v2", "k3": "v3", "k4": "v4"}, 0)
//Remove deletes one or more keys from cache, and returns its value. If multiple keys are given, it returns the value of the last deleted item.
// Remove deletes one or more keys from cache, and returns its value.
// If multiple keys are given, it returns the value of the last deleted item.
c.Removes(ctx, g.Slice{"k1", "k2", "k3"})
data, _ := c.Data(ctx)