mirror of
https://github.com/gofiber/storage.git
synced 2025-10-22 00:01:53 +08:00
feat: Update mockstorage package with Entry struct
This commit is contained in:
19
.github/workflows/release-drafter-mockstorage.yml
vendored
Normal file
19
.github/workflows/release-drafter-mockstorage.yml
vendored
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
name: Release Drafter MockStorage
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
# branches to consider in the event; optional, defaults to all
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- main
|
||||||
|
paths:
|
||||||
|
- 'mockstorage/**'
|
||||||
|
jobs:
|
||||||
|
draft_release_memcache:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
timeout-minutes: 30
|
||||||
|
steps:
|
||||||
|
- uses: release-drafter/release-drafter@v6
|
||||||
|
with:
|
||||||
|
config-name: release-drafter-memory.yml
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
@@ -23,6 +23,31 @@ A mock storage implementation for Fiber. This storage is not persistent and is o
|
|||||||
|
|
||||||
|
|
||||||
### Signatures
|
### Signatures
|
||||||
|
|
||||||
|
#### Structs
|
||||||
|
|
||||||
|
```go
|
||||||
|
type Storage struct {
|
||||||
|
// contains filtered or unexported fields
|
||||||
|
}
|
||||||
|
|
||||||
|
type Entry struct {
|
||||||
|
Value []byte
|
||||||
|
Exp time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
type CustomFuncs struct {
|
||||||
|
GetFunc func(key string) ([]byte, error)
|
||||||
|
SetFunc func(key string, val []byte, exp time.Duration) error
|
||||||
|
DeleteFunc func(key string) error
|
||||||
|
ResetFunc func() error
|
||||||
|
CloseFunc func() error
|
||||||
|
ConnFunc func() map[string]Entry
|
||||||
|
KeysFunc func() ([][]byte, error)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Functions
|
||||||
```go
|
```go
|
||||||
// New creates a new Storage instance. You can optionally pass a Config.
|
// New creates a new Storage instance. You can optionally pass a Config.
|
||||||
func New(config ...Config) *Storage
|
func New(config ...Config) *Storage
|
||||||
@@ -43,7 +68,7 @@ func (s *Storage) Reset() error
|
|||||||
func (s *Storage) Close() error
|
func (s *Storage) Close() error
|
||||||
|
|
||||||
// Conn returns a copy of the current state of the storage.
|
// Conn returns a copy of the current state of the storage.
|
||||||
func (s *Storage) Conn() map[string]entry
|
func (s *Storage) Conn() map[string]Entry
|
||||||
|
|
||||||
// Keys returns a list of all keys in the storage.
|
// Keys returns a list of all keys in the storage.
|
||||||
func (s *Storage) Keys() ([][]byte, error)
|
func (s *Storage) Keys() ([][]byte, error)
|
||||||
|
@@ -14,14 +14,14 @@ type Config struct {
|
|||||||
// Storage is the mock storage adapter.
|
// Storage is the mock storage adapter.
|
||||||
type Storage struct {
|
type Storage struct {
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
data map[string]entry
|
data map[string]Entry
|
||||||
custom *CustomFuncs
|
custom *CustomFuncs
|
||||||
}
|
}
|
||||||
|
|
||||||
// entry struct to hold value and expiration time.
|
// Entry struct to hold value and expiration time.
|
||||||
type entry struct {
|
type Entry struct {
|
||||||
value []byte
|
Value []byte
|
||||||
exp time.Time
|
Exp time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
// CustomFuncs allows injecting custom behaviors for testing.
|
// CustomFuncs allows injecting custom behaviors for testing.
|
||||||
@@ -31,14 +31,14 @@ type CustomFuncs struct {
|
|||||||
DeleteFunc func(key string) error
|
DeleteFunc func(key string) error
|
||||||
ResetFunc func() error
|
ResetFunc func() error
|
||||||
CloseFunc func() error
|
CloseFunc func() error
|
||||||
ConnFunc func() map[string]entry
|
ConnFunc func() map[string]Entry
|
||||||
KeysFunc func() ([][]byte, error)
|
KeysFunc func() ([][]byte, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new mock storage with optional configuration.
|
// New creates a new mock storage with optional configuration.
|
||||||
func New(config ...Config) *Storage {
|
func New(config ...Config) *Storage {
|
||||||
s := &Storage{
|
s := &Storage{
|
||||||
data: make(map[string]entry),
|
data: make(map[string]Entry),
|
||||||
custom: &CustomFuncs{
|
custom: &CustomFuncs{
|
||||||
GetFunc: nil,
|
GetFunc: nil,
|
||||||
SetFunc: nil,
|
SetFunc: nil,
|
||||||
@@ -71,11 +71,11 @@ func (s *Storage) Get(key string) ([]byte, error) {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return nil, errors.New("key not found")
|
return nil, errors.New("key not found")
|
||||||
}
|
}
|
||||||
if !e.exp.IsZero() && time.Now().After(e.exp) {
|
if !e.Exp.IsZero() && time.Now().After(e.Exp) {
|
||||||
delete(s.data, key)
|
delete(s.data, key)
|
||||||
return nil, errors.New("key expired")
|
return nil, errors.New("key expired")
|
||||||
}
|
}
|
||||||
return e.value, nil
|
return e.Value, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set sets the value for a given key with an expiration time.
|
// Set sets the value for a given key with an expiration time.
|
||||||
@@ -92,7 +92,7 @@ func (s *Storage) Set(key string, val []byte, exp time.Duration) error {
|
|||||||
expTime = time.Now().Add(exp)
|
expTime = time.Now().Add(exp)
|
||||||
}
|
}
|
||||||
|
|
||||||
s.data[key] = entry{value: val, exp: expTime}
|
s.data[key] = Entry{Value: val, Exp: expTime}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,7 +118,7 @@ func (s *Storage) Reset() error {
|
|||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
|
|
||||||
s.data = make(map[string]entry)
|
s.data = make(map[string]Entry)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,7 +133,7 @@ func (s *Storage) Close() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Conn returns the internal data map (for testing purposes).
|
// Conn returns the internal data map (for testing purposes).
|
||||||
func (s *Storage) Conn() map[string]entry {
|
func (s *Storage) Conn() map[string]Entry {
|
||||||
if s.custom.ConnFunc != nil {
|
if s.custom.ConnFunc != nil {
|
||||||
return s.custom.ConnFunc()
|
return s.custom.ConnFunc()
|
||||||
}
|
}
|
||||||
@@ -141,9 +141,9 @@ func (s *Storage) Conn() map[string]entry {
|
|||||||
s.mu.RLock()
|
s.mu.RLock()
|
||||||
defer s.mu.RUnlock()
|
defer s.mu.RUnlock()
|
||||||
|
|
||||||
copyData := make(map[string]entry)
|
copyData := make(map[string]Entry)
|
||||||
for k, v := range s.data {
|
for k, v := range s.data {
|
||||||
copyData[k] = v
|
copyData[k] = Entry{Value: v.Value, Exp: v.Exp}
|
||||||
}
|
}
|
||||||
return copyData
|
return copyData
|
||||||
}
|
}
|
||||||
|
@@ -68,10 +68,10 @@ func TestStorageConnFunc(t *testing.T) {
|
|||||||
store := New()
|
store := New()
|
||||||
|
|
||||||
customFuncs := &CustomFuncs{
|
customFuncs := &CustomFuncs{
|
||||||
ConnFunc: func() map[string]entry {
|
ConnFunc: func() map[string]Entry {
|
||||||
return map[string]entry{
|
return map[string]Entry{
|
||||||
"customKey1": {value: []byte("customValue1"), exp: time.Time{}},
|
"customKey1": {Value: []byte("customValue1"), Exp: time.Time{}},
|
||||||
"customKey2": {value: []byte("customValue2"), exp: time.Now().Add(1 * time.Hour)},
|
"customKey2": {Value: []byte("customValue2"), Exp: time.Now().Add(1 * time.Hour)},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -80,13 +80,13 @@ func TestStorageConnFunc(t *testing.T) {
|
|||||||
|
|
||||||
// Test custom Conn
|
// Test custom Conn
|
||||||
conn := store.Conn()
|
conn := store.Conn()
|
||||||
expectedConn := map[string]entry{
|
expectedConn := map[string]Entry{
|
||||||
"customKey1": {value: []byte("customValue1"), exp: time.Time{}},
|
"customKey1": {Value: []byte("customValue1"), Exp: time.Time{}},
|
||||||
"customKey2": {value: []byte("customValue2"), exp: time.Now().Add(1 * time.Hour)},
|
"customKey2": {Value: []byte("customValue2"), Exp: time.Now().Add(1 * time.Hour)},
|
||||||
}
|
}
|
||||||
|
|
||||||
for k, v := range expectedConn {
|
for k, v := range expectedConn {
|
||||||
if val, ok := conn[k]; !ok || !bytes.Equal(val.value, v.value) {
|
if val, ok := conn[k]; !ok || !bytes.Equal(val.Value, v.Value) {
|
||||||
t.Errorf("Conn() = %v, want %v", conn, expectedConn)
|
t.Errorf("Conn() = %v, want %v", conn, expectedConn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -174,10 +174,10 @@ func TestStorageCustomBehavior(t *testing.T) {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
ConnFunc: func() map[string]entry {
|
ConnFunc: func() map[string]Entry {
|
||||||
return map[string]entry{
|
return map[string]Entry{
|
||||||
"customKey1": {value: []byte("customValue1"), exp: time.Time{}},
|
"customKey1": {Value: []byte("customValue1"), Exp: time.Time{}},
|
||||||
"customKey2": {value: []byte("customValue2"), exp: time.Now().Add(1 * time.Hour)},
|
"customKey2": {Value: []byte("customValue2"), Exp: time.Now().Add(1 * time.Hour)},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
KeysFunc: func() ([][]byte, error) {
|
KeysFunc: func() ([][]byte, error) {
|
||||||
@@ -225,13 +225,13 @@ func TestStorageCustomBehavior(t *testing.T) {
|
|||||||
|
|
||||||
// Test custom Conn
|
// Test custom Conn
|
||||||
conn := store.Conn()
|
conn := store.Conn()
|
||||||
expectedConn := map[string]entry{
|
expectedConn := map[string]Entry{
|
||||||
"customKey1": {value: []byte("customValue1"), exp: time.Time{}},
|
"customKey1": {Value: []byte("customValue1"), Exp: time.Time{}},
|
||||||
"customKey2": {value: []byte("customValue2"), exp: time.Now().Add(1 * time.Hour)},
|
"customKey2": {Value: []byte("customValue2"), Exp: time.Now().Add(1 * time.Hour)},
|
||||||
}
|
}
|
||||||
|
|
||||||
for k, v := range expectedConn {
|
for k, v := range expectedConn {
|
||||||
if val, ok := conn[k]; !ok || !bytes.Equal(val.value, v.value) {
|
if val, ok := conn[k]; !ok || !bytes.Equal(val.Value, v.Value) {
|
||||||
t.Errorf("Conn() = %v, want %v", conn, expectedConn)
|
t.Errorf("Conn() = %v, want %v", conn, expectedConn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -261,8 +261,8 @@ func TestStorageConnAndKeys(t *testing.T) {
|
|||||||
t.Fatalf("Set() error = %v, wantErr %v", err, nil)
|
t.Fatalf("Set() error = %v, wantErr %v", err, nil)
|
||||||
}
|
}
|
||||||
conn := store.Conn()
|
conn := store.Conn()
|
||||||
if val, ok := conn["key1"]; !ok || !bytes.Equal(val.value, []byte("value1")) {
|
if val, ok := conn["key1"]; !ok || !bytes.Equal(val.Value, []byte("value1")) {
|
||||||
t.Errorf("Conn() = %v, want %v", conn, map[string]entry{"key1": {value: []byte("value1"), exp: time.Time{}}})
|
t.Errorf("Conn() = %v, want %v", conn, map[string]Entry{"key1": {Value: []byte("value1"), Exp: time.Time{}}})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test Keys
|
// Test Keys
|
||||||
|
Reference in New Issue
Block a user