Caching - Methods - 图1tip

The following is a list of commonly used methods. The documentation may lag behind new features in the code. For more methods and examples, please refer to the code documentation: https://pkg.go.dev/github.com/gogf/gf/v2/os/gcache

Set

  • Description: Use key-value pairs to set cache, and key-values can be of any type.
  • Signature:
  1. Set(ctx context.Context, key interface{}, value interface{}, duration time.Duration) error
  • Example: Set the slice to cache with the key name k1.
  1. func ExampleCache_Set() {
  2. c := gcache.New()
  3. c.Set(ctx, "k1", g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}, 0)
  4. fmt.Println(c.Get(ctx, "k1"))
  5. // Output:
  6. // [1,2,3,4,5,6,7,8,9] <nil>
  7. }

SetAdapter

  • Description: SetAdapter changes the underlying adapter of this cache object. Note that this setup function is not concurrency safe.
  • Signature:
  1. SetAdapter(adapter Adapter)
  • Example: You can implement any cache adapter according to your needs by implementing the interface method.
  1. func ExampleCache_SetAdapter() {
  2. c := gcache.New()
  3. adapter := gcache.New()
  4. c.SetAdapter(adapter)
  5. c.Set(ctx, "k1", g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}, 0)
  6. fmt.Println(c.Get(ctx, "k1"))
  7. // Output:
  8. // [1,2,3,4,5,6,7,8,9] <nil>
  9. }

