-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.go
More file actions
35 lines (31 loc) · 782 Bytes
/
string.go
File metadata and controls
35 lines (31 loc) · 782 Bytes
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
package cache
import (
"time"
"github.com/itmisx/timewheel"
)
// Set Key value with expiration and expiration callback function
func Set(key string, value interface{}, expiration time.Duration, expirationFunc func(key string, value interface{})) (success bool) {
if key == "" {
return false
}
c.mu.Lock()
defer c.mu.Unlock()
c.items[key] = value
if expiration > 0 {
c.itemFunc[key] = expirationFunc
timewheel.AddTimer(key+":::"+"", expiration, map[string]string{
"key": key,
"field": "",
})
}
return true
}
// Get the vlaue of given key , if exist return true, or return false
func Get(key string) (value interface{}, found bool) {
c.mu.RLock()
defer c.mu.RUnlock()
if _, ok := c.items[key]; ok {
return c.items[key], true
}
return nil, false
}