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