From 7de69db70720cfe96d195442f8f21113e14b5e3f Mon Sep 17 00:00:00 2001 From: 564104865 <48121048+564104865@users.noreply.github.com> Date: Wed, 3 Nov 2021 10:16:41 +0800 Subject: [PATCH 1/9] 232234 --- os/gcache/gcache_z_example_adapter_redis.go | 118 +++++ os/gcache/gcache_z_example_cache_test.go | 475 ++++++++++++++++++++ 2 files changed, 593 insertions(+) create mode 100644 os/gcache/gcache_z_example_adapter_redis.go create mode 100644 os/gcache/gcache_z_example_cache_test.go diff --git a/os/gcache/gcache_z_example_adapter_redis.go b/os/gcache/gcache_z_example_adapter_redis.go new file mode 100644 index 000000000..6f6adfe65 --- /dev/null +++ b/os/gcache/gcache_z_example_adapter_redis.go @@ -0,0 +1,118 @@ +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 +} + +// newAdapterMemory creates and returns a new memory cache object. +func NewRedis(redis *gredis.Redis) Adapter { + return &Redis{ + redis: redis, + } +} + +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 +} + +func (r Redis) SetMap(ctx context.Context, data map[interface{}]interface{}, duration time.Duration) error { + panic("implement me") +} + +func (r Redis) SetIfNotExist(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (ok bool, err error) { + panic("implement me") +} + +func (r Redis) SetIfNotExistFunc(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (ok bool, err error) { + panic("implement me") +} + +func (r Redis) SetIfNotExistFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (ok bool, err error) { + panic("implement me") +} + +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 +} + +func (r Redis) GetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (result *gvar.Var, err error) { + panic("implement me") +} + +func (r Redis) GetOrSetFunc(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (result *gvar.Var, err error) { + panic("implement me") +} + +func (r Redis) GetOrSetFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (result *gvar.Var, err error) { + panic("implement me") +} + +func (r Redis) Contains(ctx context.Context, key interface{}) (bool, error) { + panic("implement me") +} + +func (r Redis) Size(ctx context.Context) (size int, err error) { + panic("implement me") +} + +func (r Redis) Data(ctx context.Context) (data map[interface{}]interface{}, err error) { + panic("implement me") +} + +func (r Redis) Keys(ctx context.Context) (keys []interface{}, err error) { + panic("implement me") +} + +func (r Redis) Values(ctx context.Context) (values []interface{}, err error) { + panic("implement me") +} + +func (r Redis) Update(ctx context.Context, key interface{}, value interface{}) (oldValue *gvar.Var, exist bool, err error) { + panic("implement me") +} + +func (r Redis) UpdateExpire(ctx context.Context, key interface{}, duration time.Duration) (oldDuration time.Duration, err error) { + panic("implement me") +} + +func (r Redis) GetExpire(ctx context.Context, key interface{}) (time.Duration, error) { + panic("implement me") +} + +func (r Redis) Remove(ctx context.Context, keys ...interface{}) (lastValue *gvar.Var, err error) { + panic("implement me") +} + +func (r Redis) Clear(ctx context.Context) error { + panic("implement me") +} + +func (r Redis) Close(ctx context.Context) error { + panic("implement me") +} + + + diff --git a/os/gcache/gcache_z_example_cache_test.go b/os/gcache/gcache_z_example_cache_test.go new file mode 100644 index 000000000..f9b59db34 --- /dev/null +++ b/os/gcache/gcache_z_example_cache_test.go @@ -0,0 +1,475 @@ +package gcache_test + +import ( + "fmt" + "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/os/gcache" + "time" +) + + +func ExampleNew() { + + //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, "k1", "v1", 0) + + //Get cache + v, _ := c.Get(ctx, "k1") + fmt.Println(v) + + //Get cache size + n, _ := c.Size(ctx) + fmt.Println(n) + + //Does the specified key name exist in the cache + b, _ := c.Contains(ctx, "k1") + fmt.Println(b) + + //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: + // v1 + // 1 + // true + // v1 +} + +func ExampleCache_Set() { + + //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, "k1", g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}, 0) + + //Get cache + fmt.Println(c.Get(ctx, "k1")) + + // Output: + // [1,2,3,4,5,6,7,8,9] +} + +func ExampleCache_SetAdapters() { + + //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()) + c.SetAdapter(adapter) + + //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: + // [1,2,3,4,5,6,7,8,9] +} + +func ExampleCache_SetIfNotExist() { + + //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 + k1, err := c.SetIfNotExist(ctx, "k1", "v1", 1000*time.Millisecond) + fmt.Println(k1, err) + + //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 + 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. + c.SetIfNotExist(ctx, "k1", 0, -10000) + + // Wait 1 second for K1: V1 to expire automatically + time.Sleep(time.Second) + + // Print the current key value pair again and find that K1: V1 has expired + keys2, _ := c.Keys(ctx) + fmt.Println(keys2) + + // Output: + // true + // false + // [k1] + // [] +} + +func ExampleCache_Sets() { + + //Create a cache object, + //Of course, you can also easily use the gcache package method directly + c := gcache.New() + + //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. + c.SetMap(ctx, data, 1000*time.Millisecond) + + //Print the current key value pair + data2, _ := c.Data(ctx) + + //Gets the specified key value + v1, _ := c.Get(ctx, "k1") + + fmt.Println(data2, v1) + + + // Output: + // map[k1:v1 k2:v2 k3:v3] v1 +} + +func ExampleCache_Size() { + + //Create a cache object, + //Of course, you can also easily use the gcache package method directly + c := gcache.New() + + // Add 10 elements without expiration + for i := 0; i < 10; i++ { + c.Set(ctx,i, i, 0) + } + + //Size returns the number of items in the cache. + n, _ := c.Size(ctx) + fmt.Println(n) + + // Output: + // 10 +} + +func ExampleCache_Update(){ + + //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. + c.SetMap(ctx, g.MapAnyAny{"k1": "v1","k2":"v2","k3":"v3"}, 0) + + //Print the current key value pair + data, _ := c.Data(ctx) + fmt.Println(data) + + //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. + re1,exist1,_ := c.Update(ctx,"k4","v44") + fmt.Println(re1,exist1) + + + data1, _ := c.Data(ctx) + fmt.Println(data1) + // Output: + // map[k1:v1 k2:v2 k3:v3] + // v1 true + // false + // map[k1:v11 k2:v2 k3:v3] +} + +func ExampleCache_UpdateExpire(){ + + //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) + k1_expire,_ := c.GetExpire(ctx,"k1") + fmt.Println(k1_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. + c.UpdateExpire(ctx,"k1",500*time.Millisecond) + + k1_expires,_ := c.GetExpire(ctx,"k1") + fmt.Println(k1_expires) + // Output: + // 1s + // 500ms +} + +func ExampleCache_Values(){ + + //Create a cache object, + //Of course, you can also easily use the gcache package method directly + c := gcache.New() + + //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) + + //Values returns all values in the cache as slice. + data,_ :=c.Values(ctx) + fmt.Println(data) + + // Output: + // [map[k1:v1 k2:v2] Here is Value2 111] +} + +func ExampleCache_Close(){ + + //Create a cache object, + //Of course, you can also easily use the gcache package method directly + c := gcache.New() + + //Set Cache + c.Set(ctx,"k1","v",0) + data,_ := c.Get(ctx,"k1") + fmt.Println(data) + + // Close closes the cache if necessary. + c.Close(ctx) + + data1,_ := c.Get(ctx,"k1") + + fmt.Println(data1) + + // Output: + // v + // v + +} + +func ExampleCache_Contains() { + + //Create a cache object, + //Of course, you can also easily use the gcache package method directly + c := gcache.New() + + //Set Cache + c.Set(ctx,"k","v",0) + + //Contains returns true if `key` exists in the cache, or else returns false. + // return true + data,_ := c.Contains(ctx,"k") + fmt.Println(data) + + // return false + data1,_ := c.Contains(ctx,"k1") + fmt.Println(data1) + + // Output: + // true + // false + +} + +func ExampleCache_Data() { + + //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. + data,_ := c.Data(ctx) + fmt.Println(data) + + // Output: + // map[k1:v1 k2:v2 k5:v5] +} + +func ExampleCache_Get(){ + + //Create a cache object, + //Of course, you can also easily use the gcache package method directly + c := gcache.New() + + //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. + data,_ := c.Get(ctx,"k1") + fmt.Println(data) + + // Output: + // v1 +} + +func ExampleCache_GetExpire(){ + + //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) + + // Output: + // 10s +} + +func ExampleCache_GetOrSet(){ + + //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. + data,_ := c.GetOrSet(ctx,"k","v",10000*time.Millisecond) + fmt.Println(data) + + data1,_ := c.Get(ctx,"k") + fmt.Println(data1) + + // Output: + // v + // v + +} + +func ExampleCache_GetOrSetFunc(){ + + //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. + 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) + v1, _ := c.Get(ctx, 2) + fmt.Println(v1) + + // Output: + // 111 + // +} + +func ExampleCache_GetOrSetFuncLock(){ + //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. + c.GetOrSetFuncLock(ctx, 1, func() (interface{}, error) { + return 11, nil + }, 0) + v, _ := c.Get(ctx, 1) + fmt.Println(v) + + // Modification failed + c.GetOrSetFuncLock(ctx, 1, func() (interface{}, error) { + return 111, nil + }, 0) + v, _ = c.Get(ctx, 1) + fmt.Println(v) + + c.Remove(ctx, g.Slice{1, 2, 3}...) + + // Modify locking + c.GetOrSetFuncLock(ctx, 1, func() (interface{}, error) { + return 111, nil + }, 0) + v, _ = c.Get(ctx, 1) + fmt.Println(v) + + // Modification failed + c.GetOrSetFuncLock(ctx, 1, func() (interface{}, error) { + return 11, nil + }, 0) + v, _ = c.Get(ctx, 1) + fmt.Println(v) + + // Output: + // 11 + // 11 + // 111 + // 111 +} + +func ExampleCache_Keys(){ + //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 + keys1, _ := c.Keys(ctx) + fmt.Println(keys1) + + // Output: + // [k1 k2] + +} + +func ExampleCache_Remove() { + //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. + c.Remove(ctx,"k1") + + data, _ := c.Data(ctx) + fmt.Println(data) + + // Output: + // map[k2:v2 k3:v3 k4:v4] +} + +func ExampleCache_Removes(){ + //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. + c.Removes(ctx, g.Slice{"k1", "k3"}) + + data, _ := c.Data(ctx) + fmt.Println(data) + + // Output: + // map[k2:v2 k4:v4] +} + From a423fba2a83fc750cb7ac806fce0ea74edf99489 Mon Sep 17 00:00:00 2001 From: 564104865 <564104865@qq.com> Date: Wed, 3 Nov 2021 14:59:28 +0800 Subject: [PATCH 2/9] comment or be unexported exported method Redis.UpdateExpire should have comment or be unexported --- os/gcache/gcache_z_example_adapter_redis.go | 91 ++++++++++++++++++++- os/gcache/gcache_z_example_cache_test.go | 8 +- 2 files changed, 93 insertions(+), 6 deletions(-) diff --git a/os/gcache/gcache_z_example_adapter_redis.go b/os/gcache/gcache_z_example_adapter_redis.go index 6f6adfe65..6f4e324cd 100644 --- a/os/gcache/gcache_z_example_adapter_redis.go +++ b/os/gcache/gcache_z_example_adapter_redis.go @@ -19,6 +19,11 @@ func NewRedis(redis *gredis.Redis) Adapter { } } + +// 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 { @@ -33,22 +38,53 @@ func (r Redis) Set(ctx context.Context, key interface{}, value interface{}, dura 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 { @@ -58,58 +94,109 @@ func (r Redis) Get(ctx context.Context, key interface{}) (*gvar.Var, error) { 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") } -func (r Redis) UpdateExpire(ctx context.Context, key interface{}, duration time.Duration) (oldDuration time.Duration, 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") } diff --git a/os/gcache/gcache_z_example_cache_test.go b/os/gcache/gcache_z_example_cache_test.go index f9b59db34..d99e1a919 100644 --- a/os/gcache/gcache_z_example_cache_test.go +++ b/os/gcache/gcache_z_example_cache_test.go @@ -204,15 +204,15 @@ func ExampleCache_UpdateExpire(){ c := gcache.New() c.Set(ctx,"k1","v1",1000*time.Millisecond) - k1_expire,_ := c.GetExpire(ctx,"k1") - fmt.Println(k1_expire) + 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. c.UpdateExpire(ctx,"k1",500*time.Millisecond) - k1_expires,_ := c.GetExpire(ctx,"k1") - fmt.Println(k1_expires) + expire1,_ := c.GetExpire(ctx,"k1") + fmt.Println(expire1) // Output: // 1s // 500ms From 7971177c58678c23c57b330cd4aee9bf5d9860ec Mon Sep 17 00:00:00 2001 From: 564104865 <564104865@qq.com> Date: Wed, 3 Nov 2021 15:16:07 +0800 Subject: [PATCH 3/9] newredis --- os/gcache/gcache_z_example_adapter_redis.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/os/gcache/gcache_z_example_adapter_redis.go b/os/gcache/gcache_z_example_adapter_redis.go index 6f4e324cd..e7ab20889 100644 --- a/os/gcache/gcache_z_example_adapter_redis.go +++ b/os/gcache/gcache_z_example_adapter_redis.go @@ -12,7 +12,7 @@ type Redis struct { redis *gredis.Redis } -// newAdapterMemory creates and returns a new memory cache object. +// NewRedis creates and returns a new redis memory cache object. func NewRedis(redis *gredis.Redis) Adapter { return &Redis{ redis: redis, From 8f090739d9007e50d7b204a426bba409ce04d774 Mon Sep 17 00:00:00 2001 From: 564104865 <564104865@qq.com> Date: Wed, 3 Nov 2021 15:23:51 +0800 Subject: [PATCH 4/9] go fmt --- os/gcache/gcache_z_example_adapter_redis.go | 17 ++-- os/gcache/gcache_z_example_cache_test.go | 96 ++++++++++----------- 2 files changed, 51 insertions(+), 62 deletions(-) diff --git a/os/gcache/gcache_z_example_adapter_redis.go b/os/gcache/gcache_z_example_adapter_redis.go index e7ab20889..7bbde5e3e 100644 --- a/os/gcache/gcache_z_example_adapter_redis.go +++ b/os/gcache/gcache_z_example_adapter_redis.go @@ -19,7 +19,6 @@ func NewRedis(redis *gredis.Redis) Adapter { } } - // Set sets cache with `key`-`value` pair, which is expired after `duration`. // // It does not expire if `duration` == 0. @@ -27,12 +26,12 @@ func NewRedis(redis *gredis.Redis) Adapter { 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) + _, err = r.redis.Do(ctx, "DEL", key) } else { if duration == 0 { - _, err = r.redis.Do(ctx,"SET", key, value) + _, err = r.redis.Do(ctx, "SET", key, value) } else { - _, err = r.redis.Do(ctx,"SETEX", key, uint64(duration.Seconds()), value) + _, err = r.redis.Do(ctx, "SETEX", key, uint64(duration.Seconds()), value) } } return err @@ -46,7 +45,6 @@ func (r Redis) SetMap(ctx context.Context, data map[interface{}]interface{}, dur 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. @@ -57,7 +55,6 @@ func (r Redis) SetIfNotExist(ctx context.Context, key interface{}, value interfa 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. // @@ -86,7 +83,7 @@ func (r Redis) SetIfNotExistFuncLock(ctx context.Context, key interface{}, f fun // 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) + v, err := r.redis.Do(ctx, "GET", key) if err != nil { return nil, err } @@ -166,12 +163,11 @@ func (r Redis) Update(ctx context.Context, key interface{}, value interface{}) ( 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) { +func (r Redis) UpdateExpire(ctx context.Context, key interface{}, duration time.Duration) (oldDuration time.Duration, err error) { return defaultCache.UpdateExpire(ctx, key, duration) } @@ -200,6 +196,3 @@ func (r Redis) Clear(ctx context.Context) error { func (r Redis) Close(ctx context.Context) error { panic("implement me") } - - - diff --git a/os/gcache/gcache_z_example_cache_test.go b/os/gcache/gcache_z_example_cache_test.go index d99e1a919..3fe724d75 100644 --- a/os/gcache/gcache_z_example_cache_test.go +++ b/os/gcache/gcache_z_example_cache_test.go @@ -7,7 +7,6 @@ import ( "time" ) - func ExampleNew() { //Create a cache object, @@ -140,7 +139,6 @@ func ExampleCache_Sets() { fmt.Println(data2, v1) - // Output: // map[k1:v1 k2:v2 k3:v3] v1 } @@ -153,7 +151,7 @@ func ExampleCache_Size() { // Add 10 elements without expiration for i := 0; i < 10; i++ { - c.Set(ctx,i, i, 0) + c.Set(ctx, i, i, 0) } //Size returns the number of items in the cache. @@ -164,7 +162,7 @@ func ExampleCache_Size() { // 10 } -func ExampleCache_Update(){ +func ExampleCache_Update() { //Create a cache object, //Of course, you can also easily use the gcache package method directly @@ -172,21 +170,20 @@ func ExampleCache_Update(){ //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) + c.SetMap(ctx, g.MapAnyAny{"k1": "v1", "k2": "v2", "k3": "v3"}, 0) //Print the current key value pair data, _ := c.Data(ctx) fmt.Println(data) //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) + 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. - re1,exist1,_ := c.Update(ctx,"k4","v44") - fmt.Println(re1,exist1) - + re1, exist1, _ := c.Update(ctx, "k4", "v44") + fmt.Println(re1, exist1) data1, _ := c.Data(ctx) fmt.Println(data1) @@ -197,61 +194,61 @@ func ExampleCache_Update(){ // map[k1:v11 k2:v2 k3:v3] } -func ExampleCache_UpdateExpire(){ +func ExampleCache_UpdateExpire() { //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") + 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. - c.UpdateExpire(ctx,"k1",500*time.Millisecond) + c.UpdateExpire(ctx, "k1", 500*time.Millisecond) - expire1,_ := c.GetExpire(ctx,"k1") + expire1, _ := c.GetExpire(ctx, "k1") fmt.Println(expire1) // Output: // 1s // 500ms } -func ExampleCache_Values(){ +func ExampleCache_Values() { //Create a cache object, //Of course, you can also easily use the gcache package method directly c := gcache.New() //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, "k1", g.Map{"k1": "v1", "k2": "v2"}, 0) + c.Set(ctx, "k2", "Here is Value2", 0) + c.Set(ctx, "k3", 111, 0) //Values returns all values in the cache as slice. - data,_ :=c.Values(ctx) + data, _ := c.Values(ctx) fmt.Println(data) // Output: // [map[k1:v1 k2:v2] Here is Value2 111] } -func ExampleCache_Close(){ +func ExampleCache_Close() { //Create a cache object, //Of course, you can also easily use the gcache package method directly c := gcache.New() //Set Cache - c.Set(ctx,"k1","v",0) - data,_ := c.Get(ctx,"k1") + c.Set(ctx, "k1", "v", 0) + data, _ := c.Get(ctx, "k1") fmt.Println(data) // Close closes the cache if necessary. c.Close(ctx) - data1,_ := c.Get(ctx,"k1") + data1, _ := c.Get(ctx, "k1") fmt.Println(data1) @@ -268,15 +265,15 @@ func ExampleCache_Contains() { c := gcache.New() //Set Cache - c.Set(ctx,"k","v",0) + c.Set(ctx, "k", "v", 0) //Contains returns true if `key` exists in the cache, or else returns false. // return true - data,_ := c.Contains(ctx,"k") + data, _ := c.Contains(ctx, "k") fmt.Println(data) // return false - data1,_ := c.Contains(ctx,"k1") + data1, _ := c.Contains(ctx, "k1") fmt.Println(data1) // Output: @@ -291,63 +288,63 @@ func ExampleCache_Data() { //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) + 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. - data,_ := c.Data(ctx) + data, _ := c.Data(ctx) fmt.Println(data) // Output: // map[k1:v1 k2:v2 k5:v5] } -func ExampleCache_Get(){ +func ExampleCache_Get() { //Create a cache object, //Of course, you can also easily use the gcache package method directly c := gcache.New() //Set Cache Object - c.Set(ctx,"k1","v1",0) + 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. - data,_ := c.Get(ctx,"k1") + data, _ := c.Get(ctx, "k1") fmt.Println(data) // Output: // v1 } -func ExampleCache_GetExpire(){ +func ExampleCache_GetExpire() { //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) + 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") + data, _ := c.GetExpire(ctx, "k") fmt.Println(data) // Output: // 10s } -func ExampleCache_GetOrSet(){ +func ExampleCache_GetOrSet() { //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. - data,_ := c.GetOrSet(ctx,"k","v",10000*time.Millisecond) + data, _ := c.GetOrSet(ctx, "k", "v", 10000*time.Millisecond) fmt.Println(data) - data1,_ := c.Get(ctx,"k") + data1, _ := c.Get(ctx, "k") fmt.Println(data1) // Output: @@ -356,21 +353,21 @@ func ExampleCache_GetOrSet(){ } -func ExampleCache_GetOrSetFunc(){ +func ExampleCache_GetOrSetFunc() { //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. - c.GetOrSetFunc(ctx,1,func() (interface{}, error) { + 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) { + c.GetOrSetFunc(ctx, 2, func() (interface{}, error) { return nil, nil }, 10000*time.Millisecond) v1, _ := c.Get(ctx, 2) @@ -381,7 +378,7 @@ func ExampleCache_GetOrSetFunc(){ // } -func ExampleCache_GetOrSetFuncLock(){ +func ExampleCache_GetOrSetFuncLock() { //Create a cache object, //Of course, you can also easily use the gcache package method directly c := gcache.New() @@ -423,12 +420,12 @@ func ExampleCache_GetOrSetFuncLock(){ // 111 } -func ExampleCache_Keys(){ +func ExampleCache_Keys() { //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.SetMap(ctx, g.MapAnyAny{"k1": "v1", "k2": "v2"}, 0) //Print the current list of key values keys1, _ := c.Keys(ctx) @@ -444,10 +441,10 @@ func ExampleCache_Remove() { //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) + 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. - c.Remove(ctx,"k1") + c.Remove(ctx, "k1") data, _ := c.Data(ctx) fmt.Println(data) @@ -456,12 +453,12 @@ func ExampleCache_Remove() { // map[k2:v2 k3:v3 k4:v4] } -func ExampleCache_Removes(){ +func ExampleCache_Removes() { //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) + 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. c.Removes(ctx, g.Slice{"k1", "k3"}) @@ -472,4 +469,3 @@ func ExampleCache_Removes(){ // Output: // map[k2:v2 k4:v4] } - From 7bcba437a0da1f2b8a29fccba23b81a803d3bead Mon Sep 17 00:00:00 2001 From: 564104865 <564104865@qq.com> Date: Wed, 3 Nov 2021 23:44:16 +0800 Subject: [PATCH 5/9] update cache values --- os/gcache/gcache_z_example_cache_test.go | 63 ++++++++++++++++-------- 1 file changed, 42 insertions(+), 21 deletions(-) diff --git a/os/gcache/gcache_z_example_cache_test.go b/os/gcache/gcache_z_example_cache_test.go index 3fe724d75..70c06e0a3 100644 --- a/os/gcache/gcache_z_example_cache_test.go +++ b/os/gcache/gcache_z_example_cache_test.go @@ -114,7 +114,7 @@ func ExampleCache_SetIfNotExist() { // [] } -func ExampleCache_Sets() { +func ExampleCache_SetMap() { //Create a cache object, //Of course, you can also easily use the gcache package method directly @@ -131,16 +131,16 @@ func ExampleCache_Sets() { //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) - //Print the current key value pair - data2, _ := c.Data(ctx) //Gets the specified key value v1, _ := c.Get(ctx, "k1") + v2, _ := c.Get(ctx, "k2") + v3, _ := c.Get(ctx, "k3") - fmt.Println(data2, v1) + fmt.Println( v1,v2,v3) // Output: - // map[k1:v1 k2:v2 k3:v3] v1 + // v1 v2 v3 } func ExampleCache_Size() { @@ -173,8 +173,13 @@ func ExampleCache_Update() { c.SetMap(ctx, g.MapAnyAny{"k1": "v1", "k2": "v2", "k3": "v3"}, 0) //Print the current key value pair - data, _ := c.Data(ctx) - fmt.Println(data) + k1, _ := c.Get(ctx,"k1") + fmt.Println(k1) + k2, _ := c.Get(ctx,"k2") + fmt.Println(k2) + k3, _ := c.Get(ctx,"k3") + fmt.Println(k3) + //Update updates the value of `key` without changing its expiration and returns the old value. re, exist, _ := c.Update(ctx, "k1", "v11") @@ -185,13 +190,21 @@ func ExampleCache_Update() { re1, exist1, _ := c.Update(ctx, "k4", "v44") fmt.Println(re1, exist1) - data1, _ := c.Data(ctx) - fmt.Println(data1) + kup1, _ := c.Get(ctx,"k1") + fmt.Println(kup1) + kup2, _ := c.Get(ctx,"k2") + fmt.Println(kup2) + kup3, _ := c.Get(ctx,"k3") + fmt.Println(kup3) // Output: - // map[k1:v1 k2:v2 k3:v3] + // v1 + // v2 + // v3 // v1 true // false - // map[k1:v11 k2:v2 k3:v3] + // v11 + // v2 + // v3 } func ExampleCache_UpdateExpire() { @@ -223,15 +236,15 @@ func ExampleCache_Values() { //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. data, _ := c.Values(ctx) fmt.Println(data) - // Output: - // [map[k1:v1 k2:v2] Here is Value2 111] + // may Output: + // [map[k1:v1 k2:v2]] } func ExampleCache_Close() { @@ -292,11 +305,19 @@ func ExampleCache_Data() { 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. - data, _ := c.Data(ctx) + data, _ := c.Get(ctx,"k1") fmt.Println(data) + data1, _ := c.Get(ctx,"k2") + fmt.Println(data1) + + data2, _ := c.Get(ctx,"k5") + fmt.Println(data2) + // Output: - // map[k1:v1 k2:v2 k5:v5] + // v1 + // v2 + // v5 } func ExampleCache_Get() { @@ -441,7 +462,7 @@ func ExampleCache_Remove() { //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) + 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. c.Remove(ctx, "k1") @@ -450,7 +471,7 @@ func ExampleCache_Remove() { fmt.Println(data) // Output: - // map[k2:v2 k3:v3 k4:v4] + // map[k2:v2] } func ExampleCache_Removes() { @@ -461,11 +482,11 @@ func ExampleCache_Removes() { 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. - c.Removes(ctx, g.Slice{"k1", "k3"}) + c.Removes(ctx, g.Slice{"k1", "k2","k3"}) data, _ := c.Data(ctx) fmt.Println(data) // Output: - // map[k2:v2 k4:v4] + // map[k4:v4] } From b6f6ab17f9a811ef3be827478f987132ed4f396d Mon Sep 17 00:00:00 2001 From: 564104865 <564104865@qq.com> Date: Wed, 3 Nov 2021 23:50:57 +0800 Subject: [PATCH 6/9] go fmt --- os/gcache/gcache_z_example_cache_test.go | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/os/gcache/gcache_z_example_cache_test.go b/os/gcache/gcache_z_example_cache_test.go index 70c06e0a3..92ef33fd0 100644 --- a/os/gcache/gcache_z_example_cache_test.go +++ b/os/gcache/gcache_z_example_cache_test.go @@ -131,13 +131,12 @@ func ExampleCache_SetMap() { //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 v1, _ := c.Get(ctx, "k1") v2, _ := c.Get(ctx, "k2") v3, _ := c.Get(ctx, "k3") - fmt.Println( v1,v2,v3) + fmt.Println(v1, v2, v3) // Output: // v1 v2 v3 @@ -173,14 +172,13 @@ func ExampleCache_Update() { c.SetMap(ctx, g.MapAnyAny{"k1": "v1", "k2": "v2", "k3": "v3"}, 0) //Print the current key value pair - k1, _ := c.Get(ctx,"k1") + k1, _ := c.Get(ctx, "k1") fmt.Println(k1) - k2, _ := c.Get(ctx,"k2") + k2, _ := c.Get(ctx, "k2") fmt.Println(k2) - k3, _ := c.Get(ctx,"k3") + k3, _ := c.Get(ctx, "k3") fmt.Println(k3) - //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) @@ -190,11 +188,11 @@ func ExampleCache_Update() { re1, exist1, _ := c.Update(ctx, "k4", "v44") fmt.Println(re1, exist1) - kup1, _ := c.Get(ctx,"k1") + kup1, _ := c.Get(ctx, "k1") fmt.Println(kup1) - kup2, _ := c.Get(ctx,"k2") + kup2, _ := c.Get(ctx, "k2") fmt.Println(kup2) - kup3, _ := c.Get(ctx,"k3") + kup3, _ := c.Get(ctx, "k3") fmt.Println(kup3) // Output: // v1 @@ -305,13 +303,13 @@ func ExampleCache_Data() { 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. - data, _ := c.Get(ctx,"k1") + data, _ := c.Get(ctx, "k1") fmt.Println(data) - data1, _ := c.Get(ctx,"k2") + data1, _ := c.Get(ctx, "k2") fmt.Println(data1) - data2, _ := c.Get(ctx,"k5") + data2, _ := c.Get(ctx, "k5") fmt.Println(data2) // Output: @@ -482,7 +480,7 @@ func ExampleCache_Removes() { 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. - c.Removes(ctx, g.Slice{"k1", "k2","k3"}) + c.Removes(ctx, g.Slice{"k1", "k2", "k3"}) data, _ := c.Data(ctx) fmt.Println(data) From 357084e788eae98b177bba6922e544334f019c2c Mon Sep 17 00:00:00 2001 From: 564104865 <48121048+564104865@users.noreply.github.com> Date: Thu, 4 Nov 2021 20:55:01 +0800 Subject: [PATCH 7/9] update cache --- os/gcache/gcache_z_example_adapter_redis.go | 198 -------------------- os/gcache/gcache_z_example_cache_test.go | 195 ++++++++++--------- 2 files changed, 103 insertions(+), 290 deletions(-) delete mode 100644 os/gcache/gcache_z_example_adapter_redis.go diff --git a/os/gcache/gcache_z_example_adapter_redis.go b/os/gcache/gcache_z_example_adapter_redis.go deleted file mode 100644 index 7bbde5e3e..000000000 --- a/os/gcache/gcache_z_example_adapter_redis.go +++ /dev/null @@ -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") -} diff --git a/os/gcache/gcache_z_example_cache_test.go b/os/gcache/gcache_z_example_cache_test.go index 92ef33fd0..be2cdca4e 100644 --- a/os/gcache/gcache_z_example_cache_test.go +++ b/os/gcache/gcache_z_example_cache_test.go @@ -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] } 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) From 613a50428b4ebeaff51e890fb6cfaa131a618257 Mon Sep 17 00:00:00 2001 From: 564104865 <48121048+564104865@users.noreply.github.com> Date: Thu, 4 Nov 2021 23:23:40 +0800 Subject: [PATCH 8/9] Update gcache_z_example_cache_test.go --- os/gcache/gcache_z_example_cache_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/os/gcache/gcache_z_example_cache_test.go b/os/gcache/gcache_z_example_cache_test.go index be2cdca4e..c1ca72b51 100644 --- a/os/gcache/gcache_z_example_cache_test.go +++ b/os/gcache/gcache_z_example_cache_test.go @@ -451,14 +451,14 @@ func ExampleCache_Keys() { // 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.SetMap(ctx, g.MapAnyAny{"k1": "v1"}, 0) // Print the current list of key values keys1, _ := c.Keys(ctx) fmt.Println(keys1) // Output: - // [k1 k2] + // [k1] } From 5b83b2375e420d87e371bedf3ee9a8de6c3779e8 Mon Sep 17 00:00:00 2001 From: 564104865 <48121048+564104865@users.noreply.github.com> Date: Fri, 5 Nov 2021 09:41:49 +0800 Subject: [PATCH 9/9] Update gcache_z_example_cache_test.go --- os/gcache/gcache_z_example_cache_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/os/gcache/gcache_z_example_cache_test.go b/os/gcache/gcache_z_example_cache_test.go index c1ca72b51..d28acefba 100644 --- a/os/gcache/gcache_z_example_cache_test.go +++ b/os/gcache/gcache_z_example_cache_test.go @@ -10,7 +10,7 @@ import ( func ExampleNew() { // Create a cache object, - // Of course, you can also easily use the gcache package method directly + // Of course, you can also easily use the gcache package method directly. c := gcache.New() // Set cache without expiration