diff --git a/clients/inmemory/fuego.go b/clients/inmemory/fuego.go new file mode 100644 index 0000000..0fb9789 --- /dev/null +++ b/clients/inmemory/fuego.go @@ -0,0 +1,42 @@ +package embedded + +import ( + cache "github.com/tomiok/fuego-cache/fuego" + "github.com/tomiok/fuego-cache/logs" +) + +type FuegoEmbedded interface { + Insert(key, value string) error + Delete(key string) error + Get(key string) (string, error) + List() []string +} + +// FuegoInMemory is a mode for embedded database +type FuegoInMemory struct { + DB *cache.InMemoryDB +} + +func (f *FuegoInMemory) Insert(key, value string) error { + res, err := f.DB.Fuego.SetOne(key, value) + + if err != nil { + logs.Error(err.Error()) + return err + } + + logs.Info("result: " + res) + + return nil +} + +func (f *FuegoInMemory) Get(key string) (string, error) { + res, err := f.DB.Fuego.GetOne(key) + + if err != nil { + logs.Error(err.Error()) + return "", err + } + + return res, nil +} diff --git a/clients/inmemory/fuego_test.go b/clients/inmemory/fuego_test.go new file mode 100644 index 0000000..828394c --- /dev/null +++ b/clients/inmemory/fuego_test.go @@ -0,0 +1,35 @@ +package embedded + +import ( + cache "github.com/tomiok/fuego-cache/fuego" + "github.com/tomiok/fuego-cache/logs" + "testing" +) + +func Test_Insert_and_get_InMemory(t *testing.T) { + c := cache.NewCache(cache.FuegoConfig{ + DiskPersistence: true, + FileLocation: "C:\\Users\\Tomás\\Downloads\\fuego.csv", + Mode: "", + }) + fuego := FuegoInMemory{ + DB: &cache.InMemoryDB{Fuego: c}, + } + + err := fuego.Insert("1", "hola amigos") + + if err != nil { + t.Error(err.Error()) + t.Fail() + } + + res, err := fuego.Get("1") + + if err != nil { + t.Error(err.Error()) + t.Fail() + } + + logs.Info(res) +} + diff --git a/fuego/cache.go b/fuego/cache.go index 30cafd0..83974ef 100644 --- a/fuego/cache.go +++ b/fuego/cache.go @@ -45,6 +45,10 @@ type fuego struct { entries map[int]fuegoValue } +type InMemoryDB struct { + Fuego *cache +} + //fuegoValue is the actual value to store and the ttl. type fuegoValue struct { value string @@ -57,6 +61,7 @@ type entry struct { object fuegoValue } + func (c *cache) Clear() { c.cache.entries = make(map[int]fuegoValue) }