Skip to content

Commit

Permalink
chores: caching lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Majorfi committed Nov 10, 2024
1 parent 0e53fea commit 5bb7404
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 13 deletions.
8 changes: 4 additions & 4 deletions cmd/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"

"github.com/gin-gonic/gin"
cache "github.com/jfarleyx/go-simple-cache"
"github.com/patrickmn/go-cache"
"github.com/yearn/ydaemon/common/logs"
"github.com/yearn/ydaemon/external/vaults"
)
Expand All @@ -26,7 +26,7 @@ func CacheSimplifiedVaults(cachingStore *cache.Cache, expire time.Duration, hand
}

result := handle(c)
cachingStore.Set(c.Request.URL.String(), result)
cachingStore.Set(c.Request.URL.String(), result, expire)
logs.Info(`Cache miss with`, len(result), `vaults`)
c.JSON(http.StatusOK, result)
}
Expand All @@ -43,7 +43,7 @@ func CacheLegacyVaults(cachingStore *cache.Cache, expire time.Duration, handle G
}

result := handle(c)
cachingStore.Set(c.Request.URL.String(), result)
cachingStore.Set(c.Request.URL.String(), result, expire)
c.JSON(http.StatusOK, result)
}
}
Expand All @@ -59,7 +59,7 @@ func CacheCustomVaults(cachingStore *cache.Cache, expire time.Duration, handle G
}

result := handle(c)
cachingStore.Set(c.Request.URL.String(), result)
cachingStore.Set(c.Request.URL.String(), result, expire)
c.JSON(http.StatusOK, result)
}
}
11 changes: 2 additions & 9 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import (
"github.com/gin-contrib/cors"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
cache "github.com/jfarleyx/go-simple-cache"
"github.com/patrickmn/go-cache"
"github.com/yearn/ydaemon/common/helpers"
"github.com/yearn/ydaemon/common/logs"
"github.com/yearn/ydaemon/external/prices"
"github.com/yearn/ydaemon/external/strategies"
"github.com/yearn/ydaemon/external/tokens"
Expand All @@ -20,13 +19,7 @@ import (
var cachingStore *cache.Cache

func init() {
// Create cache with 1 minute default expiration
cachingStore = cache.New(1 * time.Minute)

// Add callback for cache expiration
cachingStore.OnExpired(func() {
logs.Info("Cache expired, clearing...")
})
cachingStore = cache.New(1*time.Minute, 5*time.Minute)
}

/**************************************************************************************************
Expand Down

0 comments on commit 5bb7404

Please sign in to comment.