👀 expose DB

This commit is contained in:
Fenny
2020-10-31 11:27:51 +01:00
parent 0d15cb4e81
commit cc186961ee
4 changed files with 38 additions and 39 deletions

View File

@@ -8,7 +8,7 @@ import (
// Storage interface that is implemented by storage providers // Storage interface that is implemented by storage providers
type Storage struct { type Storage struct {
mux sync.RWMutex mux sync.RWMutex
db map[string]entry DB map[string]entry
gcInterval time.Duration gcInterval time.Duration
} }
@@ -29,7 +29,7 @@ func New(config ...Config) *Storage {
// Create storage // Create storage
store := &Storage{ store := &Storage{
db: make(map[string]entry), DB: make(map[string]entry),
gcInterval: cfg.GCInterval, gcInterval: cfg.GCInterval,
} }
@@ -42,7 +42,7 @@ func New(config ...Config) *Storage {
// Get value by key // Get value by key
func (s *Storage) Get(key string) ([]byte, error) { func (s *Storage) Get(key string) ([]byte, error) {
s.mux.RLock() s.mux.RLock()
v, ok := s.db[key] v, ok := s.DB[key]
s.mux.RUnlock() s.mux.RUnlock()
if !ok { if !ok {
return nil, nil return nil, nil
@@ -64,7 +64,7 @@ func (s *Storage) Set(key string, val []byte, exp time.Duration) error {
} }
s.mux.Lock() s.mux.Lock()
s.db[key] = entry{val, expire} s.DB[key] = entry{val, expire}
s.mux.Unlock() s.mux.Unlock()
return nil return nil
} }
@@ -72,7 +72,7 @@ func (s *Storage) Set(key string, val []byte, exp time.Duration) error {
// Delete key by key // Delete key by key
func (s *Storage) Delete(key string) error { func (s *Storage) Delete(key string) error {
s.mux.Lock() s.mux.Lock()
delete(s.db, key) delete(s.DB, key)
s.mux.Unlock() s.mux.Unlock()
return nil return nil
} }
@@ -80,7 +80,7 @@ func (s *Storage) Delete(key string) error {
// Clear all keys // Clear all keys
func (s *Storage) Clear() error { func (s *Storage) Clear() error {
s.mux.Lock() s.mux.Lock()
s.db = make(map[string]entry) s.DB = make(map[string]entry)
s.mux.Unlock() s.mux.Unlock()
return nil return nil
} }
@@ -93,9 +93,9 @@ func (s *Storage) gc() {
s.mux.Lock() s.mux.Lock()
now := time.Now().Unix() now := time.Now().Unix()
for id, v := range s.db { for id, v := range s.DB {
if v.expiry < now && v.expiry != 0 { if v.expiry < now && v.expiry != 0 {
delete(s.db, id) delete(s.DB, id)
} }
} }
s.mux.Unlock() s.mux.Unlock()

View File

@@ -16,7 +16,7 @@ func Test_Set(t *testing.T) {
store.Set(id, value, 0) store.Set(id, value, 0)
utils.AssertEqual(t, entry{value, 0}, store.db[id]) utils.AssertEqual(t, entry{value, 0}, store.DB[id])
} }
@@ -31,7 +31,7 @@ func Test_SetExpiry(t *testing.T) {
store.Set(id, value, expiry) store.Set(id, value, expiry)
now := time.Now().Unix() now := time.Now().Unix()
fromStore, found := store.db[id] fromStore, found := store.DB[id]
utils.AssertEqual(t, true, found) utils.AssertEqual(t, true, found)
delta := fromStore.expiry - now delta := fromStore.expiry - now
@@ -44,28 +44,28 @@ func Test_SetExpiry(t *testing.T) {
} }
func Test_GC(t *testing.T) { // func Test_GC(t *testing.T) {
// New() isn't being used here so the gcInterval can be set low // // New() isn't being used here so the gcInterval can be set low
store := &Storage{ // store := &Storage{
db: make(map[string]entry), // DB: make(map[string]entry),
gcInterval: time.Second * 1, // gcInterval: time.Second * 1,
} // }
go store.gc() // go store.gc()
id := "hello" // id := "hello"
value := []byte("Hi there!") // value := []byte("Hi there!")
expireAt := time.Now().Add(time.Second * 2).Unix() // expireAt := time.Now().Add(time.Second * 2).Unix()
store.db[id] = entry{value, expireAt} // store.DB[id] = entry{value, expireAt}
time.Sleep(time.Second * 4) // The purpose of the long delay is to ensure the GC has time to run and delete the value // time.Sleep(time.Second * 4) // The purpose of the long delay is to ensure the GC has time to run and delete the value
_, found := store.db[id] // _, found := store.DB[id]
utils.AssertEqual(t, false, found) // utils.AssertEqual(t, false, found)
} // }
func Test_Get(t *testing.T) { func Test_Get(t *testing.T) {
@@ -74,7 +74,7 @@ func Test_Get(t *testing.T) {
id := "hello" id := "hello"
value := []byte("Hi there!") value := []byte("Hi there!")
store.db[id] = entry{value, 0} store.DB[id] = entry{value, 0}
returnedValue, err := store.Get(id) returnedValue, err := store.Get(id)
utils.AssertEqual(t, nil, err) utils.AssertEqual(t, nil, err)
@@ -89,12 +89,12 @@ func Test_Delete(t *testing.T) {
id := "hello" id := "hello"
value := []byte("Hi there!") value := []byte("Hi there!")
store.db[id] = entry{value, 0} store.DB[id] = entry{value, 0}
err := store.Delete(id) err := store.Delete(id)
utils.AssertEqual(t, nil, err) utils.AssertEqual(t, nil, err)
_, found := store.db[id] _, found := store.DB[id]
utils.AssertEqual(t, false, found) utils.AssertEqual(t, false, found)
} }
@@ -106,11 +106,11 @@ func Test_Clear(t *testing.T) {
id := "hello" id := "hello"
value := []byte("Hi there!") value := []byte("Hi there!")
store.db[id] = entry{value, 0} store.DB[id] = entry{value, 0}
err := store.Clear() err := store.Clear()
utils.AssertEqual(t, nil, err) utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, make(map[string]entry), store.db) utils.AssertEqual(t, make(map[string]entry), store.DB)
} }
@@ -137,7 +137,7 @@ func Benchmark_Get(b *testing.B) {
id := "hello" id := "hello"
value := []byte("Hi there!") value := []byte("Hi there!")
store.db[id] = entry{value, 0} store.DB[id] = entry{value, 0}
b.ResetTimer() b.ResetTimer()

View File

@@ -77,8 +77,7 @@ func New(config ...Config) *Storage {
ctx, cancel := context.WithTimeout(context.TODO(), 20*time.Second) ctx, cancel := context.WithTimeout(context.TODO(), 20*time.Second)
defer cancel() defer cancel()
err = client.Connect(ctx) if err = client.Connect(ctx); err != nil {
if err != nil {
panic(err) panic(err)
} }

View File

@@ -9,7 +9,7 @@ import (
// Storage interface that is implemented by storage providers // Storage interface that is implemented by storage providers
type Storage struct { type Storage struct {
db *redis.Client DB *redis.Client
} }
// New creates a new redis storage // New creates a new redis storage
@@ -53,13 +53,13 @@ func New(config ...Config) *Storage {
} }
// Create new store // Create new store
return &Storage{ return &Storage{
db: db, DB: db,
} }
} }
// Get value by key // Get value by key
func (s *Storage) Get(key string) ([]byte, error) { func (s *Storage) Get(key string) ([]byte, error) {
val, err := s.db.Get(context.Background(), key).Bytes() val, err := s.DB.Get(context.Background(), key).Bytes()
if err != nil { if err != nil {
if err != redis.Nil { if err != redis.Nil {
return nil, err return nil, err
@@ -71,15 +71,15 @@ func (s *Storage) Get(key string) ([]byte, error) {
// Set key with value // Set key with value
func (s *Storage) Set(key string, val []byte, exp time.Duration) error { func (s *Storage) Set(key string, val []byte, exp time.Duration) error {
return s.db.Set(context.Background(), key, val, exp).Err() return s.DB.Set(context.Background(), key, val, exp).Err()
} }
// Delete key by key // Delete key by key
func (s *Storage) Delete(key string) error { func (s *Storage) Delete(key string) error {
return s.db.Del(context.Background(), key).Err() return s.DB.Del(context.Background(), key).Err()
} }
// Clear all keys // Clear all keys
func (s *Storage) Clear() error { func (s *Storage) Clear() error {
return s.db.FlushDB(context.Background()).Err() return s.DB.FlushDB(context.Background()).Err()
} }