made cache debug optional

This commit is contained in:
darmiel 2021-04-05 11:29:01 +02:00
parent 8631f67fcb
commit 31c3837fbe
6 changed files with 83 additions and 44 deletions

View file

@ -1,20 +1,10 @@
package bcache
import (
"fmt"
"github.com/darmiel/yaxc/internal/common"
"github.com/muesli/termenv"
"sync"
"time"
)
var prefix termenv.Style
func init() {
p := common.Profile()
prefix = termenv.String("CCHE").Foreground(p.Color("0")).Background(p.Color("#D290E4"))
}
type node struct {
expires nodeExpiration
value interface{}
@ -41,17 +31,7 @@ func NewCache(defaultExpiration, cleanerInterval time.Duration) *Cache {
func (c *Cache) Set(key string, value interface{}, expiration time.Duration) {
c.mu.Lock()
// TODO: remove debug
if b, o := value.([]byte); o {
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")),
common.PrettyLimit(string(b), 48))
}
printDebugSet(key, value)
c.values[key] = &node{
expires: c.expiration(expiration),
value: value,
@ -63,16 +43,7 @@ func (c *Cache) Get(key string) (interface{}, bool) {
c.mu.Lock()
if v, o := c.values[key]; o && v != nil {
if !v.expires.IsExpired() {
if b, o := v.value.([]byte); o {
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")),
common.PrettyLimit(string(b), 48))
}
printDebugGet(key, v.value)
c.mu.Unlock()
return v.value, true
}

52
internal/bcache/debug.go Normal file
View file

@ -0,0 +1,52 @@
package bcache
import (
"fmt"
"github.com/darmiel/yaxc/internal/common"
"github.com/muesli/termenv"
)
const debugEnabled = false
func printDebugSet(key string, value interface{}) {
if !debugEnabled {
return
}
if b, o := value.([]byte); o {
fmt.Println(common.StyleCache(),
termenv.String("<-").Foreground(common.Profile().Color("#DBAB79")),
"Set",
termenv.String(key).Foreground(common.Profile().Color("#A8CC8C")),
termenv.String("=").Foreground(common.Profile().Color("#DBAB79")),
common.PrettyLimit(string(b), 48))
}
}
func printDebugGet(key string, value interface{}) {
if !debugEnabled {
return
}
if b, o := value.([]byte); o {
fmt.Println(common.StyleCache(),
termenv.String("->").Foreground(common.Profile().Color("#66C2CD")),
"Get",
termenv.String(key).Foreground(common.Profile().Color("#A8CC8C")),
termenv.String("=").Foreground(common.Profile().Color("#DBAB79")),
common.PrettyLimit(string(b), 48))
}
}
func printDebugJanitorStart() {
if !debugEnabled {
return
}
fmt.Println(common.StyleCache(),
termenv.String("JANITOR").Foreground(common.Profile().Color("#A8CC8C")),
"Starting ...")
}
func printDebugJanitorDelete(k string) {
if !debugEnabled {
return
}
fmt.Println(common.StyleCache(),
termenv.String("JANITOR").Foreground(common.Profile().Color("#A8CC8C")),
"Deleting", termenv.String(k).Foreground(common.Profile().Color("#A8CC8C")))
}

View file

@ -1,9 +1,6 @@
package bcache
import (
"fmt"
"github.com/darmiel/yaxc/internal/common"
"github.com/muesli/termenv"
"time"
)
@ -13,9 +10,7 @@ func (c *Cache) janitorService() {
}
for {
time.Sleep(c.cleanerInterval)
fmt.Println(prefix,
termenv.String("JANITOR").Foreground(common.Profile().Color("#A8CC8C")),
"Starting ...")
printDebugJanitorStart()
c.janitor()
}
}
@ -25,9 +20,7 @@ func (c *Cache) janitor() {
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")))
printDebugJanitorDelete(k)
delete(c.values, k)
}
}

View file

@ -31,6 +31,14 @@ func StyleUpdate() termenv.Style {
return termenv.String("UPDT |").Foreground(Profile().Color("0")).Background(Profile().Color("#D290E4"))
}
func StyleServe() termenv.Style {
return termenv.String("SRVE |").Foreground(Profile().Color("0")).Background(Profile().Color("#66C2CD"))
}
func StyleCache() termenv.Style {
return termenv.String("CCHE |").Foreground(Profile().Color("0")).Background(Profile().Color("#D290E4"))
}
func WordClient() termenv.Style {
return termenv.String("Client").Foreground(Profile().Color("#DBAB79"))
}

View file

@ -1,8 +1,10 @@
package server
import (
"fmt"
"github.com/darmiel/yaxc/internal/common"
"github.com/gofiber/fiber/v2"
"github.com/muesli/termenv"
"strings"
)
@ -30,7 +32,11 @@ func (s *yAxCServer) handleGetAnywhere(ctx *fiber.Ctx) (err error) {
}
}
// log.Warning(ctx.IP(), "requested VALUE", path)
fmt.Println(common.StyleServe(),
termenv.String(ctx.IP()).Foreground(common.Profile().Color("#DBAB79")),
"requested",
termenv.String("value").Foreground(common.Profile().Color("#A8CC8C")),
termenv.String(path).Foreground(common.Profile().Color("#D290E4")))
if res == "" {
ctx.Status(404)
@ -47,7 +53,11 @@ func (s *yAxCServer) handleGetHashAnywhere(ctx *fiber.Ctx) (err error) {
return
}
// log.Warning(ctx.IP(), "requested HASH", path, "with result", res, "::", res[:4])
fmt.Println(common.StyleServe(),
termenv.String(ctx.IP()).Foreground(common.Profile().Color("#DBAB79")),
"requested",
termenv.String("hash").Foreground(common.Profile().Color("#E88388")),
termenv.String(path).Foreground(common.Profile().Color("#D290E4")))
if res == "" {
ctx.Status(404)

View file

@ -5,6 +5,7 @@ import (
"fmt"
"github.com/darmiel/yaxc/internal/common"
"github.com/gofiber/fiber/v2"
"github.com/muesli/termenv"
"strings"
"time"
)
@ -13,7 +14,6 @@ var errEncryptionNotEnabled = errors.New("encryption not enabled")
func (s *yAxCServer) handlePostAnywhere(ctx *fiber.Ctx) (err error) {
path := strings.TrimSpace(ctx.Params("anywhere"))
log.Debug("requested path", path)
return s.setAnywhereWithHash(ctx, path, "")
}
@ -81,7 +81,12 @@ 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)
fmt.Println(common.StyleServe(),
termenv.String(ctx.IP()).Foreground(common.Profile().Color("#DBAB79")),
"updated",
termenv.String(path).Foreground(common.Profile().Color("#D290E4")),
"with hash",
termenv.String(hash).Foreground(common.Profile().Color("#71BEF2")))
return ctx.Status(200).SendString(content)
}