-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcache.go
195 lines (165 loc) · 4.32 KB
/
cache.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Package cache is a generic cache use and cache manager for golang.
// FileCache is a simple local file system cache implement.
// MemoryCache is a simple memory cache implement.
package cache
import (
"time"
"github.com/gookit/gsr"
)
// Cache interface definition
type Cache = gsr.SimpleCacher
// some generic expire time define.
const (
// Forever Always exist
Forever = 0
// Seconds1 1 second
Seconds1 = time.Second
// Seconds2 2 second
Seconds2 = 2 * time.Second
// Seconds3 3 second
Seconds3 = 3 * time.Second
// Seconds5 5 second
Seconds5 = 5 * time.Second
// Seconds6 6 second
Seconds6 = 6 * time.Second
// Seconds7 7 second
Seconds7 = 7 * time.Second
// Seconds8 8 second
Seconds8 = 8 * time.Second
// Seconds9 9 second
Seconds9 = 9 * time.Second
// Seconds10 10 second
Seconds10 = 10 * time.Second
// Seconds15 15 second
Seconds15 = 15 * time.Second
// Seconds20 20 second
Seconds20 = 20 * time.Second
// Seconds30 30 second
Seconds30 = 30 * time.Second
// OneMinutes 1 minutes
OneMinutes = 60 * time.Second
// TwoMinutes 2 minutes
TwoMinutes = 120 * time.Second
// ThreeMinutes 3 minutes
ThreeMinutes = 180 * time.Second
// FiveMinutes 5 minutes
FiveMinutes = 300 * time.Second
// TenMinutes 10 minutes
TenMinutes = 600 * time.Second
// FifteenMinutes 15 minutes
FifteenMinutes = 900 * time.Second
// HalfHour half an hour
HalfHour = 1800 * time.Second
// OneHour 1 hour
OneHour = 3600 * time.Second
// TwoHour 2 hours
TwoHour = 7200 * time.Second
// ThreeHour 3 hours
ThreeHour = 10800 * time.Second
// HalfDay 12 hours(half of the day)
HalfDay = 43200 * time.Second
// OneDay 24 hours(1 day)
OneDay = 86400 * time.Second
// TwoDay 2 day
TwoDay = 172800 * time.Second
// ThreeDay 3 day
ThreeDay = 259200 * time.Second
// OneWeek 7 day(one week)
OneWeek = 604800 * time.Second
)
/*************************************************************
* config default cache manager
*************************************************************/
// default cache driver manager instance
var std = NewManager()
// Register driver to manager instance
func Register(name string, driver Cache) *Manager {
std.Register(name, driver)
return std
}
// Unregister an cache driver
func Unregister(name string) int {
return std.Unregister(name)
}
// UnregisterAll cache drivers
func UnregisterAll(fn ...func(cache Cache)) int {
return std.UnregisterAll(fn...)
}
// SetDefName set default driver name.
// Deprecated
//
// please use DefaultUse() instead it
func SetDefName(driverName string) {
std.DefaultUse(driverName)
}
// DefaultUse set default driver name
func DefaultUse(driverName string) {
std.DefaultUse(driverName)
}
// Use driver object by name and set it as default driver.
func Use(driverName string) Cache {
return std.Use(driverName)
}
// GetCache returns a driver instance by name. alias of Driver()
func GetCache(driverName string) Cache {
return std.Cache(driverName)
}
// Driver get a driver instance by name
func Driver(driverName string) Cache {
return std.Driver(driverName)
}
// Std get default cache manager instance
func Std() *Manager {
return std
}
// DefManager get default cache manager instance
func DefManager() *Manager {
return std
}
// Default get default cache driver instance
func Default() Cache {
return std.Default()
}
// Close all drivers
func Close() error {
return std.Close()
}
// ClearAll all drivers caches
func ClearAll() error {
return std.ClearAll()
}
/*************************************************************
* quick use by default cache driver
*************************************************************/
// Has cache key
func Has(key string) bool {
return std.Default().Has(key)
}
// Get value by key
func Get(key string) any {
return std.Default().Get(key)
}
// Set value by key
func Set(key string, val any, ttl time.Duration) error {
return std.Default().Set(key, val, ttl)
}
// Del value by key
func Del(key string) error {
return std.Default().Del(key)
}
// GetMulti values by keys
func GetMulti(keys []string) map[string]any {
return std.Default().GetMulti(keys)
}
// SetMulti values
func SetMulti(mv map[string]any, ttl time.Duration) error {
return std.Default().SetMulti(mv, ttl)
}
// DelMulti values by keys
func DelMulti(keys []string) error {
return std.Default().DelMulti(keys)
}
// Clear all caches
func Clear() error {
return std.Default().Clear()
}