Browse Source

update refactor http cache in memory module

pull/28/head v1.1.2
Dimas Restu H 4 years ago
parent
commit
5df45b7fbc
  1. 4
      .env.default
  2. 4
      .env.development
  3. 4
      .env.production
  4. 5
      cmd/main/main.go
  5. 29
      pkg/router/cache.go
  6. 4
      pkg/router/router.go

4
.env.default

@ -13,8 +13,8 @@ HTTP_BASE_URL=/api/v1/whatsapp
# HTTP_BODY_LIMIT_SIZE=8m # HTTP_BODY_LIMIT_SIZE=8m
# HTTP_GZIP_LEVEL=1 # HTTP_GZIP_LEVEL=1
# HTTP_CACHE_CAPACITY=1000
# HTTP_CACHE_TTL_SECONDS=10
# HTTP_CACHE_CAPACITY=100
# HTTP_CACHE_TTL_SECONDS=5
# ----------------------------------- # -----------------------------------
# Authentication Configuration # Authentication Configuration

4
.env.development

@ -13,8 +13,8 @@ HTTP_BASE_URL=/api/v1/whatsapp
# HTTP_BODY_LIMIT_SIZE=8m # HTTP_BODY_LIMIT_SIZE=8m
# HTTP_GZIP_LEVEL=1 # HTTP_GZIP_LEVEL=1
# HTTP_CACHE_CAPACITY=1000
# HTTP_CACHE_TTL_SECONDS=10
# HTTP_CACHE_CAPACITY=100
# HTTP_CACHE_TTL_SECONDS=5
# ----------------------------------- # -----------------------------------
# Authentication Configuration # Authentication Configuration

4
.env.production

@ -13,8 +13,8 @@ HTTP_BASE_URL=/api/v1/whatsapp
# HTTP_BODY_LIMIT_SIZE=8m # HTTP_BODY_LIMIT_SIZE=8m
# HTTP_GZIP_LEVEL=1 # HTTP_GZIP_LEVEL=1
# HTTP_CACHE_CAPACITY=1000
# HTTP_CACHE_TTL_SECONDS=10
# HTTP_CACHE_CAPACITY=100
# HTTP_CACHE_TTL_SECONDS=5
# ----------------------------------- # -----------------------------------
# Authentication Configuration # Authentication Configuration

5
cmd/main/main.go

@ -66,10 +66,11 @@ func main() {
Limit: router.BodyLimit, Limit: router.BodyLimit,
})) }))
// Router HTTP Cache
// Router Cache
e.Use(router.HttpCacheInMemory( e.Use(router.HttpCacheInMemory(
router.CacheCapacity, router.CacheCapacity,
router.CacheTTLSeconds).Middleware())
router.CacheTTLSeconds,
))
// Router RealIP // Router RealIP
e.Use(router.HttpRealIP()) e.Use(router.HttpRealIP())

29
pkg/router/cache.go

@ -3,22 +3,31 @@ package router
import ( import (
"time" "time"
"github.com/labstack/echo/v4"
cache "github.com/SporkHubr/echo-http-cache" cache "github.com/SporkHubr/echo-http-cache"
"github.com/SporkHubr/echo-http-cache/adapter/memory" "github.com/SporkHubr/echo-http-cache/adapter/memory"
"github.com/dimaskiddo/go-whatsapp-multidevice-rest/pkg/log" "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 { if cap <= 0 {
// Set Default Cache Capacity to 1000 Keys
// Set Default Cache Capacity
cap = 1000 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( 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.AdapterWithAlgorithm(memory.LRU),
memory.AdapterWithCapacity(cap), memory.AdapterWithCapacity(cap),
) )
@ -28,8 +37,10 @@ func HttpCacheInMemory(cap int, ttl int) *cache.Client {
return nil 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.ClientWithAdapter(memcache),
cache.ClientWithTTL(time.Duration(ttl)*time.Second), cache.ClientWithTTL(time.Duration(ttl)*time.Second),
) )
@ -39,6 +50,6 @@ func HttpCacheInMemory(cap int, ttl int) *cache.Client {
return nil return nil
} }
// Return Cache Client
return client
// Return Cache as Echo Middleware
return cache.Middleware()
} }

4
pkg/router/router.go

@ -33,11 +33,11 @@ func init() {
CacheCapacity, err = env.GetEnvInt("HTTP_CACHE_CAPACITY") CacheCapacity, err = env.GetEnvInt("HTTP_CACHE_CAPACITY")
if err != nil { if err != nil {
CacheCapacity = 1000
CacheCapacity = 100
} }
CacheTTLSeconds, err = env.GetEnvInt("HTTP_CACHE_TTL_SECONDS") CacheTTLSeconds, err = env.GetEnvInt("HTTP_CACHE_TTL_SECONDS")
if err != nil { if err != nil {
CacheTTLSeconds = 10
CacheTTLSeconds = 5
} }
} }
Loading…
Cancel
Save