diff --git a/config.yaml.sample b/config.yaml.sample index 918cb19..c25c50f 100644 --- a/config.yaml.sample +++ b/config.yaml.sample @@ -13,9 +13,9 @@ webp: # Caching Settings cache: expiration_minutes: 60 # Cache expiration time in minutes - cleanup_interval_minutes: 120 # Cache cleanup interval in minutes - cache_enabled: false + cache_enabled: true nocache_header: "X-No-Cache" # You can change the header name as needed + lru_cache: 300000 # Concurrency Settings concurrency: @@ -29,4 +29,4 @@ http_client: TLS_handshake_timeout: 10 response_header_timeout: 30 expect_continue_timeout: 1 - idle_conn_timeout: 90 + idle_conn_timeout: 90 \ No newline at end of file diff --git a/go.mod b/go.mod index a87911d..ad2eacb 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,8 @@ require ( github.com/spf13/viper v1.17.0 ) +require github.com/hashicorp/golang-lru/v2 v2.0.7 + require ( github.com/fsnotify/fsnotify v1.6.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect diff --git a/go.sum b/go.sum index 5fd28cf..aaa98bd 100644 --- a/go.sum +++ b/go.sum @@ -127,6 +127,8 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v1.0.2 h1:dV3g9Z/unq5DpblPpw+Oqcv4dU/1omnb4Ok8iPY6p1c= github.com/hashicorp/golang-lru v1.0.2/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= +github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= +github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= diff --git a/main.go b/main.go index 4732083..660f252 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,7 @@ import ( "github.com/gorilla/mux" "github.com/h2non/bimg" - lru "github.com/hashicorp/golang-lru" + "github.com/hashicorp/golang-lru/v2/expirable" "github.com/sirupsen/logrus" "github.com/spf13/viper" ) @@ -29,10 +29,10 @@ type Config struct { NearLossless int `mapstructure:"near_lossless"` } `mapstructure:"webp"` Cache struct { - ExpirationMinutes int `mapstructure:"expiration_minutes"` - CleanupIntervalMinutes int `mapstructure:"cleanup_interval_minutes"` - CacheEnabled bool `mapstructure:"cache_enabled"` - NoCacheHeader string `mapstructure:"nocache_header"` + ExpirationMinutes int `mapstructure:"expiration_minutes"` + CacheEnabled bool `mapstructure:"cache_enabled"` + NoCacheHeader string `mapstructure:"nocache_header"` + LruCache int `mapstructure:"lru_cache"` } `mapstructure:"cache"` Concurrency struct { MaxGoroutines int `mapstructure:"max_goroutines"` @@ -55,7 +55,7 @@ type ImageResult struct { var ( config Config - imgCache *lru.Cache + imgCache *expirable.LRU[string, []byte] logger *logrus.Logger httpClient *http.Client semaphore chan struct{} @@ -73,11 +73,7 @@ func init() { logrus.Fatalf("Unable to decode into struct: %v", err) } - var err error - imgCache, err = lru.New(128) // Adjust the size as needed - if err != nil { - logrus.Fatalf("Failed to create LRU cache: %v", err) - } + imgCache = expirable.NewLRU[string, []byte](config.Cache.LruCache, nil, time.Duration(config.Cache.ExpirationMinutes)*time.Minute) logger = logrus.New() logger.Out = os.Stdout