Files
hamster-tycoon/storage/dao/fake/database_fake.go
2020-01-05 23:20:38 +01:00

38 lines
804 B
Go
Executable File

package fake
import (
"encoding/json"
"hamster-tycoon/storage/dao"
"hamster-tycoon/utils"
"time"
"github.com/allegro/bigcache"
)
type DatabaseFake struct {
Cache *bigcache.BigCache
}
func NewDatabaseFake() dao.Database {
cache, err := bigcache.NewBigCache(bigcache.DefaultConfig(time.Minute))
if err != nil {
utils.GetLogger().WithError(err).Fatal("Error while instantiate cache")
}
return &DatabaseFake{
Cache: cache,
}
}
func (db *DatabaseFake) save(key string, data []interface{}) {
b, err := json.Marshal(data)
if err != nil {
utils.GetLogger().WithError(err).Errorf("Error while marshal fake %s", key)
db.Cache.Set(key, []byte("[]"))
return
}
err = db.Cache.Set(key, b)
if err != nil {
utils.GetLogger().WithError(err).Errorf("Error while saving fake %s", key)
}
}