github.com/zhongdalu/gf@v1.0.0/g/os/gcache/gcache.go (about)

     1  // Copyright 2017-2018 gf Author(https://github.com/zhongdalu/gf). 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/zhongdalu/gf.
     6  
     7  // Package gcache provides high performance and concurrent-safe in-memory cache for process.
     8  package gcache
     9  
    10  // Default cache object.
    11  var cache = New()
    12  
    13  // Set sets cache with <key>-<value> pair, which is expired after <duration>.
    14  //
    15  // The parameter <duration> can be either type of int or time.Duration.
    16  // If <duration> is type of int, it means <duration> milliseconds.
    17  // If <duration> <=0 means it does not expire.
    18  func Set(key interface{}, value interface{}, duration interface{}) {
    19  	cache.Set(key, value, duration)
    20  }
    21  
    22  // SetIfNotExist sets cache with <key>-<value> pair if <key> does not exist in the cache,
    23  // which is expired after <duration>.
    24  //
    25  // The parameter <duration> can be either type of int or time.Duration.
    26  // If <duration> is type of int, it means <duration> milliseconds.
    27  // If <duration> <=0 means it does not expire.
    28  func SetIfNotExist(key interface{}, value interface{}, duration interface{}) bool {
    29  	return cache.SetIfNotExist(key, value, duration)
    30  }
    31  
    32  // Sets batch sets cache with key-value pairs by <data>, which is expired after <duration>.
    33  //
    34  // The parameter <duration> can be either type of int or time.Duration.
    35  // If <duration> is type of int, it means <duration> milliseconds.
    36  // If <duration> <=0 means it does not expire.
    37  func Sets(data map[interface{}]interface{}, duration interface{}) {
    38  	cache.Sets(data, duration)
    39  }
    40  
    41  // Get returns the value of <key>.
    42  // It returns nil if it does not exist or its value is nil.
    43  func Get(key interface{}) interface{} {
    44  	return cache.Get(key)
    45  }
    46  
    47  // GetOrSet returns the value of <key>,
    48  // or sets <key>-<value> pair and returns <value> if <key> does not exist in the cache.
    49  // The key-value pair expires after <duration>.
    50  //
    51  // The parameter <duration> can be either type of int or time.Duration.
    52  // If <duration> is type of int, it means <duration> milliseconds.
    53  // If <duration> <=0 means it does not expire.
    54  func GetOrSet(key interface{}, value interface{}, duration interface{}) interface{} {
    55  	return cache.GetOrSet(key, value, duration)
    56  }
    57  
    58  // GetOrSetFunc returns the value of <key>,
    59  // or sets <key> with result of function <f> and returns its result
    60  // if <key> does not exist in the cache.
    61  // The key-value pair expires after <duration>.
    62  //
    63  // The parameter <duration> can be either type of int or time.Duration.
    64  // If <duration> is type of int, it means <duration> milliseconds.
    65  // If <duration> <=0 means it does not expire.
    66  func GetOrSetFunc(key interface{}, f func() interface{}, duration interface{}) interface{} {
    67  	return cache.GetOrSetFunc(key, f, duration)
    68  }
    69  
    70  // GetOrSetFuncLock returns the value of <key>,
    71  // or sets <key> with result of function <f> and returns its result
    72  // if <key> does not exist in the cache.
    73  // The key-value pair expires after <duration>.
    74  //
    75  // The parameter <duration> can be either type of int or time.Duration.
    76  // If <duration> is type of int, it means <duration> milliseconds.
    77  // If <duration> <=0 means it does not expire.
    78  //
    79  // Note that the function <f> is executed within writing mutex lock.
    80  func GetOrSetFuncLock(key interface{}, f func() interface{}, duration interface{}) interface{} {
    81  	return cache.GetOrSetFuncLock(key, f, duration)
    82  }
    83  
    84  // Contains returns true if <key> exists in the cache, or else returns false.
    85  func Contains(key interface{}) bool {
    86  	return cache.Contains(key)
    87  }
    88  
    89  // Remove deletes the <key> in the cache, and returns its value.
    90  func Remove(key interface{}) interface{} {
    91  	return cache.Remove(key)
    92  }
    93  
    94  // Removes deletes <keys> in the cache.
    95  func Removes(keys []interface{}) {
    96  	cache.Removes(keys)
    97  }
    98  
    99  // Data returns a copy of all key-value pairs in the cache as map type.
   100  func Data() map[interface{}]interface{} {
   101  	return cache.Data()
   102  }
   103  
   104  // Keys returns all keys in the cache as slice.
   105  func Keys() []interface{} {
   106  	return cache.Keys()
   107  }
   108  
   109  // KeyStrings returns all keys in the cache as string slice.
   110  func KeyStrings() []string {
   111  	return cache.KeyStrings()
   112  }
   113  
   114  // Values returns all values in the cache as slice.
   115  func Values() []interface{} {
   116  	return cache.Values()
   117  }
   118  
   119  // Size returns the size of the cache.
   120  func Size() int {
   121  	return cache.Size()
   122  }