From 45787dd8e464e54750e742e9bb64ade4118f97fd Mon Sep 17 00:00:00 2001 From: 564104865 <48121048+564104865@users.noreply.github.com> Date: Sat, 6 Nov 2021 20:31:16 +0800 Subject: [PATCH 01/10] 11 --- .../gcache_z_example_adapter_redis_test.go | 190 ++++++++++++++++++ os/gcache/gcache_z_example_cache_test.go | 18 +- 2 files changed, 195 insertions(+), 13 deletions(-) create mode 100644 os/gcache/gcache_z_example_adapter_redis_test.go diff --git a/os/gcache/gcache_z_example_adapter_redis_test.go b/os/gcache/gcache_z_example_adapter_redis_test.go new file mode 100644 index 000000000..4c9ff7e86 --- /dev/null +++ b/os/gcache/gcache_z_example_adapter_redis_test.go @@ -0,0 +1,190 @@ +package gcache_test + +import ( + "context" + "github.com/gogf/gf/v2/container/gvar" + "github.com/gogf/gf/v2/os/gcache" + "github.com/gogf/gf/v2/os/gtimer" + + "time" +) + + +// Redis is the gcache adapter implements using Redis server. +type T_cache struct { + tcache *gcache.Adapter +} + + +// NewRedis creates and returns a new redis memory cache object. +func NewTcache(tcache *gcache.Adapter) gcache.Adapter { + + + return &T_cache{ + tcache: tcache, + } + +} + +// 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 *T_cache) Set(ctx context.Context, key interface{}, value interface{}, duration time.Duration) error { + return r.Set(ctx, key, value, duration) +} + +// 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 *T_cache) SetMap(ctx context.Context, data map[interface{}]interface{}, duration time.Duration) error { + return r.SetMap(ctx, data, duration) +} + +// 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 *T_cache) SetIfNotExist(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (ok bool, err error) { + return r.SetIfNotExist(ctx, key, value, duration) +} + +// 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 *T_cache) SetIfNotExistFunc(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (ok bool, err error) { + return r.SetIfNotExistFunc(ctx, key, f, duration) +} + +// 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 *T_cache) SetIfNotExistFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (ok bool, err error) { + return r.SetIfNotExistFuncLock(ctx, key, f, duration) +} + +// 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 *T_cache) Get(ctx context.Context, key interface{}) (*gvar.Var, error) { + return r.Get(ctx, key) +} + +// 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 *T_cache) GetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (result *gvar.Var, err error) { + return r.GetOrSet(ctx, key, value, duration) +} + +// 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 *T_cache) GetOrSetFunc(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (result *gvar.Var, err error) { + return r.GetOrSetFunc(ctx, key, f, duration) +} + +// 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 *T_cache) GetOrSetFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (result *gvar.Var, err error) { + return r.GetOrSetFuncLock(ctx, key, f, duration) +} + +// Contains checks and returns true if `key` exists in the cache, or else returns false. +func (r *T_cache) Contains(ctx context.Context, key interface{}) (bool, error) { + return r.Contains(ctx, key) +} + +// Size returns the number of items in the cache. +func (r *T_cache) Size(ctx context.Context) (size int, err error) { + return r.Size(ctx) +} + +// 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 *T_cache) Data(ctx context.Context) (data map[interface{}]interface{}, err error) { + return r.Data(ctx) +} + +// Keys returns all keys in the cache as slice. +func (r *T_cache) Keys(ctx context.Context) (keys []interface{}, err error) { + return r.Keys(ctx) +} + +// Values returns all values in the cache as slice. +func (r *T_cache) Values(ctx context.Context) (values []interface{}, err error) { + return r.Values(ctx) +} + +// 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 *T_cache) Update(ctx context.Context, key interface{}, value interface{}) (oldValue *gvar.Var, exist bool, err error) { + return r.Update(ctx, key, value) +} + +// 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 *T_cache) UpdateExpire(ctx context.Context, key interface{}, duration time.Duration) (oldDuration time.Duration, err error) { + return r.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 *T_cache) GetExpire(ctx context.Context, key interface{}) (time.Duration, error) { + return r.GetExpire(ctx, key) +} + +// 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 *T_cache) Remove(ctx context.Context, keys ...interface{}) (lastValue *gvar.Var, err error) { + return r.Remove(ctx, keys) +} + +// Clear clears all data of the cache. +// Note that this function is sensitive and should be carefully used. +func (r *T_cache) Clear(ctx context.Context) error { + return r.Clear(ctx) +} + +// Close closes the cache if necessary. +func (r *T_cache) Close(ctx context.Context) error { + return r.Close(ctx) +} diff --git a/os/gcache/gcache_z_example_cache_test.go b/os/gcache/gcache_z_example_cache_test.go index d28acefba..07ec155b0 100644 --- a/os/gcache/gcache_z_example_cache_test.go +++ b/os/gcache/gcache_z_example_cache_test.go @@ -66,7 +66,7 @@ func ExampleCache_SetAdapters() { // 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() + adapter := gcache.NewTcache() c.SetAdapter(adapter) // Set cache @@ -301,24 +301,16 @@ 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"}, 0) + //c.Set(ctx, "k5", "v5", 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.Data(ctx) fmt.Println(data) - data1, _ := c.Get(ctx, "k2") - fmt.Println(data1) - - data2, _ := c.Get(ctx, "k5") - fmt.Println(data2) - // Output: - // v1 - // v2 - // v5 + // map[k1:v1] } func ExampleCache_Get() { From 27576472115a7292c69677d7193b87e54a2a6dd7 Mon Sep 17 00:00:00 2001 From: 564104865 <48121048+564104865@users.noreply.github.com> Date: Sat, 6 Nov 2021 20:31:47 +0800 Subject: [PATCH 02/10] Revert "11" This reverts commit 45787dd8e464e54750e742e9bb64ade4118f97fd. --- .../gcache_z_example_adapter_redis_test.go | 190 ------------------ os/gcache/gcache_z_example_cache_test.go | 18 +- 2 files changed, 13 insertions(+), 195 deletions(-) delete mode 100644 os/gcache/gcache_z_example_adapter_redis_test.go diff --git a/os/gcache/gcache_z_example_adapter_redis_test.go b/os/gcache/gcache_z_example_adapter_redis_test.go deleted file mode 100644 index 4c9ff7e86..000000000 --- a/os/gcache/gcache_z_example_adapter_redis_test.go +++ /dev/null @@ -1,190 +0,0 @@ -package gcache_test - -import ( - "context" - "github.com/gogf/gf/v2/container/gvar" - "github.com/gogf/gf/v2/os/gcache" - "github.com/gogf/gf/v2/os/gtimer" - - "time" -) - - -// Redis is the gcache adapter implements using Redis server. -type T_cache struct { - tcache *gcache.Adapter -} - - -// NewRedis creates and returns a new redis memory cache object. -func NewTcache(tcache *gcache.Adapter) gcache.Adapter { - - - return &T_cache{ - tcache: tcache, - } - -} - -// 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 *T_cache) Set(ctx context.Context, key interface{}, value interface{}, duration time.Duration) error { - return r.Set(ctx, key, value, duration) -} - -// 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 *T_cache) SetMap(ctx context.Context, data map[interface{}]interface{}, duration time.Duration) error { - return r.SetMap(ctx, data, duration) -} - -// 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 *T_cache) SetIfNotExist(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (ok bool, err error) { - return r.SetIfNotExist(ctx, key, value, duration) -} - -// 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 *T_cache) SetIfNotExistFunc(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (ok bool, err error) { - return r.SetIfNotExistFunc(ctx, key, f, duration) -} - -// 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 *T_cache) SetIfNotExistFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (ok bool, err error) { - return r.SetIfNotExistFuncLock(ctx, key, f, duration) -} - -// 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 *T_cache) Get(ctx context.Context, key interface{}) (*gvar.Var, error) { - return r.Get(ctx, key) -} - -// 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 *T_cache) GetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (result *gvar.Var, err error) { - return r.GetOrSet(ctx, key, value, duration) -} - -// 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 *T_cache) GetOrSetFunc(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (result *gvar.Var, err error) { - return r.GetOrSetFunc(ctx, key, f, duration) -} - -// 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 *T_cache) GetOrSetFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (result *gvar.Var, err error) { - return r.GetOrSetFuncLock(ctx, key, f, duration) -} - -// Contains checks and returns true if `key` exists in the cache, or else returns false. -func (r *T_cache) Contains(ctx context.Context, key interface{}) (bool, error) { - return r.Contains(ctx, key) -} - -// Size returns the number of items in the cache. -func (r *T_cache) Size(ctx context.Context) (size int, err error) { - return r.Size(ctx) -} - -// 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 *T_cache) Data(ctx context.Context) (data map[interface{}]interface{}, err error) { - return r.Data(ctx) -} - -// Keys returns all keys in the cache as slice. -func (r *T_cache) Keys(ctx context.Context) (keys []interface{}, err error) { - return r.Keys(ctx) -} - -// Values returns all values in the cache as slice. -func (r *T_cache) Values(ctx context.Context) (values []interface{}, err error) { - return r.Values(ctx) -} - -// 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 *T_cache) Update(ctx context.Context, key interface{}, value interface{}) (oldValue *gvar.Var, exist bool, err error) { - return r.Update(ctx, key, value) -} - -// 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 *T_cache) UpdateExpire(ctx context.Context, key interface{}, duration time.Duration) (oldDuration time.Duration, err error) { - return r.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 *T_cache) GetExpire(ctx context.Context, key interface{}) (time.Duration, error) { - return r.GetExpire(ctx, key) -} - -// 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 *T_cache) Remove(ctx context.Context, keys ...interface{}) (lastValue *gvar.Var, err error) { - return r.Remove(ctx, keys) -} - -// Clear clears all data of the cache. -// Note that this function is sensitive and should be carefully used. -func (r *T_cache) Clear(ctx context.Context) error { - return r.Clear(ctx) -} - -// Close closes the cache if necessary. -func (r *T_cache) Close(ctx context.Context) error { - return r.Close(ctx) -} diff --git a/os/gcache/gcache_z_example_cache_test.go b/os/gcache/gcache_z_example_cache_test.go index 07ec155b0..d28acefba 100644 --- a/os/gcache/gcache_z_example_cache_test.go +++ b/os/gcache/gcache_z_example_cache_test.go @@ -66,7 +66,7 @@ func ExampleCache_SetAdapters() { // 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.NewTcache() + adapter := gcache.New() c.SetAdapter(adapter) // Set cache @@ -301,16 +301,24 @@ 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"}, 0) - //c.Set(ctx, "k5", "v5", 0) + c.SetMap(ctx, g.MapAnyAny{"k1": "v1", "k2": "v2"}, 0) + c.Set(ctx, "k5", "v5", 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.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] + // v1 + // v2 + // v5 } func ExampleCache_Get() { From d61f7af60cf46adafaf95f1b57e91c499e73fb8f Mon Sep 17 00:00:00 2001 From: 564104865 <48121048+564104865@users.noreply.github.com> Date: Mon, 8 Nov 2021 12:12:10 +0800 Subject: [PATCH 03/10] update cache adapter --- .../gcache_z_example_adapter_cache_test.go | 186 ++++++++++++++++++ os/gcache/gcache_z_example_cache_test.go | 10 +- 2 files changed, 192 insertions(+), 4 deletions(-) create mode 100644 os/gcache/gcache_z_example_adapter_cache_test.go diff --git a/os/gcache/gcache_z_example_adapter_cache_test.go b/os/gcache/gcache_z_example_adapter_cache_test.go new file mode 100644 index 000000000..c42040f45 --- /dev/null +++ b/os/gcache/gcache_z_example_adapter_cache_test.go @@ -0,0 +1,186 @@ +package gcache_test + +import ( + "context" + "github.com/gogf/gf/v2/os/gcache" + + "fmt" + "github.com/gogf/gf/v2/container/gvar" + "time" +) + +// Test cache is the gcache adapter implements using Redis server. +type T_cache struct { + cache *gcache.AdapterMemory +} + +// NewCache creates and returns a new cache memory cache object. +func NewCache(redis *gcache.AdapterMemory) gcache.Adapter { + return &T_cache{ + cache: 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 *T_cache) Set(ctx context.Context, key interface{}, value interface{}, duration time.Duration) error { + fmt.Println("111111111") + return r.cache.Set(ctx, key, value, duration) +} + +// 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 *T_cache) SetMap(ctx context.Context, data map[interface{}]interface{}, duration time.Duration) error { + return r.cache.SetMap(ctx, data, duration) +} + +// 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 *T_cache) SetIfNotExist(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (ok bool, err error) { + return r.cache.SetIfNotExist(ctx, key, value, duration) +} + +// 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 *T_cache) SetIfNotExistFunc(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (ok bool, err error) { + return r.cache.SetIfNotExistFunc(ctx, key, f, duration) +} + +// 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 *T_cache) SetIfNotExistFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (ok bool, err error) { + return r.cache.SetIfNotExistFuncLock(ctx, key, f, duration) +} + +// 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 *T_cache) Get(ctx context.Context, key interface{}) (*gvar.Var, error) { + return r.cache.Get(ctx, key) +} + +// 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 *T_cache) GetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (result *gvar.Var, err error) { + return r.cache.GetOrSet(ctx, key, value, duration) +} + +// 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 *T_cache) GetOrSetFunc(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (result *gvar.Var, err error) { + return r.cache.GetOrSetFunc(ctx, key, f, duration) +} + +// 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 *T_cache) GetOrSetFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (result *gvar.Var, err error) { + return r.cache.GetOrSetFuncLock(ctx, key, f, duration) +} + +// Contains checks and returns true if `key` exists in the cache, or else returns false. +func (r *T_cache) Contains(ctx context.Context, key interface{}) (bool, error) { + return r.cache.Contains(ctx, key) +} + +// Size returns the number of items in the cache. +func (r *T_cache) Size(ctx context.Context) (size int, err error) { + return r.cache.Size(ctx) +} + +// 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 *T_cache) Data(ctx context.Context) (data map[interface{}]interface{}, err error) { + return r.cache.Data(ctx) +} + +// Keys returns all keys in the cache as slice. +func (r *T_cache) Keys(ctx context.Context) (keys []interface{}, err error) { + return r.cache.Keys(ctx) +} + +// Values returns all values in the cache as slice. +func (r *T_cache) Values(ctx context.Context) (values []interface{}, err error) { + return r.cache.Values(ctx) +} + +// 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 *T_cache) Update(ctx context.Context, key interface{}, value interface{}) (oldValue *gvar.Var, exist bool, err error) { + return r.cache.Update(ctx, key, value) +} + +// 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 *T_cache) UpdateExpire(ctx context.Context, key interface{}, duration time.Duration) (oldDuration time.Duration, err error) { + return r.cache.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 *T_cache) GetExpire(ctx context.Context, key interface{}) (time.Duration, error) { + return r.cache.GetExpire(ctx, key) +} + +// 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 *T_cache) Remove(ctx context.Context, keys ...interface{}) (lastValue *gvar.Var, err error) { + return r.cache.Remove(ctx, keys) +} + +// Clear clears all data of the cache. +// Note that this function is sensitive and should be carefully used. +func (r *T_cache) Clear(ctx context.Context) error { + return r.cache.Clear(ctx) +} + +// Close closes the cache if necessary. +func (r *T_cache) Close(ctx context.Context) error { + return r.cache.Close(ctx) +} diff --git a/os/gcache/gcache_z_example_cache_test.go b/os/gcache/gcache_z_example_cache_test.go index d28acefba..07d535407 100644 --- a/os/gcache/gcache_z_example_cache_test.go +++ b/os/gcache/gcache_z_example_cache_test.go @@ -66,16 +66,16 @@ func ExampleCache_SetAdapters() { // 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) + adapter := NewCache(gcache.NewAdapterMemory()) + 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")) // Output: + // 111111111 // [1,2,3,4,5,6,7,8,9] } @@ -472,12 +472,14 @@ func ExampleCache_Remove() { // 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") + remove, _ := c.Remove(ctx, "k1") + fmt.Println(remove) data, _ := c.Data(ctx) fmt.Println(data) // Output: + // v1 // map[k2:v2] } From 903299728fe9445a942d68c2b73240199e1f21db Mon Sep 17 00:00:00 2001 From: 546104865 <564104865@qq.com> Date: Tue, 9 Nov 2021 11:46:53 +0800 Subject: [PATCH 04/10] update Must Cache --- .../gcache_z_example_adapter_cache_test.go | 186 ------------ os/gcache/gcache_z_example_cache_test.go | 282 ++++++++++++++++-- 2 files changed, 250 insertions(+), 218 deletions(-) delete mode 100644 os/gcache/gcache_z_example_adapter_cache_test.go diff --git a/os/gcache/gcache_z_example_adapter_cache_test.go b/os/gcache/gcache_z_example_adapter_cache_test.go deleted file mode 100644 index c42040f45..000000000 --- a/os/gcache/gcache_z_example_adapter_cache_test.go +++ /dev/null @@ -1,186 +0,0 @@ -package gcache_test - -import ( - "context" - "github.com/gogf/gf/v2/os/gcache" - - "fmt" - "github.com/gogf/gf/v2/container/gvar" - "time" -) - -// Test cache is the gcache adapter implements using Redis server. -type T_cache struct { - cache *gcache.AdapterMemory -} - -// NewCache creates and returns a new cache memory cache object. -func NewCache(redis *gcache.AdapterMemory) gcache.Adapter { - return &T_cache{ - cache: 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 *T_cache) Set(ctx context.Context, key interface{}, value interface{}, duration time.Duration) error { - fmt.Println("111111111") - return r.cache.Set(ctx, key, value, duration) -} - -// 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 *T_cache) SetMap(ctx context.Context, data map[interface{}]interface{}, duration time.Duration) error { - return r.cache.SetMap(ctx, data, duration) -} - -// 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 *T_cache) SetIfNotExist(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (ok bool, err error) { - return r.cache.SetIfNotExist(ctx, key, value, duration) -} - -// 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 *T_cache) SetIfNotExistFunc(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (ok bool, err error) { - return r.cache.SetIfNotExistFunc(ctx, key, f, duration) -} - -// 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 *T_cache) SetIfNotExistFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (ok bool, err error) { - return r.cache.SetIfNotExistFuncLock(ctx, key, f, duration) -} - -// 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 *T_cache) Get(ctx context.Context, key interface{}) (*gvar.Var, error) { - return r.cache.Get(ctx, key) -} - -// 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 *T_cache) GetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (result *gvar.Var, err error) { - return r.cache.GetOrSet(ctx, key, value, duration) -} - -// 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 *T_cache) GetOrSetFunc(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (result *gvar.Var, err error) { - return r.cache.GetOrSetFunc(ctx, key, f, duration) -} - -// 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 *T_cache) GetOrSetFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (result *gvar.Var, err error) { - return r.cache.GetOrSetFuncLock(ctx, key, f, duration) -} - -// Contains checks and returns true if `key` exists in the cache, or else returns false. -func (r *T_cache) Contains(ctx context.Context, key interface{}) (bool, error) { - return r.cache.Contains(ctx, key) -} - -// Size returns the number of items in the cache. -func (r *T_cache) Size(ctx context.Context) (size int, err error) { - return r.cache.Size(ctx) -} - -// 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 *T_cache) Data(ctx context.Context) (data map[interface{}]interface{}, err error) { - return r.cache.Data(ctx) -} - -// Keys returns all keys in the cache as slice. -func (r *T_cache) Keys(ctx context.Context) (keys []interface{}, err error) { - return r.cache.Keys(ctx) -} - -// Values returns all values in the cache as slice. -func (r *T_cache) Values(ctx context.Context) (values []interface{}, err error) { - return r.cache.Values(ctx) -} - -// 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 *T_cache) Update(ctx context.Context, key interface{}, value interface{}) (oldValue *gvar.Var, exist bool, err error) { - return r.cache.Update(ctx, key, value) -} - -// 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 *T_cache) UpdateExpire(ctx context.Context, key interface{}, duration time.Duration) (oldDuration time.Duration, err error) { - return r.cache.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 *T_cache) GetExpire(ctx context.Context, key interface{}) (time.Duration, error) { - return r.cache.GetExpire(ctx, key) -} - -// 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 *T_cache) Remove(ctx context.Context, keys ...interface{}) (lastValue *gvar.Var, err error) { - return r.cache.Remove(ctx, keys) -} - -// Clear clears all data of the cache. -// Note that this function is sensitive and should be carefully used. -func (r *T_cache) Clear(ctx context.Context) error { - return r.cache.Clear(ctx) -} - -// Close closes the cache if necessary. -func (r *T_cache) Close(ctx context.Context) error { - return r.cache.Close(ctx) -} diff --git a/os/gcache/gcache_z_example_cache_test.go b/os/gcache/gcache_z_example_cache_test.go index 24722dbf0..a8949d67e 100644 --- a/os/gcache/gcache_z_example_cache_test.go +++ b/os/gcache/gcache_z_example_cache_test.go @@ -58,27 +58,6 @@ func ExampleCache_Set() { // [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 := NewCache(gcache.NewAdapterMemory()) - 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")) - - // Output: - // 111111111 - // [1,2,3,4,5,6,7,8,9] -} - func ExampleCache_SetIfNotExist() { // Create a cache object, @@ -301,24 +280,19 @@ 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"}, 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.Data(ctx) fmt.Println(data) - data1, _ := c.Get(ctx, "k2") + // Set Cache + c.Set(ctx, "k5", "v5", 0) + data1, _ := c.Get(ctx, "k1") fmt.Println(data1) - data2, _ := c.Get(ctx, "k5") - fmt.Println(data2) - // Output: + // map[k1:v1] // v1 - // v2 - // v5 } func ExampleCache_Get() { @@ -501,3 +475,247 @@ func ExampleCache_Removes() { // Output: // map[k4:v4] } + +func ExampleCache_MustGet() { + // Intercepting panic exception information + // err is empty, so panic is not performed + defer func() { + if r := recover(); r != nil { + fmt.Println("recover...:", r) + } + }() + + // 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) + + // MustGet acts like Get, but it panics if any error occurs. + k2 := c.MustGet(ctx, "k2") + fmt.Println(k2) + + // + k1 := c.MustGet(ctx, "k1") + fmt.Println(k1) + + // Output: + // v1 + +} + +func ExampleCache_MustGetOrSet() { + + // Create a cache object, + // Of course, you can also easily use the gcache package method directly + c := gcache.New() + + // MustGetOrSet acts like GetOrSet, but it panics if any error occurs. + k1 := c.MustGetOrSet(ctx, "k1", "v1", 0) + fmt.Println(k1) + + k2 := c.MustGetOrSet(ctx, "k1", "v2", 0) + fmt.Println(k2) + + // Output: + // v1 + // v1 + +} + +func ExampleCache_MustGetOrSetFunc() { + + // Create a cache object, + // Of course, you can also easily use the gcache package method directly + c := gcache.New() + + // MustGetOrSetFunc acts like GetOrSetFunc, but it panics if any error occurs. + c.MustGetOrSetFunc(ctx, 1, func() (interface{}, error) { + return 111, nil + }, 10000*time.Millisecond) + v := c.MustGet(ctx, 1) + fmt.Println(v) + + c.MustGetOrSetFunc(ctx, 2, func() (interface{}, error) { + return nil, nil + }, 10000*time.Millisecond) + v1 := c.MustGet(ctx, 2) + fmt.Println(v1) + + // Output: + // 111 + // +} + +func ExampleCache_MustGetOrSetFuncLock() { + + // Create a cache object, + // Of course, you can also easily use the gcache package method directly + c := gcache.New() + + // MustGetOrSetFuncLock acts like GetOrSetFuncLock, but it panics if any error occurs. + c.MustGetOrSetFuncLock(ctx, 1, func() (interface{}, error) { + return 11, nil + }, 0) + v := c.MustGet(ctx, 1) + fmt.Println(v) + + // Modification failed + c.MustGetOrSetFuncLock(ctx, 1, func() (interface{}, error) { + return 111, nil + }, 0) + v = c.MustGet(ctx, 1) + fmt.Println(v) + + c.Remove(ctx, g.Slice{1, 2, 3}...) + + // Modify locking + c.MustGetOrSetFuncLock(ctx, 1, func() (interface{}, error) { + return 111, nil + }, 0) + v = c.MustGet(ctx, 1) + fmt.Println(v) + + // Modification failed + c.MustGetOrSetFuncLock(ctx, 1, func() (interface{}, error) { + return 11, nil + }, 0) + v = c.MustGet(ctx, 1) + fmt.Println(v) + + // Output: + // 11 + // 11 + // 111 + // 111 +} + +func ExampleCache_MustContains() { + + // 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.MustContains(ctx, "k") + fmt.Println(data) + + // return false + data1 := c.MustContains(ctx, "k1") + fmt.Println(data1) + + // Output: + // true + // false + +} + +func ExampleCache_MustGetExpire() { + + // 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) + + // MustGetExpire acts like GetExpire, but it panics if any error occurs. + expire := c.MustGetExpire(ctx, "k") + fmt.Println(expire) + + // Output: + // 10s +} + +func ExampleCache_MustSize() { + + // 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.MustSize(ctx) + fmt.Println(n) + + // Output: + // 10 +} + +func ExampleCache_MustData() { + + // 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"}, 0) + + data := c.MustData(ctx) + fmt.Println(data) + + // Set Cache + c.Set(ctx, "k5", "v5", 0) + data1, _ := c.Get(ctx, "k1") + fmt.Println(data1) + + // Output: + // map[k1:v1] + // v1 +} + +func ExampleCache_MustKeys() { + + // 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"}, 0) + + // MustKeys acts like Keys, but it panics if any error occurs. + keys1, _ := c.Keys(ctx) + fmt.Println(keys1) + + // Output: + // [k1] + +} + +func ExampleCache_MustKeyStrings() { + c := gcache.New() + + c.SetMap(ctx, g.MapAnyAny{"k1": "v1"}, 0) + + // MustKeyStrings returns all keys in the cache as string slice. + // MustKeyStrings acts like KeyStrings, but it panics if any error occurs. + keys := c.MustKeyStrings(ctx) + fmt.Println(keys) + + // Output: + // [k1] +} + +func ExampleCache_MustValues() { + + // 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"}, 0) + + // Values returns all values in the cache as slice. + data := c.MustValues(ctx) + fmt.Println(data) + + // Output: + // [map[k1:v1]] +} From 30f462288e7903072129d909cbc4522b93f204a2 Mon Sep 17 00:00:00 2001 From: 546104865 <564104865@qq.com> Date: Wed, 10 Nov 2021 16:39:26 +0800 Subject: [PATCH 05/10] Update gcache_z_example_cache_test.go --- os/gcache/gcache_z_example_cache_test.go | 100 ++++++++--------------- 1 file changed, 33 insertions(+), 67 deletions(-) diff --git a/os/gcache/gcache_z_example_cache_test.go b/os/gcache/gcache_z_example_cache_test.go index a8949d67e..a328320a0 100644 --- a/os/gcache/gcache_z_example_cache_test.go +++ b/os/gcache/gcache_z_example_cache_test.go @@ -359,21 +359,21 @@ func ExampleCache_GetOrSetFunc() { // 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 + c.GetOrSetFunc(ctx, "k1", func() (interface{}, error) { + return "v1", nil }, 10000*time.Millisecond) - v, _ := c.Get(ctx, 1) + v, _ := c.Get(ctx, "k1") fmt.Println(v) - c.GetOrSetFunc(ctx, 2, func() (interface{}, error) { + // If func returns nil, no action is taken + c.GetOrSetFunc(ctx, "k2", func() (interface{}, error) { return nil, nil }, 10000*time.Millisecond) - v1, _ := c.Get(ctx, 2) + v1, _ := c.Get(ctx, "k2") fmt.Println(v1) // Output: - // 111 - // + // v1 } func ExampleCache_GetOrSetFuncLock() { @@ -383,40 +383,24 @@ func ExampleCache_GetOrSetFuncLock() { 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 + c.GetOrSetFuncLock(ctx, "k1", func() (interface{}, error) { + return "v1", nil }, 0) - v, _ := c.Get(ctx, 1) + v, _ := c.Get(ctx, "k1") fmt.Println(v) // Modification failed - c.GetOrSetFuncLock(ctx, 1, func() (interface{}, error) { - return 111, nil + c.GetOrSetFuncLock(ctx, "k1", func() (interface{}, error) { + return "update v1", nil }, 0) - v, _ = c.Get(ctx, 1) + v, _ = c.Get(ctx, "k1") 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) + c.Remove(ctx, g.Slice{"k1"}...) // Output: - // 11 - // 11 - // 111 - // 111 + // v1 + // v1 } func ExampleCache_Keys() { @@ -555,40 +539,22 @@ func ExampleCache_MustGetOrSetFuncLock() { c := gcache.New() // MustGetOrSetFuncLock acts like GetOrSetFuncLock, but it panics if any error occurs. - c.MustGetOrSetFuncLock(ctx, 1, func() (interface{}, error) { - return 11, nil + c.MustGetOrSetFuncLock(ctx, "k1", func() (interface{}, error) { + return "v1", nil }, 0) - v := c.MustGet(ctx, 1) + v := c.MustGet(ctx, "k1") fmt.Println(v) // Modification failed - c.MustGetOrSetFuncLock(ctx, 1, func() (interface{}, error) { - return 111, nil + c.MustGetOrSetFuncLock(ctx, "k1", func() (interface{}, error) { + return "update v1", nil }, 0) - v = c.MustGet(ctx, 1) - fmt.Println(v) - - c.Remove(ctx, g.Slice{1, 2, 3}...) - - // Modify locking - c.MustGetOrSetFuncLock(ctx, 1, func() (interface{}, error) { - return 111, nil - }, 0) - v = c.MustGet(ctx, 1) - fmt.Println(v) - - // Modification failed - c.MustGetOrSetFuncLock(ctx, 1, func() (interface{}, error) { - return 11, nil - }, 0) - v = c.MustGet(ctx, 1) + v = c.MustGet(ctx, "k1") fmt.Println(v) // Output: - // 11 - // 11 - // 111 - // 111 + // v1 + // v1 } func ExampleCache_MustContains() { @@ -628,7 +594,7 @@ func ExampleCache_MustGetExpire() { expire := c.MustGetExpire(ctx, "k") fmt.Println(expire) - // Output: + // May Output: // 10s } @@ -678,29 +644,29 @@ func ExampleCache_MustKeys() { // Of course, you can also easily use the gcache package method directly c := gcache.New() - c.SetMap(ctx, g.MapAnyAny{"k1": "v1"}, 0) + c.SetMap(ctx, g.MapAnyAny{"k1": "v1", "k2": "v2"}, 0) // MustKeys acts like Keys, but it panics if any error occurs. keys1, _ := c.Keys(ctx) fmt.Println(keys1) - // Output: - // [k1] + // May Output: + // [k1 k2] } func ExampleCache_MustKeyStrings() { c := gcache.New() - c.SetMap(ctx, g.MapAnyAny{"k1": "v1"}, 0) + c.SetMap(ctx, g.MapAnyAny{"k1": "v1", "k2": "v2"}, 0) // MustKeyStrings returns all keys in the cache as string slice. // MustKeyStrings acts like KeyStrings, but it panics if any error occurs. keys := c.MustKeyStrings(ctx) fmt.Println(keys) - // Output: - // [k1] + // May Output: + // [k1 k2] } func ExampleCache_MustValues() { @@ -710,12 +676,12 @@ func ExampleCache_MustValues() { c := gcache.New() // Write value - c.Set(ctx, "k1", g.Map{"k1": "v1"}, 0) + c.Set(ctx, "k1", "v1", 0) // Values returns all values in the cache as slice. data := c.MustValues(ctx) fmt.Println(data) // Output: - // [map[k1:v1]] + // [v1] } From 655426c322cfda6754af130d1ac1b0e51efc3fbc Mon Sep 17 00:00:00 2001 From: 546104865 <564104865@qq.com> Date: Thu, 11 Nov 2021 21:19:33 +0800 Subject: [PATCH 06/10] Update gcache_z_example_cache_test.go --- os/gcache/gcache_z_example_cache_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/os/gcache/gcache_z_example_cache_test.go b/os/gcache/gcache_z_example_cache_test.go index a328320a0..f9a8193a4 100644 --- a/os/gcache/gcache_z_example_cache_test.go +++ b/os/gcache/gcache_z_example_cache_test.go @@ -480,7 +480,6 @@ func ExampleCache_MustGet() { k2 := c.MustGet(ctx, "k2") fmt.Println(k2) - // k1 := c.MustGet(ctx, "k1") fmt.Println(k1) From 9b663b4c2bf4752f7c5970c0ea64273234257aca Mon Sep 17 00:00:00 2001 From: 546104865 <564104865@qq.com> Date: Thu, 11 Nov 2021 22:18:01 +0800 Subject: [PATCH 07/10] Update gcache_z_example_cache_test.go --- os/gcache/gcache_z_example_cache_test.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/os/gcache/gcache_z_example_cache_test.go b/os/gcache/gcache_z_example_cache_test.go index f9a8193a4..587467e2e 100644 --- a/os/gcache/gcache_z_example_cache_test.go +++ b/os/gcache/gcache_z_example_cache_test.go @@ -420,6 +420,19 @@ func ExampleCache_Keys() { } +func ExampleCache_KeyStrings() { + c := gcache.New() + + c.SetMap(ctx, g.MapAnyAny{"k1": "v1", "k2": "v2"}, 0) + + // KeyStrings returns all keys in the cache as string slice. + keys, _ := c.KeyStrings(ctx) + fmt.Println(keys) + + // May Output: + // [k1 k2] +} + func ExampleCache_Remove() { // Create a cache object, @@ -646,7 +659,7 @@ func ExampleCache_MustKeys() { c.SetMap(ctx, g.MapAnyAny{"k1": "v1", "k2": "v2"}, 0) // MustKeys acts like Keys, but it panics if any error occurs. - keys1, _ := c.Keys(ctx) + keys1 := c.MustKeys(ctx) fmt.Println(keys1) // May Output: From 01a50b06cb960604250e170b25664e6f23646c38 Mon Sep 17 00:00:00 2001 From: 546104865 <564104865@qq.com> Date: Thu, 11 Nov 2021 22:32:08 +0800 Subject: [PATCH 08/10] 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 587467e2e..76128d27d 100644 --- a/os/gcache/gcache_z_example_cache_test.go +++ b/os/gcache/gcache_z_example_cache_test.go @@ -327,7 +327,7 @@ func ExampleCache_GetExpire() { expire, _ := c.GetExpire(ctx, "k") fmt.Println(expire) - // Output: + // May Output: // 10s } From 195e167502eda3426706e1243a55bd26f5db4cee Mon Sep 17 00:00:00 2001 From: 546104865 <564104865@qq.com> Date: Thu, 11 Nov 2021 23:29:29 +0800 Subject: [PATCH 09/10] Update gcache_z_example_cache_test.go --- os/gcache/gcache_z_example_cache_test.go | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/os/gcache/gcache_z_example_cache_test.go b/os/gcache/gcache_z_example_cache_test.go index 76128d27d..68a207472 100644 --- a/os/gcache/gcache_z_example_cache_test.go +++ b/os/gcache/gcache_z_example_cache_test.go @@ -527,16 +527,16 @@ func ExampleCache_MustGetOrSetFunc() { c := gcache.New() // MustGetOrSetFunc acts like GetOrSetFunc, but it panics if any error occurs. - c.MustGetOrSetFunc(ctx, 1, func() (interface{}, error) { - return 111, nil + c.MustGetOrSetFunc(ctx, "k1", func() (interface{}, error) { + return "v1", nil }, 10000*time.Millisecond) - v := c.MustGet(ctx, 1) + v := c.MustGet(ctx, "k1") fmt.Println(v) - c.MustGetOrSetFunc(ctx, 2, func() (interface{}, error) { + c.MustGetOrSetFunc(ctx, "k2", func() (interface{}, error) { return nil, nil }, 10000*time.Millisecond) - v1 := c.MustGet(ctx, 2) + v1 := c.MustGet(ctx, "k2") fmt.Println(v1) // Output: @@ -635,19 +635,13 @@ func ExampleCache_MustData() { // Of course, you can also easily use the gcache package method directly c := gcache.New() - c.SetMap(ctx, g.MapAnyAny{"k1": "v1"}, 0) + c.SetMap(ctx, g.MapAnyAny{"k1": "v1", "k2": "v2"}, 0) data := c.MustData(ctx) fmt.Println(data) - // Set Cache - c.Set(ctx, "k5", "v5", 0) - data1, _ := c.Get(ctx, "k1") - fmt.Println(data1) - - // Output: - // map[k1:v1] - // v1 + // May Output: + // map[k1:v1 k2:v2] } func ExampleCache_MustKeys() { From 4e91039254965175ae803b0974cca9d3102c6285 Mon Sep 17 00:00:00 2001 From: 546104865 <564104865@qq.com> Date: Thu, 11 Nov 2021 23:37:01 +0800 Subject: [PATCH 10/10] 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 68a207472..8b783c153 100644 --- a/os/gcache/gcache_z_example_cache_test.go +++ b/os/gcache/gcache_z_example_cache_test.go @@ -540,7 +540,7 @@ func ExampleCache_MustGetOrSetFunc() { fmt.Println(v1) // Output: - // 111 + // v1 // }