-
Notifications
You must be signed in to change notification settings - Fork 0
/
method.go
132 lines (124 loc) · 2.15 KB
/
method.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
package pot
import (
"fmt"
"time"
)
/*
checkClientCache
@Desc: check client
@receiver: p
@return: error
*/
func (p *Pot) checkClientCache() error {
if p.cache == nil || p.cache.elems == nil {
return fmt.Errorf("pot: client cache is nil, chech cache init")
}
return nil
}
/*
Set
@Desc: set value to cache
@receiver: p
@param: key
@param: value
@param: expiration
@return: error
*/
func (p *Pot) Set(key string, value interface{}, expiration ...time.Duration) *StatusCmd {
cmd := newStatusCmd(key)
if err := p.checkClientCache(); err != nil {
cmd.setErr(err)
return cmd
}
p.cache.set(key, value, expiration...)
cmd.setSuccess(true)
return cmd
}
/*
Get
@Desc: get value by cache
@receiver: p
@param: key
@return: interface{}
@return: error
*/
func (p *Pot) Get(key string) *StringCmd {
cmd := newStringCmd(key)
if err := p.checkClientCache(); err != nil {
cmd.setErr(err)
return cmd
}
cmd.setVal(p.cache.get(key))
return cmd
}
/*
Del
@Desc:
@receiver: p
@param: key
*/
func (p *Pot) Del(key string) *StatusCmd {
cmd := newStatusCmd(key)
if err := p.checkClientCache(); err != nil {
cmd.setErr(err)
return cmd
}
p.cache.del(key)
cmd.setSuccess(true)
return cmd
}
/*
Exists
@Desc: check key exists
@receiver: p
@param: key
@return: bool
*/
func (p *Pot) Exists(key string) *StatusCmd {
cmd := newStatusCmd(key)
if err := p.checkClientCache(); err != nil {
cmd.setErr(err)
return cmd
}
if p.cache.exists(key) {
cmd.setResult(POT_ACTION_RESULT_EXISTS)
} else {
cmd.setResult(EXPIRATION_IS_EXPIRED)
}
cmd.setSuccess(true)
return cmd
}
/*
TTL
@Desc: check key expire ttl
@receiver: p
@param: key
@return: int64
*/
func (p *Pot) TTL(key string) *StatusCmd {
cmd := newStatusCmd(key)
if err := p.checkClientCache(); err != nil {
cmd.setErr(err)
return cmd
}
cmd.setSuccess(true)
cmd.setResult(p.cache.ttl(key))
return cmd
}
/*
Expire
@Desc: set key expire
@receiver: c
@param: key
@param: expire
*/
func (c *Pot) Expire(key string, expire time.Duration) *StatusCmd {
cmd := newStatusCmd(key)
if err := c.checkClientCache(); err != nil {
cmd.setErr(err)
return cmd
}
c.cache.expire(key, expire)
cmd.setSuccess(true)
return cmd
}