Created
September 9, 2019 09:36
-
-
Save Loupax/3ab57e0f458ecb493698aa5b07afabc9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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