Skip to content

Commit

Permalink
feat: output diff in cache
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Gleich <[email protected]>
  • Loading branch information
gleich committed Feb 16, 2025
1 parent d94928e commit 4b5c9e3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module go.mattglei.ch/lcp-2
go 1.24

require (
github.com/aymanbagabas/go-udiff v0.2.0
github.com/buckket/go-blurhash v1.1.0
github.com/caarlos0/env/v11 v11.3.1
github.com/joho/godotenv v1.5.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/aymanbagabas/go-udiff v0.2.0 h1:TK0fH4MteXUDspT88n8CKzvK0X9O2xu9yQjWpi6yML8=
github.com/aymanbagabas/go-udiff v0.2.0/go.mod h1:RE4Ex0qsGkTAJoQdQQCA0uG+nAzJO/pI/QwceO5fgrA=
github.com/buckket/go-blurhash v1.1.0 h1:X5M6r0LIvwdvKiUtiNcRL2YlmOfMzYobI3VCKCZc9Do=
github.com/buckket/go-blurhash v1.1.0/go.mod h1:aT2iqo5W9vu9GpyoLErKfTHwgODsZp3bQfXjXJUxNb8=
github.com/caarlos0/env/v11 v11.3.1 h1:cArPWC15hWmEt+gWk7YBi7lEXTXCvpaSdCiZE2X5mCA=
Expand Down
12 changes: 9 additions & 3 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"sync"
"time"

"github.com/aymanbagabas/go-udiff"
"go.mattglei.ch/lcp-2/internal/apis"
"go.mattglei.ch/lcp-2/internal/auth"
"go.mattglei.ch/lcp-2/internal/secrets"
Expand Down Expand Up @@ -59,20 +60,25 @@ func (c *Cache[T]) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

func (c *Cache[T]) Update(data T) {
const jsonIndent = " "
c.DataMutex.RLock()
old, err := json.Marshal(c.Data)
oldBin, err := json.MarshalIndent(c.Data, "", jsonIndent)
if err != nil {
timber.Error(err, "failed to json marshal old data")
return
}
c.DataMutex.RUnlock()
new, err := json.Marshal(data)
newBin, err := json.MarshalIndent(data, "", jsonIndent)
if err != nil {
timber.Error(err, "failed to json marshal new data")
return
}

if string(old) != string(new) && string(new) != "null" && strings.Trim(string(new), " ") != "" {
old := string(oldBin)
new := string(newBin)
if old != new && new != "null" && strings.Trim(string(newBin), " ") != "" {
unified := udiff.Unified("old cache", "new cache", old, new)
timber.Info(fmt.Sprintf("\n%s", unified))
c.DataMutex.Lock()
c.Data = data
c.Updated = time.Now()
Expand Down

0 comments on commit 4b5c9e3

Please sign in to comment.