-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
gcache.go
122 lines (105 loc) · 4.26 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
// Copyright 2017-2018 gf Author(https://github.com/gogf/gf). 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/gogf/gf.
// Package gcache provides high performance and concurrent-safe in-memory cache for process.
package gcache
// Default cache object.
var cache = New()
// Set sets cache with <key>-<value> pair, which is expired after <duration>.
//
// The parameter <duration> can be either type of int or time.Duration.
// If <duration> is type of int, it means <duration> milliseconds.
// If <duration> <=0 means it does not expire.
func Set(key interface{}, value interface{}, duration interface{}) {
cache.Set(key, value, duration)
}
// SetIfNotExist sets cache with <key>-<value> pair if <key> does not exist in the cache,
// which is expired after <duration>.
//
// The parameter <duration> can be either type of int or time.Duration.
// If <duration> is type of int, it means <duration> milliseconds.
// If <duration> <=0 means it does not expire.
func SetIfNotExist(key interface{}, value interface{}, duration interface{}) bool {
return cache.SetIfNotExist(key, value, duration)
}
// Sets batch sets cache with key-value pairs by <data>, which is expired after <duration>.
//
// The parameter <duration> can be either type of int or time.Duration.
// If <duration> is type of int, it means <duration> milliseconds.
// If <duration> <=0 means it does not expire.
func Sets(data map[interface{}]interface{}, duration interface{}) {
cache.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{} {
return cache.Get(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>.
//
// The parameter <duration> can be either type of int or time.Duration.
// If <duration> is type of int, it means <duration> milliseconds.
// If <duration> <=0 means it does not expire.
func GetOrSet(key interface{}, value interface{}, duration interface{}) interface{} {
return cache.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>.
//
// The parameter <duration> can be either type of int or time.Duration.
// If <duration> is type of int, it means <duration> milliseconds.
// If <duration> <=0 means it does not expire.
func GetOrSetFunc(key interface{}, f func() interface{}, duration interface{}) interface{} {
return cache.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>.
//
// The parameter <duration> can be either type of int or time.Duration.
// If <duration> is type of int, it means <duration> milliseconds.
// If <duration> <=0 means it does not expire.
//
// Note that the function <f> is executed within writing mutex lock.
func GetOrSetFuncLock(key interface{}, f func() interface{}, duration interface{}) interface{} {
return cache.GetOrSetFuncLock(key, f, duration)
}
// Contains returns true if <key> exists in the cache, or else returns false.
func Contains(key interface{}) bool {
return cache.Contains(key)
}
// Remove deletes the <key> in the cache, and returns its value.
func Remove(key interface{}) interface{} {
return cache.Remove(key)
}
// Removes deletes <keys> in the cache.
func Removes(keys []interface{}) {
cache.Removes(keys)
}
// Data returns a copy of all key-value pairs in the cache as map type.
func Data() map[interface{}]interface{} {
return cache.Data()
}
// Keys returns all keys in the cache as slice.
func Keys() []interface{} {
return cache.Keys()
}
// KeyStrings returns all keys in the cache as string slice.
func KeyStrings() []string {
return cache.KeyStrings()
}
// Values returns all values in the cache as slice.
func Values() []interface{} {
return cache.Values()
}
// Size returns the size of the cache.
func Size() int {
return cache.Size()
}