Memory cache usage

Overview

This section mainly describes the use of cache.

Preparing

  1. Complete golang installation

Create

  1. NewCache
  1. Function signature:
  2. NewCache func(expire time.Duration, opts ...CacheOption) (*Cache, error)
  3. description:
  4. create cache objects.
  5. Attention:
  6. 1. expire: expiration time
  7. 2. opts: Action option
  8. 2.1 WithLimit: Set maximum cache data
  9. 2.2 WithName: Setup cache name, output log printing
  10. return value:
  11. 1. *Cache: cache object
  12. 2. error: create result

Methodological description

  1. Set
  1. Function signature:
  2. Set func(key string, value interface{})
  3. description:
  4. add value to cache.
  5. Attention:
  6. 1. key: key
  7. 2. value: value
  8. Example:
  9. cache, err := NewCache(time.Second*2, WithName("any"))
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. cache.Set("first", "first element")
  1. SetWithExpire
  1. Function signature:
  2. SetWithExpire func(key string, value interface{}, expire time.Duration)cache, err := NewCache(time.Second*2, WithName("any"))
  3. if err != nil {
  4. log.Fatal(err)
  5. }
  6. cache.SetWithExpire("first", "first element", time.Second)
  7. description:
  8. add value to cache and specify expiration time
  9. to participate:
  10. 1. key: key
  11. 2. value: value
  12. 3. expire: Expiration Time
  13. Example:
  14. cache, err := NewCache(time.Second*2, WithName("any"))
  15. if err != nil {
  16. log.Fatal(err)
  17. }
  18. cache.SetWithExpire("first", "first element", time.Second)
  1. Get
  1. Function signature:
  2. Get func(key string) (interface{}, bool)
  3. description:
  4. query cache
  5. join:
  6. key: key
  7. returned value:
  8. 1. interface{}: value
  9. 2. bool: exists
  10. Example:
  11. cache, err := NewCache(time.Second*2, WithName("any"))
  12. if err != nil {
  13. log.Fatal(err)
  14. }
  15. cache.Set("first", "first element")
  16. v, exist := cache.Get("first")
  17. if !exist {
  18. // deal with not exist
  19. }
  20. value, ok := v.(string)
  21. if !ok {
  22. // deal with type error
  23. }
  24. // use value
  1. Del
  1. Function signature:
  2. Del func(key string)
  3. description:
  4. Delete cache.
  5. Attention:
  6. 1. key: key
  7. 2. value: value
  8. Example:
  9. cache, err := NewCache(time.Second*2, WithName("any"))
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. cache.Del("first")
  1. Take
  1. Function signature:
  2. Take funcTake(key string, fetch func() (interface{}, error)) (interface{}, error)
  3. description:
  4. Fetch function returns the result if cache exists and if cache does not exist.
  5. Attention:
  6. 1. key: key
  7. 2. fetch: Custom return result
  8. Example:
  9. cache, err := NewCache(time.Second*2, WithName("any"))
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. v, err := cache.Take("first", func() (interface{}, error) {
  14. return "first element", nil
  15. })
  16. println(v) // output: first element
  17. cache.Set("first", "first element 2")
  18. v, err = cache.Take("first", func() (interface{}, error) {
  19. return "first element", nil
  20. })
  21. println(v) // // output: first element 2