Skip to content

Commit 72099f2

Browse files
committed
feat: add increment/decrement in cache
1 parent a5a0b79 commit 72099f2

File tree

6 files changed

+140
-9
lines changed

6 files changed

+140
-9
lines changed

cache/memory_store.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,54 @@ func (s *MemoryStore) Put(key string, val interface{}, timeout time.Duration) er
8585
return nil
8686
}
8787

88+
// Increment the value of an item in the cache.
89+
func (s *MemoryStore) Increment(key string, value ...int) (int, error) {
90+
s.mu.RLock()
91+
defer s.mu.RUnlock()
92+
93+
var by = 1
94+
if len(value) > 0 {
95+
by = value[0]
96+
}
97+
98+
exist, ok := s.items[s.prefix+key]
99+
if !ok {
100+
s.items[s.prefix+key] = item{
101+
Object: 1 + by,
102+
}
103+
} else {
104+
by = exist.Object.(int) + by
105+
exist.Object = by
106+
s.items[s.prefix+key] = exist
107+
}
108+
109+
return by, nil
110+
}
111+
112+
// Decrement the value of an item in the cache.
113+
func (s *MemoryStore) Decrement(key string, value ...int) (int, error) {
114+
s.mu.RLock()
115+
defer s.mu.RUnlock()
116+
117+
var by = 1
118+
if len(value) > 0 {
119+
by = value[0]
120+
}
121+
122+
exist, ok := s.items[s.prefix+key]
123+
if !ok {
124+
s.items[s.prefix+key] = item{
125+
Object: 0 - by,
126+
}
127+
} else {
128+
by = exist.Object.(int) - by
129+
exist.Object = by
130+
s.items[s.prefix+key] = exist
131+
}
132+
133+
return by, nil
134+
}
135+
88136
// Exist check cache's existence in memory.
89137
func (s *MemoryStore) Exist(key string) bool {
90138
s.mu.RLock()
@@ -99,6 +147,31 @@ func (s *MemoryStore) Exist(key string) bool {
99147
return ok
100148
}
101149

150+
// Expire set value expire time.
151+
func (s *MemoryStore) Expire(key string, timeout time.Duration) error {
152+
var e int64
153+
if timeout > 0 {
154+
e = time.Now().Add(timeout).UnixNano()
155+
}
156+
157+
s.mu.RLock()
158+
defer s.mu.RUnlock()
159+
160+
if !s.Exist(key) {
161+
return errors.New("key not exist")
162+
}
163+
164+
item := s.items[s.prefix+key]
165+
item.Expiration = e
166+
s.items[s.prefix+key] = item
167+
168+
if e > 0 {
169+
s.DeleteExpired()
170+
}
171+
172+
return nil
173+
}
174+
102175
// Forget Remove an item from the cache.
103176
func (s *MemoryStore) Forget(key string) error {
104177
delete(s.items, s.prefix+key)

cache/redis_store.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,32 @@ func (s *RedisStore) Put(key string, val interface{}, timeout time.Duration) err
4444
return err
4545
}
4646

47+
// Increment the value of an item in the cache.
48+
func (s *RedisStore) Increment(key string, value ...int) (int, error) {
49+
c := s.pool.Get()
50+
defer c.Close()
51+
52+
var by = 1
53+
if len(value) > 0 {
54+
by = value[0]
55+
}
56+
57+
return redis.Int(c.Do("INCRBY", s.prefix+key, by))
58+
}
59+
60+
// Decrement the value of an item in the cache.
61+
func (s *RedisStore) Decrement(key string, value ...int) (int, error) {
62+
c := s.pool.Get()
63+
defer c.Close()
64+
65+
var by = 1
66+
if len(value) > 0 {
67+
by = value[0]
68+
}
69+
70+
return redis.Int(c.Do("DECRBY", s.prefix+key, by))
71+
}
72+
4773
// Exist check cache's existence in redis.
4874
func (s *RedisStore) Exist(key string) bool {
4975
c := s.pool.Get()
@@ -55,6 +81,15 @@ func (s *RedisStore) Exist(key string) bool {
5581
return v
5682
}
5783

84+
// Expire set value expire time.
85+
func (s *RedisStore) Expire(key string, timeout time.Duration) error {
86+
c := s.pool.Get()
87+
defer c.Close()
88+
_, err := c.Do("EXPIRE", s.prefix+key, int64(timeout/time.Second))
89+
90+
return err
91+
}
92+
5893
// Forget Remove an item from the cache.
5994
func (s *RedisStore) Forget(key string) error {
6095
c := s.pool.Get()

cache/repository.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,21 @@ func (r *Repository) Add(key string, val interface{}, timeout time.Duration) err
5656
return r.store.Put(key, val, timeout)
5757
}
5858

59+
// Increment the value of an item in the cache.
60+
func (r *Repository) Increment(key string, value ...int) (int, error) {
61+
return r.store.Increment(key, value...)
62+
}
63+
64+
// Decrement the value of an item in the cache.
65+
func (r *Repository) Decrement(key string, value ...int) (int, error) {
66+
return r.store.Decrement(key, value...)
67+
}
68+
69+
// Expire set value expire time.
70+
func (r *Repository) Expire(key string, timeout time.Duration) error {
71+
return r.store.Expire(key, timeout)
72+
}
73+
5974
// Remember Get an item from the cache, or store the default value.
6075
func (r *Repository) Remember(key string, val interface{}, timeout time.Duration, callback func() interface{}) error {
6176
err := r.Get(key, val)

cache/store.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,18 @@ type Store interface {
1111
// Put set cached value with key and expire time.
1212
Put(key string, val interface{}, timeout time.Duration) error
1313

14+
// Increment the value of an item in the cache.
15+
Increment(key string, value ...int) (int, error)
16+
17+
// Decrement the value of an item in the cache.
18+
Decrement(key string, value ...int) (int, error)
19+
1420
// Exist check cache's existence in redis.
1521
Exist(key string) bool
1622

23+
// Expire set value expire time.
24+
Expire(key string, timeout time.Duration) error
25+
1726
// Forget Remove an item from the cache.
1827
Forget(key string) error
1928

@@ -23,4 +32,3 @@ type Store interface {
2332
// TTL get the ttl of the key.
2433
TTL(key string) (int64, error)
2534
}
26-

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ module github.com/forgoer/thinkgo
33
go 1.14
44

55
require (
6-
github.com/gomodule/redigo v2.0.0+incompatible
7-
github.com/stretchr/testify v1.5.1
6+
github.com/gomodule/redigo v1.8.8
7+
github.com/stretchr/testify v1.7.0
88
)

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
22
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3-
github.com/gomodule/redigo v2.0.0+incompatible h1:K/R+8tc58AaqLkqG2Ol3Qk+DR/TlNuhuh457pBFPtt0=
4-
github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
3+
github.com/gomodule/redigo v1.8.8 h1:f6cXq6RRfiyrOJEV7p3JhLDlmawGBVBBP1MggY8Mo4E=
4+
github.com/gomodule/redigo v1.8.8/go.mod h1:7ArFNvsTjH8GMMzB4uy1snslv2BwmginuMs06a1uzZE=
55
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
66
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
77
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
8-
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
9-
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
8+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
9+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
1010
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1111
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
12-
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
13-
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
12+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
13+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)