Skip to content

Commit

Permalink
add in memory draft
Browse files Browse the repository at this point in the history
  • Loading branch information
tomiok committed Dec 8, 2020
1 parent 4db4739 commit 5ad2e52
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
42 changes: 42 additions & 0 deletions clients/inmemory/fuego.go
Original file line number Diff line number Diff line change
@@ -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
}
35 changes: 35 additions & 0 deletions clients/inmemory/fuego_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

5 changes: 5 additions & 0 deletions fuego/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -57,6 +61,7 @@ type entry struct {
object fuegoValue
}


func (c *cache) Clear() {
c.cache.entries = make(map[int]fuegoValue)
}
Expand Down

0 comments on commit 5ad2e52

Please sign in to comment.