@@ -57,23 +57,28 @@ func (m CacheMap) MSet(data map[string]interface{}, duration time.Duration) {
5757 for key , value := range data {
5858 shard := m .GetShard (key )
5959 shard .Lock ()
60- shard .items [key ] = newItem (value , duration , time .Now ().Add (m .options .maxLifetime ))
60+ shard .items [key ] = newItem (value , duration , time .Now ().Add (m .options .maxLifetime ), nil )
6161 shard .Unlock ()
6262 }
6363}
6464
65- // Sets the given value under the specified key
66- func (m CacheMap ) Set (key string , value interface {}, duration * time.Duration ) {
65+ func (m CacheMap ) SetWithCleanup (key string , value interface {}, duration * time.Duration , cleanup func (* Item )) {
6766 // Get map shard.
6867 shard := m .GetShard (key )
6968 shard .Lock ()
7069 if duration == nil {
7170 duration = & m .options .defaultCacheDuration
7271 }
73- shard .items [key ] = newItem (value , * duration , time .Now ().Add (m .options .maxLifetime ))
72+ itm := newItem (value , * duration , time .Now ().Add (m .options .maxLifetime ), cleanup )
73+ shard .items [key ] = itm
7474 shard .Unlock ()
7575}
7676
77+ // Sets the given value under the specified key
78+ func (m CacheMap ) Set (key string , value interface {}, duration * time.Duration ) {
79+ m .SetWithCleanup (key , value , duration , nil )
80+ }
81+
7782// Retrieves an item from the map with the given key, and optionally increase its expiry time if found
7883func (m CacheMap ) TouchGet (key string , touch bool ) (interface {}, bool ) {
7984 shard := m .GetShard (key )
@@ -133,6 +138,10 @@ func (ms CacheMapShared) Remove(key string) {
133138
134139// Removes an element from the map
135140func (ms CacheMapShared ) remove (key string ) {
141+ if itm , ok := ms .items [key ]; ok && itm .onDelete != nil {
142+ itm .onDelete (itm )
143+ }
144+
136145 delete (ms .items , key )
137146}
138147
0 commit comments