github.com/gogf/gf@v1.16.9/os/gcache/gcache.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  // Package gcache provides kinds of cache management for process.
     8  // It provides a concurrent-safe in-memory cache adapter for process in default.
     9  package gcache
    10  
    11  import (
    12  	"context"
    13  	"github.com/gogf/gf/container/gvar"
    14  	"time"
    15  )
    16  
    17  // Default cache object.
    18  var defaultCache = New()
    19  
    20  // Ctx is a chaining function, which shallowly clones current object and sets the context
    21  // for next operation.
    22  func Ctx(ctx context.Context) *Cache {
    23  	return defaultCache.Ctx(ctx)
    24  }
    25  
    26  // Set sets cache with `key`-`value` pair, which is expired after `duration`.
    27  // It does not expire if `duration` == 0.
    28  func Set(key interface{}, value interface{}, duration time.Duration) error {
    29  	return defaultCache.Set(key, value, duration)
    30  }
    31  
    32  // SetIfNotExist sets cache with `key`-`value` pair if `key` does not exist in the cache,
    33  // which is expired after `duration`. It does not expire if `duration` == 0.
    34  func SetIfNotExist(key interface{}, value interface{}, duration time.Duration) (bool, error) {
    35  	return defaultCache.SetIfNotExist(key, value, duration)
    36  }
    37  
    38  // Sets batch sets cache with key-value pairs by `data`, which is expired after `duration`.
    39  //
    40  // It does not expire if `duration` == 0.
    41  func Sets(data map[interface{}]interface{}, duration time.Duration) error {
    42  	return defaultCache.Sets(data, duration)
    43  }
    44  
    45  // Get returns the value of `key`.
    46  // It returns nil if it does not exist or its value is nil.
    47  func Get(key interface{}) (interface{}, error) {
    48  	return defaultCache.Get(key)
    49  }
    50  
    51  // GetVar retrieves and returns the value of `key` as gvar.Var.
    52  func GetVar(key interface{}) (*gvar.Var, error) {
    53  	return defaultCache.GetVar(key)
    54  }
    55  
    56  // GetOrSet returns the value of `key`,
    57  // or sets `key`-`value` pair and returns `value` if `key` does not exist in the cache.
    58  // The key-value pair expires after `duration`.
    59  //
    60  // It does not expire if `duration` == 0.
    61  func GetOrSet(key interface{}, value interface{}, duration time.Duration) (interface{}, error) {
    62  	return defaultCache.GetOrSet(key, value, duration)
    63  }
    64  
    65  // GetOrSetFunc returns the value of `key`, or sets `key` with result of function `f`
    66  // and returns its result if `key` does not exist in the cache. The key-value pair expires
    67  // after `duration`. It does not expire if `duration` == 0.
    68  func GetOrSetFunc(key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error) {
    69  	return defaultCache.GetOrSetFunc(key, f, duration)
    70  }
    71  
    72  // GetOrSetFuncLock returns the value of `key`, or sets `key` with result of function `f`
    73  // and returns its result if `key` does not exist in the cache. The key-value pair expires
    74  // after `duration`. It does not expire if `duration` == 0.
    75  //
    76  // Note that the function `f` is executed within writing mutex lock.
    77  func GetOrSetFuncLock(key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error) {
    78  	return defaultCache.GetOrSetFuncLock(key, f, duration)
    79  }
    80  
    81  // Contains returns true if `key` exists in the cache, or else returns false.
    82  func Contains(key interface{}) (bool, error) {
    83  	return defaultCache.Contains(key)
    84  }
    85  
    86  // Remove deletes the one or more keys from cache, and returns its value.
    87  // If multiple keys are given, it returns the value of the deleted last item.
    88  func Remove(keys ...interface{}) (value interface{}, err error) {
    89  	return defaultCache.Remove(keys...)
    90  }
    91  
    92  // Removes deletes `keys` in the cache.
    93  // Deprecated, use Remove instead.
    94  func Removes(keys []interface{}) {
    95  	defaultCache.Remove(keys...)
    96  }
    97  
    98  // Data returns a copy of all key-value pairs in the cache as map type.
    99  func Data() (map[interface{}]interface{}, error) {
   100  	return defaultCache.Data()
   101  }
   102  
   103  // Keys returns all keys in the cache as slice.
   104  func Keys() ([]interface{}, error) {
   105  	return defaultCache.Keys()
   106  }
   107  
   108  // KeyStrings returns all keys in the cache as string slice.
   109  func KeyStrings() ([]string, error) {
   110  	return defaultCache.KeyStrings()
   111  }
   112  
   113  // Values returns all values in the cache as slice.
   114  func Values() ([]interface{}, error) {
   115  	return defaultCache.Values()
   116  }
   117  
   118  // Size returns the size of the cache.
   119  func Size() (int, error) {
   120  	return defaultCache.Size()
   121  }
   122  
   123  // GetExpire retrieves and returns the expiration of `key`.
   124  // It returns -1 if the `key` does not exist in the cache.
   125  func GetExpire(key interface{}) (time.Duration, error) {
   126  	return defaultCache.GetExpire(key)
   127  }
   128  
   129  // Update updates the value of `key` without changing its expiration and returns the old value.
   130  // The returned `exist` value is false if the `key` does not exist in the cache.
   131  func Update(key interface{}, value interface{}) (oldValue interface{}, exist bool, err error) {
   132  	return defaultCache.Update(key, value)
   133  }
   134  
   135  // UpdateExpire updates the expiration of `key` and returns the old expiration duration value.
   136  // It returns -1 if the `key` does not exist in the cache.
   137  func UpdateExpire(key interface{}, duration time.Duration) (oldDuration time.Duration, err error) {
   138  	return defaultCache.UpdateExpire(key, duration)
   139  }