@@ -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.
89137func (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.
103176func (s * MemoryStore ) Forget (key string ) error {
104177 delete (s .items , s .prefix + key )
0 commit comments