mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-12 11:15:12 +00:00
add list keys to cache (#352)
* add list keys to cache * add list keys example
This commit is contained in:
32
pkg/cache/cache.go
vendored
32
pkg/cache/cache.go
vendored
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -24,11 +25,12 @@ type Cache interface {
|
||||
Delete(key string) error
|
||||
Increment(key string, val int64) (int64, error)
|
||||
Decrement(key string, val int64) (int64, error)
|
||||
ListKeys() ([]string, error)
|
||||
Close() error
|
||||
}
|
||||
|
||||
type cache struct {
|
||||
sync.Mutex
|
||||
sync.RWMutex
|
||||
closed chan bool
|
||||
LRU *lru.Cache
|
||||
Disk *diskv.Diskv
|
||||
@@ -259,6 +261,30 @@ func (c *cache) Decrement(key string, value int64) (int64, error) {
|
||||
return val, nil
|
||||
}
|
||||
|
||||
func (c *cache) ListKeys() ([]string, error) {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
if c.Store == nil {
|
||||
c.Store = store.DefaultStore
|
||||
}
|
||||
|
||||
prefix := c.Key("")
|
||||
|
||||
recKeys, err := c.Store.List(store.ListPrefix(prefix))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var keys []string
|
||||
|
||||
for _, key := range recKeys {
|
||||
keys = append(keys, strings.TrimPrefix(key, prefix))
|
||||
}
|
||||
|
||||
return keys, nil
|
||||
}
|
||||
|
||||
func Context(ctx context.Context) Cache {
|
||||
return DefaultCache.Context(ctx)
|
||||
}
|
||||
@@ -282,3 +308,7 @@ func Increment(key string, val int64) (int64, error) {
|
||||
func Decrement(key string, val int64) (int64, error) {
|
||||
return DefaultCache.Decrement(key, val)
|
||||
}
|
||||
|
||||
func ListKeys() ([]string, error) {
|
||||
return DefaultCache.ListKeys()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user