mirror of
https://github.com/gofiber/storage.git
synced 2025-12-24 13:29:30 +08:00
Add support for Keys() to Redis Driver
This commit is contained in:
@@ -74,8 +74,6 @@ func New(config ...Config) *Storage {
|
||||
}
|
||||
}
|
||||
|
||||
// ...
|
||||
|
||||
// Get value by key
|
||||
func (s *Storage) Get(key string) ([]byte, error) {
|
||||
if len(key) <= 0 {
|
||||
@@ -118,3 +116,32 @@ func (s *Storage) Close() error {
|
||||
func (s *Storage) Conn() redis.UniversalClient {
|
||||
return s.db
|
||||
}
|
||||
|
||||
// Return all the keys
|
||||
func (s *Storage) Keys() ([][]byte, error) {
|
||||
var keys [][]byte
|
||||
var cursor uint64
|
||||
|
||||
for {
|
||||
var batch []string
|
||||
var err error
|
||||
batch, cursor, err = s.db.Scan(context.Background(), cursor, "*", 10).Result()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, key := range batch {
|
||||
keys = append(keys, []byte(key))
|
||||
}
|
||||
|
||||
if cursor == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if len(keys) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return keys, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user