SetIfNotExist

  • Description: Sets the corresponding key value value and returns true when the specified key does not exist. Otherwise, it does nothing and returns false.
  • Signature:
  1. SetIfNotExist(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (ok bool, err error)
  • Example: Directly judging and writing through SetIfNotExist, and set the expiration time.
  1. func ExampleCache_SetIfNotExist() {
  2. c := gcache.New()
  3. // Write when the key name does not exist, and set the expiration time to 1000 milliseconds
  4. k1, err := c.SetIfNotExist(ctx, "k1", "v1", 1000*time.Millisecond)
  5. fmt.Println(k1, err)
  6. // Returns false when the key name already exists
  7. k2, err := c.SetIfNotExist(ctx, "k1", "v2", 1000*time.Millisecond)
  8. fmt.Println(k2, err)
  9. // Print the current list of key values
  10. keys1, _ := c.Keys(ctx)
  11. fmt.Println(keys1)
  12. // It does not expire if `duration` == 0. It deletes the `key` if `duration` < 0 or given `value` is nil.
  13. c.SetIfNotExist(ctx, "k1", 0, -10000)
  14. // Wait 1 second for K1: V1 to expire automatically
  15. time.Sleep(1200 * time.Millisecond)
  16. // Print the current key-value pair again and find that K1: V1 has expired
  17. keys2, _ := c.Keys(ctx)
  18. fmt.Println(keys2)
  19. // Output:
  20. // true <nil>
  21. // false <nil>
  22. // [k1]
  23. // [<nil>]
  24. }

SetMap

  • Description: Set key-value pairs in batches, the input parameter type is map[interface{}]interface{}.
  • Signature:
  1. SetMap(ctx context.Context, data map[interface{}]interface{}, duration time.Duration) error
  • Example:
  1. func ExampleCache_SetMap() {
  2. c := gcache.New()
  3. // map[interface{}]interface{}
  4. data := g.MapAnyAny{
  5. "k1": "v1",
  6. "k2": "v2",
  7. "k3": "v3",
  8. }
  9. c.SetMap(ctx, data, 1000*time.Millisecond)
  10. // Gets the specified key value
  11. v1, _ := c.Get(ctx, "k1")
  12. v2, _ := c.Get(ctx, "k2")
  13. v3, _ := c.Get(ctx, "k3")
  14. fmt.Println(v1, v2, v3)
  15. // Output:
  16. // v1 v2 v3
  17. }

Size

  • Description: Size returns the number of items in the cache.
  • Signature:
  1. Size(ctx context.Context) (size int, err error)
  • Example:
  1. func ExampleCache_Size() {
  2. c := gcache.New()
  3. // Add 10 elements without expiration
  4. for i := 0; i < 10; i++ {
  5. c.Set(ctx, i, i, 0)
  6. }
  7. // Size returns the number of items in the cache.
  8. n, _ := c.Size(ctx)
  9. fmt.Println(n)
  10. // Output:
  11. // 10
  12. }

Update

  • Description: Update updates the value corresponding to the key without changing its expiration time, and returns the old value. If key does not exist in the cache, the returned exist value is false.
  • Signature:
  1. Update(ctx context.Context, key interface{}, value interface{}) (oldValue *gvar.Var, exist bool, err error)
  • Example: Add multiple caches through SetMap and modify value through Update with the specified key.
  1. func ExampleCache_Update() {
  2. c := gcache.New()
  3. c.SetMap(ctx, g.MapAnyAny{"k1": "v1", "k2": "v2", "k3": "v3"}, 0)
  4. k1, _ := c.Get(ctx, "k1")
  5. fmt.Println(k1)
  6. k2, _ := c.Get(ctx, "k2")
  7. fmt.Println(k2)
  8. k3, _ := c.Get(ctx, "k3")
  9. fmt.Println(k3)
  10. re, exist, _ := c.Update(ctx, "k1", "v11")
  11. fmt.Println(re, exist)
  12. re1, exist1, _ := c.Update(ctx, "k4", "v44")
  13. fmt.Println(re1, exist1)
  14. kup1, _ := c.Get(ctx, "k1")
  15. fmt.Println(kup1)
  16. kup2, _ := c.Get(ctx, "k2")
  17. fmt.Println(kup2)
  18. kup3, _ := c.Get(ctx, "k3")
  19. fmt.Println(kup3)
  20. // Output:
  21. // v1
  22. // v2
  23. // v3
  24. // v1 true
  25. // false
  26. // v11
  27. // v2
  28. // v3
  29. }

UpdateExpire

  • Description: UpdateExpire updates the expiration time of the key and returns the old expiration time value. If key does not exist in the cache, it returns -1.
  • Signature:
  1. UpdateExpire(ctx context.Context, key interface{}, duration time.Duration) (oldDuration time.Duration, err error)
  • Example: Update the expiration time of key through UpdateExpire and print to check.
  1. func ExampleCache_UpdateExpire() {
  2. c := gcache.New()
  3. c.Set(ctx, "k1", "v1", 1000*time.Millisecond)
  4. expire, _ := c.GetExpire(ctx, "k1")
  5. fmt.Println(expire)
  6. c.UpdateExpire(ctx, "k1", 500*time.Millisecond)
  7. expire1, _ := c.GetExpire(ctx, "k1")
  8. fmt.Println(expire1)
  9. // Output:
  10. // 1s
  11. // 500ms
  12. }

Values

  • Description: Get all values in the cache via Values, returned in slice form.
  • Signature:
  1. Values(ctx context.Context) (values []interface{}, err error)
  • Example:
  1. func ExampleCache_Values() {
  2. c := gcache.New()
  3. c.Set(ctx, "k1", g.Map{"k1": "v1", "k2": "v2"}, 0)
  4. // Values returns all values in the cache as slice.
  5. data, _ := c.Values(ctx)
  6. fmt.Println(data)
  7. // May Output:
  8. // [map[k1:v1 k2:v2]]
  9. }

Close

  • Description: Close the cache and let GC reclaim resources, by default no need to close.
  • Signature:
  1. Close(ctx context.Context) error
  • Example: Close the cache by Close.
  1. func ExampleCache_Close() {
  2. c := gcache.New()
  3. c.Set(ctx, "k1", "v", 0)
  4. data, _ := c.Get(ctx, "k1")
  5. fmt.Println(data)
  6. // Close closes the cache if necessary.
  7. c.Close(ctx)
  8. data1, _ := c.Get(ctx, "k1")
  9. fmt.Println(data1)
  10. // Output:
  11. // v
  12. // v
  13. }

Contains

  • Description: Contains returns true if the specified key exists in the cache, otherwise it returns false.
  • Signature:
  1. Contains(ctx context.Context, key interface{}) (bool, error)
  • Example:
  1. func ExampleCache_Contains() {
  2. c := gcache.New()
  3. // Set Cache
  4. c.Set(ctx, "k", "v", 0)
  5. data, _ := c.Contains(ctx, "k")
  6. fmt.Println(data)
  7. // return false
  8. data1, _ := c.Contains(ctx, "k1")
  9. fmt.Println(data1)
  10. // Output:
  11. // true
  12. // false
  13. }

Data

  • Description: Data is returned as a map type with a copy of all key-value pairs (‘key’:’value’) in the cache.
  • Signature:
  1. Data(ctx context.Context) (data map[interface{}]interface{}, err error)
  • Example: Get all cached data and return it as map[interface{}]interface{}
  1. func ExampleCache_Data() {
  2. c := gcache.New()
  3. c.Set(ctx, "k5", "v5", 0)
  4. data, _ := c.Data(ctx)
  5. fmt.Println(data)
  6. // Output:
  7. // map[k1:v1]
  8. }

Get

  • Description: Get retrieves and returns the associated value for a given key. If it doesn’t exist, the value is zero or has expired; it returns nil.
  • Signature:
  1. Get(ctx context.Context, key interface{}) (*gvar.Var, error)
  • Example:
  1. func ExampleCache_Get() {
  2. c := gcache.New()
  3. // Set Cache Object
  4. c.Set(ctx, "k1", "v1", 0)
  5. data, _ := c.Get(ctx, "k1")
  6. fmt.Println(data)
  7. // Output:
  8. // v1
  9. }

GetExpire

  • Description: GetExpire retrieves and returns the expiration time of key in the cache. Note that if key never expires, it returns 0. If key does not exist in the cache, it returns -1.
  • Signature:
  1. GetExpire(ctx context.Context, key interface{}) (time.Duration, error)
  • Example:
  1. func ExampleCache_GetExpire() {
  2. c := gcache.New()
  3. // Set cache without expiration
  4. c.Set(ctx, "k", "v", 10000*time.Millisecond)
  5. expire, _ := c.GetExpire(ctx, "k")
  6. fmt.Println(expire)
  7. // Output:
  8. // 10s
  9. }

GetOrSet

  • Description: Retrieve and return the value of key, or set the key-value pair directly if key does not exist in the cache.
  • Signature:
  1. GetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (result *gvar.Var, err error)
  • Example: Use GetOrSet to determine if key does not exist, then set it directly, and set the duration time.
  1. func ExampleCache_GetOrSet() {
  2. c := gcache.New()
  3. data, _ := c.GetOrSet(ctx, "k", "v", 10000*time.Millisecond)
  4. fmt.Println(data)
  5. data1, _ := c.Get(ctx, "k")
  6. fmt.Println(data1)
  7. // Output:
  8. // v
  9. // v
  10. }

GetOrSetFunc

  • Description: Retrieve and return the value of key; if the value corresponding to key does not exist, set key with the result of the function func. If key exists in the cache, return its result.
  • Signature:
  1. GetOrSetFunc(ctx context.Context, key interface{}, f func(ctx context.Context) (interface{}, error), duration time.Duration) (result *gvar.Var, err error)
  • Example: The setting of k1 returns the execution result of func, k2 returns nil and does not perform any operation.
  1. func ExampleCache_GetOrSetFunc() {
  2. c := gcache.New()
  3. c.GetOrSetFunc(ctx, "k1", func(ctx context.Context) (value interface{}, err error) {
  4. return "v1", nil
  5. }, 10000*time.Millisecond)
  6. v, _ := c.Get(ctx, "k1")
  7. fmt.Println(v)
  8. c.GetOrSetFunc(ctx, "k2", func(ctx context.Context) (value interface{}, err error) {
  9. return nil, nil
  10. }, 10000*time.Millisecond)
  11. v1, _ := c.Get(ctx, "k2")
  12. fmt.Println(v1)
  13. // Output:
  14. // v1
  15. }

GetOrSetFuncLock

  • Description: It is consistent with GetOrSetFunc, but cannot repeat or overwrite registration of the cache.
  • Signature:
  1. GetOrSetFuncLock(ctx context.Context, key interface{}, f func(ctx context.Context) (interface{}, error), duration time.Duration) (result *gvar.Var, err error)
  • Example: The first time setting returns the execution result of func, the operation for the second time setting is invalid.
  1. func ExampleCache_GetOrSetFuncLock() {
  2. c := gcache.New()
  3. c.GetOrSetFuncLock(ctx, "k1", func(ctx context.Context) (value interface{}, err error) {
  4. return "v1", nil
  5. }, 0)
  6. v, _ := c.Get(ctx, "k1")
  7. fmt.Println(v)
  8. c.GetOrSetFuncLock(ctx, "k1", func(ctx context.Context) (value interface{}, err error) {
  9. return "update v1", nil
  10. }, 0)
  11. v, _ = c.Get(ctx, "k1")
  12. fmt.Println(v)
  13. c.Remove(ctx, g.Slice{"k1"}...)
  14. // Output:
  15. // v1
  16. // v1
  17. }

Keys

  • Description: All keys in the cache are returned in the form of slice.
  • Signature:
  1. Keys(ctx context.Context) (keys []interface{}, err error)
  • Example:
  1. func ExampleCache_Keys() {
  2. c := gcache.New()
  3. c.SetMap(ctx, g.MapAnyAny{"k1": "v1"}, 0)
  4. // Print the current list of key values
  5. keys1, _ := c.Keys(ctx)
  6. fmt.Println(keys1)
  7. // Output:
  8. // [k1]
  9. }

KeyStrings

  • Description: KeyStrings returns all the keys in the cache as a string slice.
  • Signature:
  1. func (c *Cache) KeyStrings(ctx context.Context) ([]string, error) {
  2. keys, err := c.Keys(ctx)
  3. if err != nil {
  4. return nil, err
  5. }
  6. return gconv.Strings(keys), nil
  7. }
  • Example:
  1. func ExampleCache_KeyStrings() {
  2. c := gcache.New()
  3. c.SetMap(ctx, g.MapAnyAny{"k1": "v1", "k2": "v2"}, 0)
  4. // KeyStrings returns all keys in the cache as string slice.
  5. keys,_ := c.KeyStrings(ctx)
  6. fmt.Println(keys)
  7. // May Output:
  8. // [k1 k2]
  9. }

Remove

  • Description: Remove one or more keys from the cache, and return the value of the last deleted key.
  • Signature:
  1. Remove(ctx context.Context, keys ...interface{}) (lastValue *gvar.Var, err error)
  • Example:
  1. func ExampleCache_Remove() {
  2. c := gcache.New()
  3. c.SetMap(ctx, g.MapAnyAny{"k1": "v1", "k2": "v2"}, 0)
  4. c.Remove(ctx, "k1")
  5. data, _ := c.Data(ctx)
  6. fmt.Println(data)
  7. // Output:
  8. // map[k2:v2]
  9. }

Removes

  • Description: Remove multiple keys from the cache.
  • Signature:
  1. func (c *Cache) Removes(ctx context.Context, keys []interface{}) error {
  2. _, err := c.Remove(ctx, keys...)
  3. return err
  4. }
  • Example:
  1. func ExampleCache_Removes() {
  2. c := gcache.New()
  3. c.SetMap(ctx, g.MapAnyAny{"k1": "v1", "k2": "v2", "k3": "v3", "k4": "v4"}, 0)
  4. c.Removes(ctx, g.Slice{"k1", "k2", "k3"})
  5. data, _ := c.Data(ctx)
  6. fmt.Println(data)
  7. // Output:
  8. // map[k4:v4]
  9. }

Clear

  • Description: Clear all cache.
  • Signature:
  1. func (c *Cache) Clear(ctx context.Context) error
  • Example:
  1. func ExampleCache_Clear() {
  2. c := gcache.New()
  3. c.SetMap(ctx, g.MapAnyAny{"k1": "v1", "k2": "v2", "k3": "v3", "k4": "v4"}, 0)
  4. c.Clear(ctx)
  5. data, _ := c.Data(ctx)
  6. fmt.Println(data)
  7. // Output:
  8. // map[]
  9. }

MustGet

  • Description: MustGet retrieves and returns the associated value for a given key. If it doesn’t exist, the value is zero or has expired, it returns nil. If err is not nil, it will panic(err).
  • Signature:
  1. func (c *Cache) MustGet(ctx context.Context, key interface{}) *gvar.Var {
  2. v, err := c.Get(ctx, key)
  3. if err != nil {
  4. panic(err)
  5. }
  6. return v
  7. }
  • Example:
  1. func ExampleCache_MustGet() {
  2. c := gcache.New()
  3. c.Set(ctx, "k1", "v1", 0)
  4. k2 := c.MustGet(ctx, "k2")
  5. k1 := c.MustGet(ctx, "k1")
  6. fmt.Println(k1)
  7. fmt.Println(k2)
  8. // Output:
  9. // v1
  10. //
  11. }

MustGetOrSet

  • Description: Retrieve and return the value of key, or set the key-value pair directly if key does not exist in the cache. If err is not nil, it will panic(err).
  • Signature:
  1. func (c *Cache) MustGetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) *gvar.Var {
  2. v, err := c.GetOrSet(ctx, key, value, duration)
  3. if err != nil {
  4. panic(err)
  5. }
  6. return v
  7. }
  • Example:
  1. func ExampleCache_MustGetOrSet() {
  2. // Create a cache object,
  3. // Of course, you can also easily use the gcache package method directly
  4. c := gcache.New()
  5. // MustGetOrSet acts like GetOrSet, but it panics if any error occurs.
  6. k1 := c.MustGetOrSet(ctx, "k1", "v1", 0)
  7. fmt.Println(k1)
  8. k2 := c.MustGetOrSet(ctx, "k1", "v2", 0)
  9. fmt.Println(k2)
  10. // Output:
  11. // v1
  12. // v1
  13. }

