Skip to content

Commit

Permalink
Merge pull request #10 from krakend/lru_http_cache
Browse files Browse the repository at this point in the history
Add LRU cache option
  • Loading branch information
kpacha authored Jan 22, 2025
2 parents c120d80 + 410e802 commit 6db7a58
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 85 deletions.
2 changes: 1 addition & 1 deletion .deepsource.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ name = "go"
enabled = true

[analyzers.meta]
import_root = "github.com/krakendio/krakend-cors"
import_root = "github.com/krakendio/krakend-httpcache/v2"

[[transformers]]
name = "gofmt"
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module github.com/krakendio/krakend-httpcache/v2

go 1.17
go 1.22

require (
github.com/krakend/lru v0.0.0-20250121172718-0e3a6eab620d
github.com/krakendio/httpcache v0.0.0-20221129153752-65a87a5c2bc5
github.com/luraproject/lura/v2 v2.2.3
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
github.com/krakend/lru v0.0.0-20250121172718-0e3a6eab620d h1:Kktd6hjWjY+TgL/oESBdTTVKJ7BFroS/YA2PZ1PsFLA=
github.com/krakend/lru v0.0.0-20250121172718-0e3a6eab620d/go.mod h1:VgOve4vVYbY6UkD3qHdqmq2AKwqHlp39TKMi2b2cQBI=
github.com/krakendio/flatmap v1.1.1 h1:rGBNVpBY0pMk6cLOwerVzoKY4HELnpu0xvqB231lOCQ=
github.com/krakendio/flatmap v1.1.1/go.mod h1:KBuVkiH5BcBFRa5A1HdSHDn8a8LzsyRTKZArX0vqTbo=
github.com/krakendio/httpcache v0.0.0-20221129153752-65a87a5c2bc5 h1:gB5divkol6FZK6OVGjh7odTsOY3FAESpeq9qyAVCZkc=
github.com/krakendio/httpcache v0.0.0-20221129153752-65a87a5c2bc5/go.mod h1:VWTP3MZHr/W9OauBMrOQ2bD1rIjDetAQo5JbXCezYKE=
github.com/luraproject/lura/v2 v2.0.5 h1:Mc4uj37s7mv6qRLy+Uo983CiaITPSVJYooeUilbiD+k=
github.com/luraproject/lura/v2 v2.0.5/go.mod h1:r2N4j89Snm1j+Y9CCa9cYR1T2ETRL0E4y9P+DgymqX4=
github.com/luraproject/lura/v2 v2.2.3 h1:zePcqPP7XXjDKmWht8hJbFoPVNtBgdV3I6F2PyTFaB8=
github.com/luraproject/lura/v2 v2.2.3/go.mod h1:Outo/8O5zqa2itP1wD/mdD5nskjo09R74EbMVgr1L98=
github.com/valyala/fastrand v1.1.0 h1:f+5HkLW4rsgzdNoleUOB69hyT9IlD2ZQh9GyDMfb5G8=
Expand Down
52 changes: 41 additions & 11 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"net/http"

"github.com/krakend/lru"
"github.com/krakendio/httpcache"

"github.com/luraproject/lura/v2/config"
Expand All @@ -32,19 +33,24 @@ func NewHTTPClient(cfg *config.Backend, nextF client.HTTPClientFactory) client.H
return nextF
}

var cache Cache

if b, err := json.Marshal(raw); err == nil {
var opts options
if err := json.Unmarshal(b, &opts); err == nil && opts.Shared {
cache = globalCache
}
b, err := json.Marshal(raw)
if err != nil {
return defaultClient(nextF)
}

if cache == nil {
cache = httpcache.NewMemoryCache()
var opts options
if err := json.Unmarshal(b, &opts); err != nil {
return defaultClient(nextF)
}

return getCachedClient(nextF, selectCache(opts))
}

func defaultClient(nextF client.HTTPClientFactory) client.HTTPClientFactory {
return getCachedClient(nextF, httpcache.NewMemoryCache())
}

func getCachedClient(nextF client.HTTPClientFactory, cache Cache) client.HTTPClientFactory {
return func(ctx context.Context) *http.Client {
httpClient := nextF(ctx)
return &http.Client{
Expand All @@ -59,8 +65,32 @@ func NewHTTPClient(cfg *config.Backend, nextF client.HTTPClientFactory) client.H
}
}

var globalCache = httpcache.NewMemoryCache()
func selectCache(opts options) Cache {
if opts.MaxSize == 0 || opts.MaxItems == 0 {
if opts.Shared {
return globalCache
}
return httpcache.NewMemoryCache()
}

if !opts.Shared {
cache, _ := lru.NewLruCache(opts.MaxSize, opts.MaxItems)
return cache
}

if globalLruCache == nil {
globalLruCache, _ = lru.NewLruCache(opts.MaxSize, opts.MaxItems)
}
return globalLruCache
}

var (
globalLruCache Cache
globalCache = httpcache.NewMemoryCache()
)

type options struct {
Shared bool `json:"shared"`
Shared bool `json:"shared"`
MaxSize uint64 `json:"max_size"`
MaxItems uint64 `json:"max_items"`
}
Loading

0 comments on commit 6db7a58

Please sign in to comment.