File tree 2 files changed +35
-0
lines changed
2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -911,6 +911,23 @@ func (c *cache) Delete(k string) {
911
911
}
912
912
}
913
913
914
+ // Remove an item from the cache. Does nothing if the key is not in the cache.
915
+ // There is delete cache data, return cache data and presence status
916
+ func (c * cache ) Remove (k string ) (interface {}, bool ) {
917
+ c .mu .Lock ()
918
+ v , found := c .items [k ]
919
+ if found {
920
+ delete (c .items , k )
921
+ c .mu .Unlock ()
922
+ if c .onEvicted != nil {
923
+ c .onEvicted (k , v )
924
+ }
925
+ return v .Object , true
926
+ }
927
+ c .mu .Unlock ()
928
+ return nil , false
929
+ }
930
+
914
931
func (c * cache ) delete (k string ) (interface {}, bool ) {
915
932
if c .onEvicted != nil {
916
933
if v , found := c .items [k ]; found {
Original file line number Diff line number Diff line change @@ -1149,7 +1149,25 @@ func TestDelete(t *testing.T) {
1149
1149
t .Error ("x is not nil:" , x )
1150
1150
}
1151
1151
}
1152
+ func TestREmove (t * testing.T ) {
1153
+ tc := New (DefaultExpiration , 0 )
1154
+ tc .Set ("foo" , "bar" , DefaultExpiration )
1155
+ data , ok := tc .Remove ("foo" )
1156
+ if ! ok {
1157
+ t .Error ("foo was not found, It should be discovered" )
1158
+ }
1159
+ if data != "bar" {
1160
+ t .Error ("foo should be bar " )
1161
+ }
1152
1162
1163
+ x , found := tc .Get ("foo" )
1164
+ if found {
1165
+ t .Error ("foo was found, but it should have been deleted" )
1166
+ }
1167
+ if x != nil {
1168
+ t .Error ("x is not nil:" , x )
1169
+ }
1170
+ }
1153
1171
func TestItemCount (t * testing.T ) {
1154
1172
tc := New (DefaultExpiration , 0 )
1155
1173
tc .Set ("foo" , "1" , DefaultExpiration )
You can’t perform that action at this time.
0 commit comments