MustGetOrSetFunc

  • Description: Retrieve and return the value of key; if the value corresponding to key does not exist, set key with the result of the function func. If key exists in the cache, return its result. If err is not nil, it will panic(err).
  • Signature:
  1. func (c *Cache) MustGetOrSetFunc(ctx context.Context, key interface{}, f func(ctx context.Context) (interface{}, error), duration time.Duration) *gvar.Var {
  2. v, err := c.GetOrSetFunc(ctx, key, f, duration)
  3. if err != nil {
  4. panic(err)
  5. }
  6. return v
  7. }
  • Example:
  1. func ExampleCache_MustGetOrSetFunc() {
  2. c := gcache.New()
  3. c.MustGetOrSetFunc(ctx, 1, func(ctx context.Context) (interface{}, error) {
  4. return 111, nil
  5. }, 10000*time.Millisecond)
  6. v := c.MustGet(ctx, 1)
  7. fmt.Println(v)
  8. c.MustGetOrSetFunc(ctx, 2, func(ctx context.Context) (interface{}, error) {
  9. return nil, nil
  10. }, 10000*time.Millisecond)
  11. v1 := c.MustGet(ctx, 2)
  12. fmt.Println(v1)
  13. // Output:
  14. // 111
  15. //
  16. }

MustGetOrSetFuncLock

  • Description: It is consistent with MustGetOrSetFunc, but cannot repeat or overwrite registration of the cache. If err is not nil, it will panic(err).
  • Signature:
  1. func (c *Cache) MustGetOrSetFuncLock(ctx context.Context, key interface{}, f func(ctx context.Context) (interface{}, error), duration time.Duration) *gvar.Var {
  2. v, err := c.GetOrSetFuncLock(ctx, key, f, duration)
  3. if err != nil {
  4. panic(err)
  5. }
  6. return v
  7. }
  • Example:
  1. func ExampleCache_MustGetOrSetFuncLock() {
  2. c := gcache.New()
  3. c.MustGetOrSetFuncLock(ctx, "k1", func(ctx context.Context) (interface{}, error) {
  4. return "v1", nil
  5. }, 0)
  6. v := c.MustGet(ctx, "k1")
  7. fmt.Println(v)
  8. // Modification failed
  9. c.MustGetOrSetFuncLock(ctx, "k1", func(ctx context.Context) (interface{}, error) {
  10. return "update v1", nil
  11. }, 0)
  12. v = c.MustGet(ctx, "k1")
  13. fmt.Println(v)
  14. // Output:
  15. // v1
  16. // v1
  17. }

