mirror of
https://github.com/darmiel/yaxc.git
synced 2025-09-08 15:34:29 +08:00
made cache debug optional
This commit is contained in:
parent
8631f67fcb
commit
31c3837fbe
6 changed files with 83 additions and 44 deletions
|
@ -1,20 +1,10 @@
|
||||||
package bcache
|
package bcache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/darmiel/yaxc/internal/common"
|
|
||||||
"github.com/muesli/termenv"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"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 {
|
type node struct {
|
||||||
expires nodeExpiration
|
expires nodeExpiration
|
||||||
value interface{}
|
value interface{}
|
||||||
|
@ -41,17 +31,7 @@ func NewCache(defaultExpiration, cleanerInterval time.Duration) *Cache {
|
||||||
|
|
||||||
func (c *Cache) Set(key string, value interface{}, expiration time.Duration) {
|
func (c *Cache) Set(key string, value interface{}, expiration time.Duration) {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
|
printDebugSet(key, value)
|
||||||
// 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))
|
|
||||||
}
|
|
||||||
|
|
||||||
c.values[key] = &node{
|
c.values[key] = &node{
|
||||||
expires: c.expiration(expiration),
|
expires: c.expiration(expiration),
|
||||||
value: value,
|
value: value,
|
||||||
|
@ -63,16 +43,7 @@ func (c *Cache) Get(key string) (interface{}, bool) {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
if v, o := c.values[key]; o && v != nil {
|
if v, o := c.values[key]; o && v != nil {
|
||||||
if !v.expires.IsExpired() {
|
if !v.expires.IsExpired() {
|
||||||
|
printDebugGet(key, v.value)
|
||||||
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))
|
|
||||||
}
|
|
||||||
|
|
||||||
c.mu.Unlock()
|
c.mu.Unlock()
|
||||||
return v.value, true
|
return v.value, true
|
||||||
}
|
}
|
||||||
|
|
52
internal/bcache/debug.go
Normal file
52
internal/bcache/debug.go
Normal 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")))
|
||||||
|
}
|
|
@ -1,9 +1,6 @@
|
||||||
package bcache
|
package bcache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/darmiel/yaxc/internal/common"
|
|
||||||
"github.com/muesli/termenv"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -13,9 +10,7 @@ func (c *Cache) janitorService() {
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
time.Sleep(c.cleanerInterval)
|
time.Sleep(c.cleanerInterval)
|
||||||
fmt.Println(prefix,
|
printDebugJanitorStart()
|
||||||
termenv.String("JANITOR").Foreground(common.Profile().Color("#A8CC8C")),
|
|
||||||
"Starting ...")
|
|
||||||
c.janitor()
|
c.janitor()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,9 +20,7 @@ func (c *Cache) janitor() {
|
||||||
for k, v := range c.values {
|
for k, v := range c.values {
|
||||||
// nil node
|
// nil node
|
||||||
if v == nil || v.expires.IsExpired() {
|
if v == nil || v.expires.IsExpired() {
|
||||||
fmt.Println(prefix,
|
printDebugJanitorDelete(k)
|
||||||
termenv.String("JANITOR").Foreground(common.Profile().Color("#A8CC8C")),
|
|
||||||
"Deleting", termenv.String(k).Foreground(common.Profile().Color("#A8CC8C")))
|
|
||||||
delete(c.values, k)
|
delete(c.values, k)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,14 @@ func StyleUpdate() termenv.Style {
|
||||||
return termenv.String("UPDT |").Foreground(Profile().Color("0")).Background(Profile().Color("#D290E4"))
|
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 {
|
func WordClient() termenv.Style {
|
||||||
return termenv.String("Client").Foreground(Profile().Color("#DBAB79"))
|
return termenv.String("Client").Foreground(Profile().Color("#DBAB79"))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/darmiel/yaxc/internal/common"
|
"github.com/darmiel/yaxc/internal/common"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/muesli/termenv"
|
||||||
"strings"
|
"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 == "" {
|
if res == "" {
|
||||||
ctx.Status(404)
|
ctx.Status(404)
|
||||||
|
@ -47,7 +53,11 @@ func (s *yAxCServer) handleGetHashAnywhere(ctx *fiber.Ctx) (err error) {
|
||||||
return
|
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 == "" {
|
if res == "" {
|
||||||
ctx.Status(404)
|
ctx.Status(404)
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/darmiel/yaxc/internal/common"
|
"github.com/darmiel/yaxc/internal/common"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/muesli/termenv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -13,7 +14,6 @@ var errEncryptionNotEnabled = errors.New("encryption not enabled")
|
||||||
|
|
||||||
func (s *yAxCServer) handlePostAnywhere(ctx *fiber.Ctx) (err error) {
|
func (s *yAxCServer) handlePostAnywhere(ctx *fiber.Ctx) (err error) {
|
||||||
path := strings.TrimSpace(ctx.Params("anywhere"))
|
path := strings.TrimSpace(ctx.Params("anywhere"))
|
||||||
log.Debug("requested path", path)
|
|
||||||
return s.setAnywhereWithHash(ctx, 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))
|
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)
|
return ctx.Status(200).SendString(content)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue