diff --git a/.env.default b/.env.default index bc2bdf1..e68cb35 100644 --- a/.env.default +++ b/.env.default @@ -13,8 +13,8 @@ HTTP_BASE_URL=/api/v1/whatsapp # HTTP_BODY_LIMIT_SIZE=8m # HTTP_GZIP_LEVEL=1 -# HTTP_CACHE_CAPACITY=1000 -# HTTP_CACHE_TTL_SECONDS=10 +# HTTP_CACHE_CAPACITY=100 +# HTTP_CACHE_TTL_SECONDS=5 # ----------------------------------- # Authentication Configuration diff --git a/.env.development b/.env.development index e41af8f..0a9fbb7 100644 --- a/.env.development +++ b/.env.development @@ -13,8 +13,8 @@ HTTP_BASE_URL=/api/v1/whatsapp # HTTP_BODY_LIMIT_SIZE=8m # HTTP_GZIP_LEVEL=1 -# HTTP_CACHE_CAPACITY=1000 -# HTTP_CACHE_TTL_SECONDS=10 +# HTTP_CACHE_CAPACITY=100 +# HTTP_CACHE_TTL_SECONDS=5 # ----------------------------------- # Authentication Configuration diff --git a/.env.production b/.env.production index 43980e2..f0920c5 100644 --- a/.env.production +++ b/.env.production @@ -13,8 +13,8 @@ HTTP_BASE_URL=/api/v1/whatsapp # HTTP_BODY_LIMIT_SIZE=8m # HTTP_GZIP_LEVEL=1 -# HTTP_CACHE_CAPACITY=1000 -# HTTP_CACHE_TTL_SECONDS=10 +# HTTP_CACHE_CAPACITY=100 +# HTTP_CACHE_TTL_SECONDS=5 # ----------------------------------- # Authentication Configuration diff --git a/cmd/main/main.go b/cmd/main/main.go index 69a5acd..c714be8 100644 --- a/cmd/main/main.go +++ b/cmd/main/main.go @@ -66,10 +66,11 @@ func main() { Limit: router.BodyLimit, })) - // Router HTTP Cache + // Router Cache e.Use(router.HttpCacheInMemory( router.CacheCapacity, - router.CacheTTLSeconds).Middleware()) + router.CacheTTLSeconds, + )) // Router RealIP e.Use(router.HttpRealIP()) diff --git a/pkg/router/cache.go b/pkg/router/cache.go index aaae745..b5859d7 100644 --- a/pkg/router/cache.go +++ b/pkg/router/cache.go @@ -3,22 +3,31 @@ package router import ( "time" + "github.com/labstack/echo/v4" + cache "github.com/SporkHubr/echo-http-cache" "github.com/SporkHubr/echo-http-cache/adapter/memory" "github.com/dimaskiddo/go-whatsapp-multidevice-rest/pkg/log" ) -func HttpCacheInMemory(cap int, ttl int) *cache.Client { - // Check if Cache Capacity is Zero or Less Than Zero +func HttpCacheInMemory(cap int, ttl int) echo.MiddlewareFunc { + // Check if Cache Capacity is Zero or Less if cap <= 0 { - // Set Default Cache Capacity to 1000 Keys + // Set Default Cache Capacity cap = 1000 } - // Create New InMemory Cache Adapter + // Check if Cache TTL is Zero or Less + if ttl <= 0 { + // Set Default Cache TTL + ttl = 5 + } + + // Create New In-Memory Cache Adapter memcache, err := memory.NewAdapter( - // Set InMemory Cache Adapter Algorithm to LRU + // Set In-Memory Cache Adapter Algorithm to LRU + // and With Desired Capacity memory.AdapterWithAlgorithm(memory.LRU), memory.AdapterWithCapacity(cap), ) @@ -28,8 +37,10 @@ func HttpCacheInMemory(cap int, ttl int) *cache.Client { return nil } - // Create New Cache Client with InMemory Adapter - client, err := cache.NewClient( + // Create New Cache + cache, err := cache.NewClient( + // Set Cache Adapter with In-Memory Cache Adapter + // and Set Cache TTL in Second(s) cache.ClientWithAdapter(memcache), cache.ClientWithTTL(time.Duration(ttl)*time.Second), ) @@ -39,6 +50,6 @@ func HttpCacheInMemory(cap int, ttl int) *cache.Client { return nil } - // Return Cache Client - return client + // Return Cache as Echo Middleware + return cache.Middleware() } diff --git a/pkg/router/router.go b/pkg/router/router.go index 8bf4f1f..bc81b5b 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -33,11 +33,11 @@ func init() { CacheCapacity, err = env.GetEnvInt("HTTP_CACHE_CAPACITY") if err != nil { - CacheCapacity = 1000 + CacheCapacity = 100 } CacheTTLSeconds, err = env.GetEnvInt("HTTP_CACHE_TTL_SECONDS") if err != nil { - CacheTTLSeconds = 10 + CacheTTLSeconds = 5 } }