MustContains

  • Description: Contains returns true if the specified key exists in the cache, otherwise it returns false. If err is not nil, it will panic(err).
  • Signature:
  1. func (c *Cache) MustContains(ctx context.Context, key interface{}) bool {
  2. v, err := c.Contains(ctx, key)
  3. if err != nil {
  4. panic(err)
  5. }
  6. return v
  7. }
  • Example:
  1. func ExampleCache_MustContains() {
  2. c := gcache.New()
  3. // Set Cache
  4. c.Set(ctx, "k", "v", 0)
  5. // Contains returns true if `key` exists in the cache, or else returns false.
  6. // return true
  7. data := c.MustContains(ctx, "k")
  8. fmt.Println(data)
  9. // return false
  10. data1 := c.MustContains(ctx, "k1")
  11. fmt.Println(data1)
  12. // Output:
  13. // true
  14. // false
  15. }

MustGetExpire

  • Description: MustGetExpire retrieves and returns the expiration time of key in the cache. Note that if key never expires, it returns 0. If key does not exist in the cache, it returns -1, if err is not nil, it will panic(err).
  • Signature:
  1. func (c *Cache) MustGetExpire(ctx context.Context, key interface{}) time.Duration {
  2. v, err := c.GetExpire(ctx, key)
  3. if err != nil {
  4. panic(err)
  5. }
  6. return v
  7. }
  • Example:
  1. func ExampleCache_MustGetExpire() {
  2. c := gcache.New()
  3. // Set cache without expiration
  4. c.Set(ctx, "k", "v", 10000*time.Millisecond)
  5. // MustGetExpire acts like GetExpire, but it panics if any error occurs.
  6. expire := c.MustGetExpire(ctx, "k")
  7. fmt.Println(expire)
  8. // May Output:
  9. // 10s
  10. }

