Skip to content

Instantly share code, notes, and snippets.

@Loupax
Created September 9, 2019 09:36
Show Gist options
  • Save Loupax/3ab57e0f458ecb493698aa5b07afabc9 to your computer and use it in GitHub Desktop.
Save Loupax/3ab57e0f458ecb493698aa5b07afabc9 to your computer and use it in GitHub Desktop.
package redis
import (
"fmt"
"sync"
)
type MockClient struct {
lock sync.RWMutex
cache map[string]string
}
func NewMockClient() *MockClient {
return &MockClient{
lock: sync.RWMutex{},
cache: make(map[string]string),
}
}
func (m *MockClient) Get(key string) (v string, err error) {
m.lock.RLock()
val, ok := m.cache[key]
m.lock.RUnlock()
if ok == true {
return val, nil
}
return "", NewNotFoundError(fmt.Sprintf("key %s not found", key))
}
func (m *MockClient) Set(key, value string, expiresSeconds uint) error {
m.lock.Lock()
m.cache[key] = value
m.lock.Unlock()
return nil
}
func (m *MockClient) Delete(keys ...string) error {
m.lock.Lock()
for _, key := range keys {
delete(m.cache, key)
}
m.lock.Unlock()
return nil
}
func (m *MockClient) Close() error { return nil }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment