Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scan and keys functions to get objects and keys matching key pattern #171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ Dustin Sallings <[email protected]>
Jason Mooberry <[email protected]>
Sergey Shepelev <[email protected]>
Alex Edwards <[email protected]>
Minudika Malshan <[email protected]>
38 changes: 38 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"os"
"runtime"
"strings"
"sync"
"time"
)
Expand Down Expand Up @@ -165,6 +166,43 @@ func (c *cache) GetWithExpiration(k string) (interface{}, time.Time, bool) {
return item.Object, time.Time{}, true
}

// Keys retrieves all non-expired keys in the cache that contain the given substring pattern.
// It returns a slice of keys matching the pattern.
func (c *cache) Keys(pattern string) []interface{} {
c.mu.RLock()
defer c.mu.RUnlock()

var matchedKeys []interface{}
for k, i := range c.items {
if strings.Contains(k, pattern) {
if i.Expiration <= 0 || time.Now().UnixNano() < i.Expiration {
matchedKeys = append(matchedKeys, k)
}
}
}

return matchedKeys
}

// Scan searches for and retrieves all non-expired cached items associated with keys that
// contain the given substring pattern.
// It returns a slice of matching cached items.
func (c *cache) Scan(pattern string) []interface{} {
c.mu.RLock()
defer c.mu.RUnlock()

var matchedObjects []interface{}
for k, i := range c.items {
if strings.Contains(k, pattern) {
if i.Expiration <= 0 || time.Now().UnixNano() < i.Expiration {
matchedObjects = append(matchedObjects, i.Object)
}
}
}

return matchedObjects
}

func (c *cache) get(k string) (interface{}, bool) {
item, found := c.items[k]
if !found {
Expand Down
47 changes: 47 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1769,3 +1769,50 @@ func TestGetWithExpiration(t *testing.T) {
t.Error("expiration for e is in the past")
}
}

func TestKeys(t *testing.T) {
tc := New(DefaultExpiration, 0)

tc.Set("jane_001", "admin", 50*time.Millisecond)
tc.Set("john_002", "user", NoExpiration)
tc.Set("alice_003", "editor", 10*time.Millisecond)

keys := tc.Keys("jane")
if len(keys) != 1 || keys[0] != "jane_001" {
t.Error("Expected to find 'jane_001' but got:", keys)
}

keys = tc.Keys("002")
if len(keys) != 1 || keys[0] != "john_002" {
t.Error("Expected to find 'john_002' but got:", keys)
}

keys = tc.Keys("003")
if len(keys) != 0 {
t.Error("Expected no keys for 'alice_003' as it should have expired, but got:", keys)
}
}

func TestScan(t *testing.T) {
tc := New(DefaultExpiration, 0)

tc.Set("jane_001", "admin", 50*time.Millisecond)
tc.Set("john_002", "user", NoExpiration)
tc.Set("alice_003", "editor", 10*time.Millisecond)

roles := tc.Scan("jane")
if len(roles) != 1 || roles[0].(string) != "admin" {
t.Error("Expected to find role 'admin' for 'jane_001' but got:", roles)
}

roles = tc.Scan("002")
if len(roles) != 1 || roles[0].(string) != "user" {
t.Error("Expected to find role 'user' for 'john_002' but got:", roles)
}

<-time.After(25 * time.Millisecond)
roles = tc.Scan("003")
if len(roles) != 0 {
t.Error("Expected no roles for 'alice_003' as it should have expired, but got:", roles)
}
}