fix(cache): pretty-print cache envelope
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
fbf410e630
commit
248f542b08
2 changed files with 8 additions and 4 deletions
8
cache.go
8
cache.go
|
|
@ -166,12 +166,12 @@ func (c *Cache) Set(key string, data any) error {
|
|||
ExpiresAt: time.Now().Add(ttl),
|
||||
}
|
||||
|
||||
entryResult := core.JSONMarshal(entry)
|
||||
if !entryResult.OK {
|
||||
return core.E("cache.Set", "failed to marshal cache entry", entryResult.Value.(error))
|
||||
entryBytes, err := json.MarshalIndent(entry, "", " ")
|
||||
if err != nil {
|
||||
return core.E("cache.Set", "failed to marshal cache entry", err)
|
||||
}
|
||||
|
||||
if err := c.medium.Write(path, string(entryResult.Value.([]byte))); err != nil {
|
||||
if err := c.medium.Write(path, string(entryBytes)); err != nil {
|
||||
return core.E("cache.Set", "failed to write cache file", err)
|
||||
}
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
package cache_test
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
|
@ -60,6 +61,9 @@ func TestCache_New_Good(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatalf("Read failed: %v", err)
|
||||
}
|
||||
if !strings.Contains(raw, "\n \"data\":") {
|
||||
t.Fatalf("expected pretty-printed cache entry, got %q", raw)
|
||||
}
|
||||
|
||||
entry := readEntry(t, raw)
|
||||
ttl := entry.ExpiresAt.Sub(entry.CachedAt)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue