Skip to content

Commit

Permalink
fix: cache?
Browse files Browse the repository at this point in the history
  • Loading branch information
Majorfi committed Nov 10, 2024
1 parent b99d2a4 commit 31fc81f
Show file tree
Hide file tree
Showing 32 changed files with 296 additions and 190 deletions.
54 changes: 33 additions & 21 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"
"github.com/patrickmn/go-cache"
cache "github.com/jfarleyx/go-simple-cache"
"github.com/yearn/ydaemon/common/logs"
"github.com/yearn/ydaemon/external/vaults"
)
Expand All @@ -16,38 +16,50 @@ type GetCustomVaults func(c *gin.Context) []vaults.TRotkiVaults

func CacheSimplifiedVaults(cachingStore *cache.Cache, expire time.Duration, handle GetSimplifiedVaults) gin.HandlerFunc {
return func(c *gin.Context) {
if result, found := cachingStore.Get(c.Request.URL.String()); found && result != nil && len(result.([]vaults.TSimplifiedExternalVault)) > 0 {
logs.Info(`Cache hit with`, len(result.([]vaults.TSimplifiedExternalVault)), `vaults`)
c.JSON(http.StatusOK, result)
} else {
result := handle(c)
cachingStore.Set(c.Request.URL.String(), result, expire)
logs.Info(`Cache miss with`, len(result), `vaults`)
c.JSON(http.StatusOK, result)
if result, found := cachingStore.Get(c.Request.URL.String()); found && result != nil {
vaults, ok := result.([]vaults.TSimplifiedExternalVault)
if ok && len(vaults) > 0 {
logs.Info(`Cache hit with`, len(vaults), `vaults`)
c.JSON(http.StatusOK, vaults)
return
}
}

result := handle(c)
cachingStore.Set(c.Request.URL.String(), result)
logs.Info(`Cache miss with`, len(result), `vaults`)
c.JSON(http.StatusOK, result)
}
}

func CacheLegacyVaults(cachingStore *cache.Cache, expire time.Duration, handle GetLegacyExternalVaults) gin.HandlerFunc {
return func(c *gin.Context) {
if result, found := cachingStore.Get(c.Request.URL.String()); found && result != nil && len(result.([]vaults.TExternalVault)) > 0 {
c.JSON(http.StatusOK, result)
} else {
result := handle(c)
cachingStore.Set(c.Request.URL.String(), result, expire)
c.JSON(http.StatusOK, result)
if result, found := cachingStore.Get(c.Request.URL.String()); found && result != nil {
vaults, ok := result.([]vaults.TExternalVault)
if ok && len(vaults) > 0 {
c.JSON(http.StatusOK, vaults)
return
}
}

result := handle(c)
cachingStore.Set(c.Request.URL.String(), result)
c.JSON(http.StatusOK, result)
}
}

func CacheCustomVaults(cachingStore *cache.Cache, expire time.Duration, handle GetCustomVaults) gin.HandlerFunc {
return func(c *gin.Context) {
if result, found := cachingStore.Get(c.Request.URL.String()); found && result != nil && len(result.([]vaults.TRotkiVaults)) > 0 {
c.JSON(http.StatusOK, result)
} else {
result := handle(c)
cachingStore.Set(c.Request.URL.String(), result, expire)
c.JSON(http.StatusOK, result)
if result, found := cachingStore.Get(c.Request.URL.String()); found && result != nil {
vaults, ok := result.([]vaults.TRotkiVaults)
if ok && len(vaults) > 0 {
c.JSON(http.StatusOK, vaults)
return
}
}

result := handle(c)
cachingStore.Set(c.Request.URL.String(), result)
c.JSON(http.StatusOK, result)
}
}
16 changes: 13 additions & 3 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,32 @@ import (
"github.com/gin-contrib/cors"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
"github.com/patrickmn/go-cache"
cache "github.com/jfarleyx/go-simple-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"
"github.com/yearn/ydaemon/external/utils"
"github.com/yearn/ydaemon/external/vaults"
)

var cachingStore = cache.New(1*time.Minute, 5*time.Minute)
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...")
})
}

/**************************************************************************************************
** NewRouter create the routes and setup the server
**************************************************************************************************/
func NewRouter() *gin.Engine {

gin.EnableJsonDecoderDisallowUnknownFields()
gin.SetMode(gin.ReleaseMode)
gin.DefaultWriter = nil
Expand Down
1 change: 0 additions & 1 deletion common/env/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,4 @@ func init() {
for k := range CHAINS {
SUPPORTED_CHAIN_IDS = append(SUPPORTED_CHAIN_IDS, k)
}
logs.Pretty(ETHEREUM.RpcURI)
}
3 changes: 0 additions & 3 deletions common/ethereum/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,4 @@ func Initialize() {
chain.MulticallContract.Address,
)
}
for _, chain := range env.GetChains() {
logs.Pretty(GetRPCURI(chain.ID))
}
}
11 changes: 11 additions & 0 deletions data/meta/registries/1.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@
"chainID": 1,
"blockNumber": 12528748
},
"0x0655977feb2f289a4ab78af67bab0d17aab84367": {
"address": "0x0655977feb2f289a4ab78af67bab0d17aab84367",
"registryAddress": "0x770d0d1fb036483ed4abb6d53c1c88fb277d812f",
"tokenAddress": "0xf939e0a03fb07f59a73314e73794be0e57ac1b4e",
"type": "Yearn Vault",
"kind": "Multi Strategy",
"extraProperties": {},
"version": "3.0.1",
"chainID": 1,
"blockNumber": 21087889
},
"0x082a5743aadf3d0daf750eef24652b36a68b1e9c": {
"address": "0x082a5743aadf3d0daf750eef24652b36a68b1e9c",
"registryAddress": "0xff31a1b020c868f6ea3f61eb953344920eeca3af",
Expand Down
16 changes: 8 additions & 8 deletions data/meta/strategies/1.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"lastUpdate": "2024-11-10T12:12:36.673032+01:00",
"lastUpdate": "2024-11-10T12:51:48.210428+01:00",
"version": {
"major": 0,
"minor": 3,
"patch": 150
"patch": 204
},
"shouldRefresh": false,
"strategies": {
Expand Down Expand Up @@ -1091,12 +1091,12 @@
"keepCRV": null,
"keepCRVPercent": null,
"keepCVX": null,
"lastTotalDebt": "809807048685048262277",
"lastTotalDebt": "744031722149787024688",
"lastTotalLoss": "0",
"lastTotalGain": "0",
"lastPerformanceFee": "500",
"lastReport": "1730919395",
"lastDebtRatio": "6619",
"lastDebtRatio": "6602",
"isRetired": true,
"displayName": "",
"description": "",
Expand Down Expand Up @@ -9422,11 +9422,11 @@
"keepCRV": null,
"keepCRVPercent": null,
"keepCVX": null,
"lastTotalDebt": "585235026815164510942",
"lastTotalDebt": "585673843048963538918",
"lastTotalLoss": "0",
"lastTotalGain": "37895251326398768165",
"lastTotalGain": "38659817172777998318",
"lastPerformanceFee": "0",
"lastReport": "1730153171",
"lastReport": "1731239195",
"lastDebtRatio": "8400",
"isRetired": false,
"displayName": "Convex Reinvest",
Expand Down Expand Up @@ -27788,7 +27788,7 @@
"lastTotalGain": "0",
"lastPerformanceFee": "500",
"lastReport": "1730919407",
"lastDebtRatio": "3130",
"lastDebtRatio": "3398",
"isRetired": true,
"displayName": "",
"description": "",
Expand Down
4 changes: 2 additions & 2 deletions data/meta/strategies/10.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"lastUpdate": "2024-11-10T12:12:44.359541+01:00",
"lastUpdate": "2024-11-10T12:34:11.886647+01:00",
"version": {
"major": 0,
"minor": 10,
"patch": 63
"patch": 71
},
"shouldRefresh": false,
"strategies": {
Expand Down
4 changes: 2 additions & 2 deletions data/meta/strategies/100.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"lastUpdate": "2024-11-10T12:12:05.918298+01:00",
"lastUpdate": "2024-11-10T12:33:54.516096+01:00",
"version": {
"major": 0,
"minor": 3,
"patch": 39
"patch": 41
},
"shouldRefresh": false,
"strategies": {
Expand Down
4 changes: 2 additions & 2 deletions data/meta/strategies/137.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"lastUpdate": "2024-11-10T12:12:09.048075+01:00",
"lastUpdate": "2024-11-10T12:34:03.272676+01:00",
"version": {
"major": 0,
"minor": 1,
"patch": 119
"patch": 183
},
"shouldRefresh": false,
"strategies": {
Expand Down
6 changes: 3 additions & 3 deletions data/meta/strategies/250.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"lastUpdate": "2024-11-10T12:12:05.826524+01:00",
"lastUpdate": "2024-11-10T12:33:54.57111+01:00",
"version": {
"major": 0,
"minor": 3,
"patch": 64
"patch": 66
},
"shouldRefresh": false,
"strategies": {
Expand Down Expand Up @@ -1095,7 +1095,7 @@
"keepCRV": null,
"keepCRVPercent": null,
"keepCVX": null,
"lastTotalDebt": "82000645286457266553631",
"lastTotalDebt": "81999148789359057686811",
"lastTotalLoss": "0",
"lastTotalGain": "40948193788906764933274",
"lastPerformanceFee": "0",
Expand Down
6 changes: 3 additions & 3 deletions data/meta/strategies/42161.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"lastUpdate": "2024-11-10T12:12:20.364637+01:00",
"lastUpdate": "2024-11-10T12:34:12.201877+01:00",
"version": {
"major": 0,
"minor": 33,
"patch": 1110
"patch": 1242
},
"shouldRefresh": false,
"strategies": {
Expand Down Expand Up @@ -363,7 +363,7 @@
"lastTotalGain": "0",
"lastPerformanceFee": "2000",
"lastReport": "1730719438",
"lastDebtRatio": "10000",
"lastDebtRatio": "9998",
"isRetired": true,
"displayName": "",
"description": "",
Expand Down
4 changes: 2 additions & 2 deletions data/meta/strategies/8453.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"lastUpdate": "2024-11-10T12:12:09.798118+01:00",
"lastUpdate": "2024-11-10T12:33:56.000967+01:00",
"version": {
"major": 0,
"minor": 1,
"patch": 30
"patch": 44
},
"shouldRefresh": false,
"strategies": {
Expand Down
2 changes: 1 addition & 1 deletion data/meta/tokens/1.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"lastUpdate": "2024-11-10T12:12:08.327317+01:00",
"lastUpdate": "2024-11-10T12:51:10.612472+01:00",
"version": {
"major": 0,
"minor": 73,
Expand Down
2 changes: 1 addition & 1 deletion data/meta/tokens/10.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"lastUpdate": "2024-11-10T12:12:43.829107+01:00",
"lastUpdate": "2024-11-10T12:34:10.828542+01:00",
"version": {
"major": 0,
"minor": 16,
Expand Down
2 changes: 1 addition & 1 deletion data/meta/tokens/100.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"lastUpdate": "2024-11-10T12:12:05.519464+01:00",
"lastUpdate": "2024-11-10T12:33:54.106939+01:00",
"version": {
"major": 0,
"minor": 4,
Expand Down
2 changes: 1 addition & 1 deletion data/meta/tokens/137.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"lastUpdate": "2024-11-10T12:12:05.964453+01:00",
"lastUpdate": "2024-11-10T12:33:57.507668+01:00",
"version": {
"major": 0,
"minor": 2,
Expand Down
2 changes: 1 addition & 1 deletion data/meta/tokens/250.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"lastUpdate": "2024-11-10T12:12:05.74833+01:00",
"lastUpdate": "2024-11-10T12:33:54.470169+01:00",
"version": {
"major": 0,
"minor": 2,
Expand Down
2 changes: 1 addition & 1 deletion data/meta/tokens/42161.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"lastUpdate": "2024-11-10T12:12:19.891956+01:00",
"lastUpdate": "2024-11-10T12:33:56.529917+01:00",
"version": {
"major": 0,
"minor": 37,
Expand Down
2 changes: 1 addition & 1 deletion data/meta/tokens/8453.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"lastUpdate": "2024-11-10T12:12:05.728832+01:00",
"lastUpdate": "2024-11-10T12:33:54.330478+01:00",
"version": {
"major": 0,
"minor": 11,
Expand Down
Loading

0 comments on commit 31fc81f

Please sign in to comment.