MustSize

  • Description: MustSize returns the number of items in the cache. If err is not nil, it will panic(err).
  • Signature:
  1. func (c *Cache) MustSize(ctx context.Context) int {
  2. v, err := c.Size(ctx)
  3. if err != nil {
  4. panic(err)
  5. }
  6. return v
  7. }
  • Example:
  1. func ExampleCache_MustSize() {
  2. c := gcache.New()
  3. // Add 10 elements without expiration
  4. for i := 0; i < 10; i++ {
  5. c.Set(ctx, i, i, 0)
  6. }
  7. // Size returns the number of items in the cache.
  8. n := c.MustSize(ctx)
  9. fmt.Println(n)
  10. // Output:
  11. // 10
  12. }

MustData

  • Description: Data is returned as a map type with a copy of all key-value pairs (‘key’:’value’) in the cache. If err is not nil, it will panic(err).
  • Signature:
  1. func (c *Cache) MustData(ctx context.Context) map[interface{}]interface{} {
  2. v, err := c.Data(ctx)
  3. if err != nil {
  4. panic(err)
  5. }
  6. return v
  7. }
  • Example:
  1. func ExampleCache_MustData() {
  2. c := gcache.New()
  3. c.SetMap(ctx, g.MapAnyAny{"k1": "v1"}, 0)
  4. data := c.MustData(ctx)
  5. fmt.Println(data)
  6. // Set Cache
  7. c.Set(ctx, "k5", "v5", 0)
  8. data1, _ := c.Get(ctx, "k1")
  9. fmt.Println(data1)
  10. // Output:
  11. // map[k1:v1]
  12. // v1
  13. }

