github.com/gogf/gf@v1.16.9/os/gcache/gcache_adapter.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
     8  
     9  import (
    10  	"context"
    11  	"time"
    12  )
    13  
    14  // Adapter is the core adapter for cache features implements.
    15  type Adapter interface {
    16  	// Set sets cache with `key`-`value` pair, which is expired after `duration`.
    17  	//
    18  	// It does not expire if `duration` == 0.
    19  	// It deletes the `key` if `duration` < 0.
    20  	Set(ctx context.Context, key interface{}, value interface{}, duration time.Duration) error
    21  
    22  	// Sets batch sets cache with key-value pairs by `data`, which is expired after `duration`.
    23  	//
    24  	// It does not expire if `duration` == 0.
    25  	// It deletes the keys of `data` if `duration` < 0 or given `value` is nil.
    26  	Sets(ctx context.Context, data map[interface{}]interface{}, duration time.Duration) error
    27  
    28  	// SetIfNotExist sets cache with `key`-`value` pair which is expired after `duration`
    29  	// if `key` does not exist in the cache. It returns true the `key` does not exist in the
    30  	// cache, and it sets `value` successfully to the cache, or else it returns false.
    31  	//
    32  	// The parameter `value` can be type of `func() interface{}`, but it does nothing if its
    33  	// result is nil.
    34  	//
    35  	// It does not expire if `duration` == 0.
    36  	// It deletes the `key` if `duration` < 0 or given `value` is nil.
    37  	SetIfNotExist(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (bool, error)
    38  
    39  	// Get retrieves and returns the associated value of given `key`.
    40  	// It returns nil if it does not exist, its value is nil, or it's expired.
    41  	Get(ctx context.Context, key interface{}) (interface{}, error)
    42  
    43  	// GetOrSet retrieves and returns the value of `key`, or sets `key`-`value` pair and
    44  	// returns `value` if `key` does not exist in the cache. The key-value pair expires
    45  	// after `duration`.
    46  	//
    47  	// It does not expire if `duration` == 0.
    48  	// It deletes the `key` if `duration` < 0 or given `value` is nil, but it does nothing
    49  	// if `value` is a function and the function result is nil.
    50  	GetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (interface{}, error)
    51  
    52  	// GetOrSetFunc retrieves and returns the value of `key`, or sets `key` with result of
    53  	// function `f` and returns its result if `key` does not exist in the cache. The key-value
    54  	// pair expires after `duration`.
    55  	//
    56  	// It does not expire if `duration` == 0.
    57  	// It deletes the `key` if `duration` < 0 or given `value` is nil, but it does nothing
    58  	// if `value` is a function and the function result is nil.
    59  	GetOrSetFunc(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error)
    60  
    61  	// GetOrSetFuncLock retrieves and returns the value of `key`, or sets `key` with result of
    62  	// function `f` and returns its result if `key` does not exist in the cache. The key-value
    63  	// pair expires after `duration`.
    64  	//
    65  	// It does not expire if `duration` == 0.
    66  	// It does nothing if function `f` returns nil.
    67  	//
    68  	// Note that the function `f` should be executed within writing mutex lock for concurrent
    69  	// safety purpose.
    70  	GetOrSetFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error)
    71  
    72  	// Contains returns true if `key` exists in the cache, or else returns false.
    73  	Contains(ctx context.Context, key interface{}) (bool, error)
    74  
    75  	// GetExpire retrieves and returns the expiration of `key` in the cache.
    76  	//
    77  	// It returns 0 if the `key` does not expire.
    78  	// It returns -1 if the `key` does not exist in the cache.
    79  	GetExpire(ctx context.Context, key interface{}) (time.Duration, error)
    80  
    81  	// Remove deletes one or more keys from cache, and returns its value.
    82  	// If multiple keys are given, it returns the value of the last deleted item.
    83  	Remove(ctx context.Context, keys ...interface{}) (value interface{}, err error)
    84  
    85  	// Update updates the value of `key` without changing its expiration and returns the old value.
    86  	// The returned value `exist` is false if the `key` does not exist in the cache.
    87  	//
    88  	// It deletes the `key` if given `value` is nil.
    89  	// It does nothing if `key` does not exist in the cache.
    90  	Update(ctx context.Context, key interface{}, value interface{}) (oldValue interface{}, exist bool, err error)
    91  
    92  	// UpdateExpire updates the expiration of `key` and returns the old expiration duration value.
    93  	//
    94  	// It returns -1 and does nothing if the `key` does not exist in the cache.
    95  	// It deletes the `key` if `duration` < 0.
    96  	UpdateExpire(ctx context.Context, key interface{}, duration time.Duration) (oldDuration time.Duration, err error)
    97  
    98  	// Size returns the number of items in the cache.
    99  	Size(ctx context.Context) (size int, err error)
   100  
   101  	// Data returns a copy of all key-value pairs in the cache as map type.
   102  	// Note that this function may lead lots of memory usage, you can implement this function
   103  	// if necessary.
   104  	Data(ctx context.Context) (map[interface{}]interface{}, error)
   105  
   106  	// Keys returns all keys in the cache as slice.
   107  	Keys(ctx context.Context) ([]interface{}, error)
   108  
   109  	// Values returns all values in the cache as slice.
   110  	Values(ctx context.Context) ([]interface{}, error)
   111  
   112  	// Clear clears all data of the cache.
   113  	// Note that this function is sensitive and should be carefully used.
   114  	Clear(ctx context.Context) error
   115  
   116  	// Close closes the cache if necessary.
   117  	Close(ctx context.Context) error
   118  }