Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammad362 committed Nov 29, 2023
1 parent 9ce655b commit d3f6906
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ require (
github.com/spf13/viper v1.17.0
)

require github.com/hashicorp/golang-lru/v2 v2.0.7
require (
github.com/hashicorp/golang-lru/arc/v2 v2.0.7
github.com/hashicorp/golang-lru/v2 v2.0.7
)

require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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/arc/v2 v2.0.7 h1:QxkVTxwColcduO+LP7eJO56r2hFiG8zEbfAAzRv52KQ=
github.com/hashicorp/golang-lru/arc/v2 v2.0.7/go.mod h1:Pe7gBlGdc8clY5LJ0LpJXMt5AmgmWNH1g+oFFVUHOEc=
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=
Expand Down
21 changes: 14 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

"github.com/gorilla/mux"
"github.com/h2non/bimg"
"github.com/hashicorp/golang-lru/v2/expirable"
"github.com/hashicorp/golang-lru/arc/v2"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
Expand All @@ -29,10 +29,10 @@ type Config struct {
NearLossless int `mapstructure:"near_lossless"`
} `mapstructure:"webp"`
Cache struct {
ExpirationMinutes int `mapstructure:"expiration_minutes"`
CacheEnabled bool `mapstructure:"cache_enabled"`
NoCacheHeader string `mapstructure:"nocache_header"`
LruCache int `mapstructure:"lru_cache"`
// 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"`
Expand All @@ -55,7 +55,7 @@ type ImageResult struct {

var (
config Config
imgCache *expirable.LRU[string, []byte]
imgCache *arc.ARCCache[string, []byte]
logger *logrus.Logger
httpClient *http.Client
semaphore chan struct{}
Expand All @@ -73,7 +73,11 @@ func init() {
logrus.Fatalf("Unable to decode into struct: %v", err)
}

imgCache = expirable.NewLRU[string, []byte](config.Cache.LruCache, nil, time.Duration(config.Cache.ExpirationMinutes)*time.Minute)
var err error
imgCache, err = arc.NewARC[string, []byte](config.Cache.LruCache) // Size of the cache
if err != nil {
logrus.Fatalf("Failed to create ARCCache: %v", err)
}

logger = logrus.New()
logger.Out = os.Stdout
Expand Down Expand Up @@ -150,6 +154,9 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {

logger.Info("Cache miss for URL: ", imageURL)

cacheCount := imgCache.Len()
logger.Info("Cache element count: ", cacheCount)

semaphoreWaitStart := time.Now()
logger.Info("Waiting for semaphore slot")
semaphore <- struct{}{}
Expand Down

0 comments on commit d3f6906

Please sign in to comment.