MustKeys

  • Description: MustKeys returns all keys in the cache in the form of (slice), if err is not nil, it will panic(err).
  • Signature:
  1. func (c *Cache) MustKeys(ctx context.Context) []interface{} {
  2. v, err := c.Keys(ctx)
  3. if err != nil {
  4. panic(err)
  5. }
  6. return v
  7. }
  • Example:
  1. func ExampleCache_MustKeys() {
  2. c := gcache.New()
  3. c.SetMap(ctx, g.MapAnyAny{"k1": "v1", "k2": "v2"}, 0)
  4. // MustKeys acts like Keys, but it panics if any error occurs.
  5. keys1 := c.MustKeys(ctx)
  6. fmt.Println(keys1)
  7. // May Output:
  8. // [k1 k2]
  9. }

MustKeyStrings

  • Description: MustKeyStrings returns all the keys in the cache as a string (slice). If err is not nil, it will panic(err).
  • Signature:
  1. func (c *Cache) MustKeyStrings(ctx context.Context) []string {
  2. v, err := c.KeyStrings(ctx)
  3. if err != nil {
  4. panic(err)
  5. }
  6. return v
  7. }
  • Example:
  1. func ExampleCache_MustKeyStrings() {
  2. c := gcache.New()
  3. c.SetMap(ctx, g.MapAnyAny{"k1": "v1", "k2": "v2"}, 0)
  4. // MustKeyStrings returns all keys in the cache as string slice.
  5. // MustKeyStrings acts like KeyStrings, but it panics if any error occurs.
  6. keys := c.MustKeyStrings(ctx)
  7. fmt.Println(keys)
  8. // May Output:
  9. // [k1 k2]
  10. }

MustValues

  • Description: MustValues returns all values in the cache in the form of (slice). If err is not nil, it will panic(err).
  • Signature:
  1. func (c *Cache) MustValues(ctx context.Context) []interface{} {
  2. v, err := c.Values(ctx)
  3. if err != nil {
  4. panic(err)
  5. }
  6. return v
  7. }
  • Example:
  1. func ExampleCache_MustValues() {
  2. c := gcache.New()
  3. // Write value
  4. c.Set(ctx, "k1", "v1", 0)
  5. // Values returns all values in the cache as slice.
  6. data := c.MustValues(ctx)
  7. fmt.Println(data)
  8. // Output:
  9. // [v1]
  10. }