Files
gf/os/gcache/gcache_adapter.go

119 lines
5.4 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2020-09-26 20:47:29 +08:00
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gcache
import (
2021-01-26 14:11:36 +08:00
"context"
2020-09-26 20:47:29 +08:00
"time"
)
// Adapter is the adapter for cache features implements.
type Adapter interface {
// Set sets cache with <key>-<value> pair, which is expired after <duration>.
//
// It does not expire if <duration> == 0.
// It deletes the <key> if <duration> < 0.
2021-01-26 14:11:36 +08:00
Set(ctx context.Context, key interface{}, value interface{}, duration time.Duration) error
2020-09-26 20:47:29 +08:00
// Sets batch sets cache with key-value pairs by <data>, which is expired after <duration>.
//
// It does not expire if <duration> == 0.
// It deletes the keys of <data> if <duration> < 0 or given <value> is nil.
2021-01-26 14:11:36 +08:00
Sets(ctx context.Context, data map[interface{}]interface{}, duration time.Duration) error
2020-09-26 20:47:29 +08:00
// SetIfNotExist sets cache with <key>-<value> pair which is expired after <duration>
// if <key> does not exist in the cache. It returns true the <key> dose not exist in the
// cache and it sets <value> successfully to the cache, or else it returns false.
//
2020-09-26 20:47:29 +08:00
// The parameter <value> can be type of <func() interface{}>, but it dose nothing if its
// result is nil.
//
// It does not expire if <duration> == 0.
// It deletes the <key> if <duration> < 0 or given <value> is nil.
2021-01-26 14:11:36 +08:00
SetIfNotExist(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (bool, error)
2020-09-26 20:47:29 +08:00
// Get retrieves and returns the associated value of given <key>.
2021-01-26 13:33:24 +08:00
// It returns nil if it does not exist, its value is nil or it's expired.
2021-01-26 14:11:36 +08:00
Get(ctx context.Context, key interface{}) (interface{}, error)
2020-09-26 20:47:29 +08:00
// GetOrSet retrieves and returns the value of <key>, or sets <key>-<value> pair and
// returns <value> if <key> does not exist in the cache. The key-value pair expires
// after <duration>.
//
// It does not expire if <duration> == 0.
// It deletes the <key> if <duration> < 0 or given <value> is nil, but it does nothing
// if <value> is a function and the function result is nil.
2021-01-26 14:11:36 +08:00
GetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (interface{}, error)
2020-09-26 20:47:29 +08:00
// GetOrSetFunc retrieves and returns the value of <key>, or sets <key> with result of
// function <f> and returns its result if <key> does not exist in the cache. The key-value
// pair expires after <duration>.
//
// It does not expire if <duration> == 0.
// It deletes the <key> if <duration> < 0 or given <value> is nil, but it does nothing
// if <value> is a function and the function result is nil.
2021-01-26 14:11:36 +08:00
GetOrSetFunc(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error)
2020-09-26 20:47:29 +08:00
// GetOrSetFuncLock retrieves and returns the value of <key>, or sets <key> with result of
// function <f> and returns its result if <key> does not exist in the cache. The key-value
// pair expires after <duration>.
//
// It does not expire if <duration> == 0.
// It does nothing if function <f> returns nil.
//
// Note that the function <f> should be executed within writing mutex lock for concurrent
// safety purpose.
2021-01-26 14:11:36 +08:00
GetOrSetFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error)
// Contains returns true if <key> exists in the cache, or else returns false.
2021-01-26 14:11:36 +08:00
Contains(ctx context.Context, key interface{}) (bool, error)
2020-09-26 20:47:29 +08:00
2020-09-26 21:00:28 +08:00
// GetExpire retrieves and returns the expiration of <key> in the cache.
//
// It returns 0 if the <key> does not expire.
2020-09-26 21:00:28 +08:00
// It returns -1 if the <key> does not exist in the cache.
2021-01-26 14:11:36 +08:00
GetExpire(ctx context.Context, key interface{}) (time.Duration, error)
2020-09-26 21:00:28 +08:00
2020-09-26 20:47:29 +08:00
// Remove deletes one or more keys from cache, and returns its value.
// If multiple keys are given, it returns the value of the last deleted item.
2021-01-26 14:11:36 +08:00
Remove(ctx context.Context, keys ...interface{}) (value interface{}, err error)
2020-09-26 20:47:29 +08:00
// Update updates the value of <key> without changing its expiration and returns the old value.
// The returned value <exist> is false if the <key> does not exist in the cache.
2020-09-26 21:13:09 +08:00
//
// It deletes the <key> if given <value> is nil.
// It does nothing if <key> does not exist in the cache.
2021-01-26 14:11:36 +08:00
Update(ctx context.Context, key interface{}, value interface{}) (oldValue interface{}, exist bool, err error)
2020-09-26 20:47:29 +08:00
// UpdateExpire updates the expiration of <key> and returns the old expiration duration value.
2020-09-26 21:13:09 +08:00
//
// It returns -1 and does nothing if the <key> does not exist in the cache.
2020-09-26 21:13:09 +08:00
// It deletes the <key> if <duration> < 0.
2021-01-26 14:11:36 +08:00
UpdateExpire(ctx context.Context, key interface{}, duration time.Duration) (oldDuration time.Duration, err error)
2020-09-26 20:47:29 +08:00
// Size returns the number of items in the cache.
2021-01-26 14:11:36 +08:00
Size(ctx context.Context) (size int, err error)
2020-09-26 20:47:29 +08:00
// Data returns a copy of all key-value pairs in the cache as map type.
// Note that this function may leads lots of memory usage, you can implement this function
// if necessary.
2021-01-26 14:11:36 +08:00
Data(ctx context.Context) (map[interface{}]interface{}, error)
2020-09-26 20:47:29 +08:00
// Keys returns all keys in the cache as slice.
2021-01-26 14:11:36 +08:00
Keys(ctx context.Context) ([]interface{}, error)
2020-09-26 20:47:29 +08:00
// Values returns all values in the cache as slice.
2021-01-26 14:11:36 +08:00
Values(ctx context.Context) ([]interface{}, error)
2020-09-26 20:47:29 +08:00
// Clear clears all data of the cache.
// Note that this function is sensitive and should be carefully used.
2021-01-26 14:11:36 +08:00
Clear(ctx context.Context) error
2020-09-26 20:47:29 +08:00
// Close closes the cache if necessary.
2021-01-26 14:11:36 +08:00
Close(ctx context.Context) error
2020-09-26 20:47:29 +08:00
}