diff --git a/internal/fcache/cache.go b/internal/bcache/cache.go similarity index 64% rename from internal/fcache/cache.go rename to internal/bcache/cache.go index 6a04a09..ccd9844 100644 --- a/internal/fcache/cache.go +++ b/internal/bcache/cache.go @@ -1,4 +1,4 @@ -package fcache +package bcache import ( "fmt" @@ -17,21 +17,21 @@ func init() { type node struct { expires nodeExpiration - value interface{} + value []byte } type Cache struct { - mu sync.Mutex - val map[string]*node - de time.Duration - cleanerInterval time.Duration + mu sync.Mutex + values map[string]*node + defaultExpiration time.Duration + cleanerInterval time.Duration } func NewCache(defaultExpiration, cleanerInterval time.Duration) *Cache { c := &Cache{ - val: make(map[string]*node), - de: defaultExpiration, - cleanerInterval: cleanerInterval, + values: make(map[string]*node), + defaultExpiration: defaultExpiration, + cleanerInterval: cleanerInterval, } if cleanerInterval != 0 { go c.janitorService() @@ -39,30 +39,32 @@ func NewCache(defaultExpiration, cleanerInterval time.Duration) *Cache { return c } -func (c *Cache) Set(key string, value interface{}, expiration time.Duration) { +func (c *Cache) Set(key string, value []byte, expiration time.Duration) { c.mu.Lock() // TODO: remove debug fmt.Println(prefix, + termenv.String("<-").Foreground(common.Profile().Color("#DBAB79")), "Set", termenv.String(key).Foreground(common.Profile().Color("#A8CC8C")), termenv.String("=").Foreground(common.Profile().Color("#DBAB79")), value) - c.val[key] = &node{ + c.values[key] = &node{ expires: c.expiration(expiration), value: value, } c.mu.Unlock() } -func (c *Cache) Get(key string) (interface{}, bool) { +func (c *Cache) Get(key string) ([]byte, bool) { c.mu.Lock() - if v, o := c.val[key]; o && v != nil { + if v, o := c.values[key]; o && v != nil { if !v.expires.IsExpired() { // TODO: remove debug fmt.Println(prefix, + termenv.String("->").Foreground(common.Profile().Color("#66C2CD")), "Get", termenv.String(key).Foreground(common.Profile().Color("#A8CC8C")), termenv.String("=").Foreground(common.Profile().Color("#DBAB79")), diff --git a/internal/fcache/expiration.go b/internal/bcache/expiration.go similarity index 91% rename from internal/fcache/expiration.go rename to internal/bcache/expiration.go index 4d77f79..92bd2be 100644 --- a/internal/fcache/expiration.go +++ b/internal/bcache/expiration.go @@ -1,4 +1,4 @@ -package fcache +package bcache import "time" @@ -21,7 +21,7 @@ func (c *Cache) expiration(d time.Duration) nodeExpiration { return 0 } if d == ExpirationDefault { - d = c.de + d = c.defaultExpiration } return nodeExpiration(time.Now().Add(d).Unix()) } diff --git a/internal/fcache/janitor.go b/internal/bcache/janitor.go similarity index 90% rename from internal/fcache/janitor.go rename to internal/bcache/janitor.go index 0b98b88..d53f9c1 100644 --- a/internal/fcache/janitor.go +++ b/internal/bcache/janitor.go @@ -1,4 +1,4 @@ -package fcache +package bcache import ( "fmt" @@ -22,13 +22,13 @@ func (c *Cache) janitorService() { func (c *Cache) janitor() { c.mu.Lock() - for k, v := range c.val { + for k, v := range c.values { // nil node if v == nil || v.expires.IsExpired() { fmt.Println(prefix, termenv.String("JANITOR").Foreground(common.Profile().Color("#A8CC8C")), "Deleting", termenv.String(k).Foreground(common.Profile().Color("#A8CC8C"))) - delete(c.val, k) + delete(c.values, k) } } c.mu.Unlock() diff --git a/internal/server/backend_cache.go b/internal/server/backend_cache.go index 5816daf..52d550f 100644 --- a/internal/server/backend_cache.go +++ b/internal/server/backend_cache.go @@ -1,12 +1,12 @@ package server import ( - "github.com/darmiel/yaxc/internal/fcache" + "github.com/darmiel/yaxc/internal/bcache" "time" ) type CacheBackend struct { - cache *fcache.Cache + cache *bcache.Cache errCast error } @@ -19,21 +19,18 @@ func (b *CacheBackend) GetHash(key string) (res string, err error) { } func (b *CacheBackend) Set(key, value string, ttl time.Duration) error { - b.cache.Set("val::"+key, value, ttl) + b.cache.Set("val::"+key, []byte(value), ttl) return nil } func (b *CacheBackend) SetHash(key, value string, ttl time.Duration) error { - b.cache.Set("hash::"+key, value, ttl) + b.cache.Set("hash::"+key, []byte(value), ttl) return nil } func (b *CacheBackend) get(key string) (res string, err error) { if v, ok := b.cache.Get(key); ok { - if s, ok := v.(string); ok { - return s, nil - } - return "", b.errCast + res = string(v) } - return "", nil + return } diff --git a/internal/server/request_get.go b/internal/server/request_get.go index b154949..e484e03 100644 --- a/internal/server/request_get.go +++ b/internal/server/request_get.go @@ -30,7 +30,7 @@ func (s *yAxCServer) handleGetAnywhere(ctx *fiber.Ctx) (err error) { } } - log.Warning(ctx.IP(), "requested VALUE", path) + // log.Warning(ctx.IP(), "requested VALUE", path) if res == "" { ctx.Status(404) @@ -47,7 +47,7 @@ func (s *yAxCServer) handleGetHashAnywhere(ctx *fiber.Ctx) (err error) { return } - log.Warning(ctx.IP(), "requested HASH", path, "with result", res, "::", res[:4]) + // log.Warning(ctx.IP(), "requested HASH", path, "with result", res, "::", res[:4]) if res == "" { ctx.Status(404) diff --git a/internal/server/request_post.go b/internal/server/request_post.go index 7d562f0..fcdc3ab 100644 --- a/internal/server/request_post.go +++ b/internal/server/request_post.go @@ -81,7 +81,7 @@ func (s *yAxCServer) setAnywhereWithHash(ctx *fiber.Ctx, path, hash string) (err fmt.Sprintf("ERROR (Val): %v\nERROR (Hsh): %v", errVal, errHsh)) } - log.Debug(ctx.IP(), "updated", path, "with hash", hash) + //log.Debug(ctx.IP(), "updated", path, "with hash", hash) return ctx.Status(200).SendString(content) } diff --git a/internal/server/type.go b/internal/server/type.go index cddd681..6557cf8 100644 --- a/internal/server/type.go +++ b/internal/server/type.go @@ -3,7 +3,7 @@ package server import ( "context" "errors" - "github.com/darmiel/yaxc/internal/fcache" + "github.com/darmiel/yaxc/internal/bcache" "github.com/go-redis/redis/v8" "github.com/gofiber/fiber/v2" "github.com/op/go-logging" @@ -60,7 +60,7 @@ func NewServer(cfg *YAxCConfig) (s *yAxCServer) { if s.RedisAddress == "" { // use cache backend s.Backend = &CacheBackend{ - cache: fcache.NewCache(s.DefaultTTL, 10*time.Second), + cache: bcache.NewCache(s.DefaultTTL, 10*time.Second), errCast: errors.New("not a string"), } } else {