Skip to content

Commit

Permalink
feat: Add DeleteKeysByPattern method
Browse files Browse the repository at this point in the history
  • Loading branch information
TwiN committed Aug 4, 2022
1 parent 785ded3 commit f728acb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ cache.StartJanitor()
| GetKeysByPattern | Retrieves a slice of keys that matches a given pattern. |
| Delete | Removes a key from the cache. |
| DeleteAll | Removes multiple keys from the cache. |
| DeleteKeysByPattern | Removes all keys that that matches a given pattern. |
| Count | Gets the size of the cache. This includes cache keys which may have already expired, but have not been removed yet. |
| Clear | Wipes the cache. |
| TTL | Gets the time until a cache key expires. |
Expand Down
10 changes: 8 additions & 2 deletions gocache.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,7 @@ func (cache *Cache) GetAll() map[string]interface{} {
// cache.GetKeysByPattern("*some*", 0) will return all keys containing "some" in them
// cache.GetKeysByPattern("*some*", 5) will return 5 keys (or less) containing "some" in them
//
// Note that GetKeysByPattern does not trigger active evictions, nor does it count as accessing the entry, the latter
// only applying if the cache uses the LeastRecentlyUsed eviction policy.
// Note that GetKeysByPattern does not trigger active evictions, nor does it count as accessing the entry (if LRU).
// The reason for that behavior is that these two (active eviction and access) only applies when you access the value
// of the cache entry, and this function only returns the keys.
func (cache *Cache) GetKeysByPattern(pattern string, limit int) []string {
Expand Down Expand Up @@ -435,6 +434,13 @@ func (cache *Cache) DeleteAll(keys []string) int {
return numberOfKeysDeleted
}

// DeleteKeysByPattern deletes all entries matching a given key pattern and returns the number of entries deleted.
//
// Note that DeleteKeysByPattern does not trigger active evictions, nor does it count as accessing the entry (if LRU).
func (cache *Cache) DeleteKeysByPattern(pattern string) int {
return cache.DeleteAll(cache.GetKeysByPattern(pattern, 0))
}

// Count returns the total amount of entries in the cache, regardless of whether they're expired or not
func (cache *Cache) Count() int {
cache.mutex.RLock()
Expand Down
17 changes: 17 additions & 0 deletions gocache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,23 @@ func TestCache_DeleteAll(t *testing.T) {
}
}

func TestCache_DeleteKeysByPattern(t *testing.T) {
cache := NewCache()
cache.Set("a1", []byte("v"))
cache.Set("a2", []byte("v"))
cache.Set("b1", []byte("v"))
if len(cache.GetByKeys([]string{"a1", "a2", "b1"})) != 3 {
t.Error("Expected keys 1, 2 and 3 to exist")
}
numberOfDeletedKeys := cache.DeleteKeysByPattern("a*")
if numberOfDeletedKeys != 2 {
t.Errorf("Expected 2 keys to have been deleted, but only %d were deleted", numberOfDeletedKeys)
}
if _, exists := cache.Get("b1"); !exists {
t.Error("Expected key b1 to still exist")
}
}

func TestCache_TTL(t *testing.T) {
cache := NewCache()
ttl, err := cache.TTL("key")
Expand Down

0 comments on commit f728acb

Please sign in to comment.