Skip to content

Commit

Permalink
Use strings for all cache keys
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikKalkoken committed Jan 16, 2025
1 parent 4943f4f commit 04e7115
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 24 deletions.
8 changes: 4 additions & 4 deletions internal/app/externalservices.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
// Defines a cache service
type CacheService interface {
Clear()
Delete(any)
Exists(any) bool
Get(any) (any, bool)
Set(any, any, time.Duration)
Delete(string)
Exists(string) bool
Get(string) (any, bool)
Set(string, any, time.Duration)
}

type EveImageService interface {
Expand Down
16 changes: 8 additions & 8 deletions internal/app/pcache/pcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,23 @@ func (c *PCache) Clear() {
}
}

func (c *PCache) Delete(key any) {
err := c.st.CacheDelete(context.Background(), key.(string))
func (c *PCache) Delete(key string) {
err := c.st.CacheDelete(context.Background(), key)
if err != nil {
slog.Error("cache failure", "error", err)
}
}

func (c *PCache) Exists(key any) bool {
found, err := c.st.CacheExists(context.Background(), key.(string))
func (c *PCache) Exists(key string) bool {
found, err := c.st.CacheExists(context.Background(), key)
if err != nil {
slog.Error("cache failure", "error", err)
}
return found
}

func (c *PCache) Get(key any) (any, bool) {
v, err := c.st.CacheGet(context.Background(), key.(string))
func (c *PCache) Get(key string) (any, bool) {
v, err := c.st.CacheGet(context.Background(), key)
if errors.Is(err, storage.ErrNotFound) {
return nil, false
}
Expand All @@ -76,13 +76,13 @@ func (c *PCache) Get(key any) (any, bool) {
return v, true
}

func (c *PCache) Set(key, value any, timeout time.Duration) {
func (c *PCache) Set(key string, value any, timeout time.Duration) {
var expiresAt time.Time
if timeout > 0 {
expiresAt = time.Now().Add(timeout)
}
arg := storage.CacheSetParams{
Key: key.(string),
Key: key,
Value: value.([]byte),
ExpiresAt: expiresAt,
}
Expand Down
13 changes: 9 additions & 4 deletions internal/app/statuscache/statuscache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package statuscache

import (
"context"
"fmt"
"time"

"github.com/ErikKalkoken/evebuddy/internal/app"
Expand Down Expand Up @@ -64,6 +65,10 @@ type cacheKey struct {
section string
}

func (ck cacheKey) String() string {
return fmt.Sprintf("%d-%s", ck.id, ck.section)
}

type cacheValue struct {
CompletedAt time.Time
ErrorMessage string
Expand All @@ -72,7 +77,7 @@ type cacheValue struct {

func (sc *StatusCacheService) CharacterSectionGet(characterID int32, section app.CharacterSection) (app.SectionStatus, bool) {
k := cacheKey{id: characterID, section: string(section)}
x, ok := sc.cache.Get(k)
x, ok := sc.cache.Get(k.String())
if !ok {
return app.SectionStatus{}, false
}
Expand Down Expand Up @@ -164,7 +169,7 @@ func (sc *StatusCacheService) CharacterSectionSet(o *app.CharacterSectionStatus)
CompletedAt: o.CompletedAt,
StartedAt: o.StartedAt,
}
sc.cache.Set(k, v, 0)
sc.cache.Set(k.String(), v, 0)
}

// Return the name of a character by ID or an empty string if not found.
Expand All @@ -191,7 +196,7 @@ func (sc *StatusCacheService) GeneralSectionExists(section app.GeneralSection) b

func (sc *StatusCacheService) GeneralSectionGet(section app.GeneralSection) (app.SectionStatus, bool) {
k := cacheKey{id: app.GeneralSectionEntityID, section: string(section)}
x, ok := sc.cache.Get(k)
x, ok := sc.cache.Get(k.String())
if !ok {
return app.SectionStatus{}, false
}
Expand Down Expand Up @@ -233,7 +238,7 @@ func (sc *StatusCacheService) GeneralSectionSet(o *app.GeneralSectionStatus) {
CompletedAt: o.CompletedAt,
StartedAt: o.StartedAt,
}
sc.cache.Set(k, v, 0)
sc.cache.Set(k.String(), v, 0)
}

func (sc *StatusCacheService) GeneralSectionSummary() app.StatusSummary {
Expand Down
12 changes: 6 additions & 6 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,19 @@ func (c *Cache) Clear() {
}

// Delete deletes an item from the cache
func (c *Cache) Delete(key any) {
func (c *Cache) Delete(key string) {
c.items.Delete(key)
}

// Exists reports wether a key exists
func (c *Cache) Exists(key any) bool {
func (c *Cache) Exists(key string) bool {
_, ok := c.Get(key)
return ok
}

// Get returns an item if it exits and it not stale.
// It also reports whether the item was found.
func (c *Cache) Get(key any) (any, bool) {
func (c *Cache) Get(key string) (any, bool) {
value, ok := c.items.Load(key)
if !ok {
return nil, false
Expand All @@ -96,7 +96,7 @@ func (c *Cache) Get(key any) (any, bool) {
//
// If an item with the same key already exists it will be overwritten.
// An item with timeout = 0 never expires
func (c *Cache) Set(key any, value any, timeout time.Duration) {
func (c *Cache) Set(key string, value any, timeout time.Duration) {
var at time.Time
if timeout > 0 {
at = time.Now().Add(timeout)
Expand All @@ -110,9 +110,9 @@ func (c *Cache) cleanUp() {
slog.Info("cache clean up: started")
count := 0
c.items.Range(func(key, value any) bool {
_, found := c.Get(key)
_, found := c.Get(key.(string))
if !found {
c.Delete(key)
c.Delete(key.(string))
count++
}
return true
Expand Down
4 changes: 2 additions & 2 deletions internal/eveimage/eveimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ var (
// Defines a cache service
type CacheService interface {
Clear()
Get(any) (any, bool)
Set(any, any, time.Duration)
Get(string) (any, bool)
Set(string, any, time.Duration)
}

// EveImageService provides cached access to images on the Eve Online image server.
Expand Down

0 comments on commit 04e7115

Please sign in to comment.