|
9 | 9 | "context" |
10 | 10 | "errors" |
11 | 11 | "fmt" |
| 12 | + "path" |
12 | 13 | "strings" |
13 | 14 | "time" |
14 | 15 |
|
@@ -63,18 +64,33 @@ func (b *Backend) Delete(key string) error { |
63 | 64 | return b.cache.Delete(context.Background(), key) |
64 | 65 | } |
65 | 66 |
|
66 | | -// List returns the values of all entries whose keys start with prefix. |
67 | | -// An empty prefix returns every value currently in the store. |
| 67 | +// List returns the values of all entries whose keys match wildcard. |
| 68 | +// If wildcard contains no glob tokens, it is treated as a key prefix. |
| 69 | +// If wildcard contains glob tokens ('*', '?', '['), path.Match is used. |
| 70 | +// An empty wildcard returns every value currently in the store. |
68 | 71 | // The returned slice is a snapshot; mutations to it do not affect the cache. |
69 | 72 | // eko/gocache does not expose a scan API, so this method accesses the |
70 | 73 | // underlying go-cache client directly via Items(). |
71 | | -func (b *Backend) List(prefix string) ([][]byte, error) { |
| 74 | +func (b *Backend) List(wildcard string) ([][]byte, error) { |
72 | 75 | items := b.client.Items() |
73 | 76 | var result [][]byte |
| 77 | + hasGlob := strings.ContainsAny(wildcard, "*?[") |
| 78 | + if hasGlob { |
| 79 | + if _, err := path.Match(wildcard, ""); err != nil { |
| 80 | + return nil, fmt.Errorf("gocache: invalid wildcard %q: %w", wildcard, err) |
| 81 | + } |
| 82 | + } |
74 | 83 |
|
75 | 84 | for k, item := range items { |
76 | | - if !strings.HasPrefix(k, prefix) { |
77 | | - continue |
| 85 | + if wildcard != "" { |
| 86 | + if hasGlob { |
| 87 | + matched, _ := path.Match(wildcard, k) |
| 88 | + if !matched { |
| 89 | + continue |
| 90 | + } |
| 91 | + } else if !strings.HasPrefix(k, wildcard) { |
| 92 | + continue |
| 93 | + } |
78 | 94 | } |
79 | 95 |
|
80 | 96 | data, ok := item.Object.([]byte) |
|
0 commit comments