Skip to content

Commit

Permalink
add: cache for remote images (#310)
Browse files Browse the repository at this point in the history
* add: cache for remote image requests

* update: use default expiration for NoExpiration setting

* update: clean code

* fix: do not save empty etag

* fix: panic during type assertion
  • Loading branch information
HolgerHuo authored Feb 11, 2024
1 parent 12d4cc1 commit 123c96d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"ENABLE_EXTRA_PARAMS": false,
"READ_BUFFER_SIZE": 4096,
"CONCURRENCY": 262144,
"DISABLE_KEEPALIVE": false
"DISABLE_KEEPALIVE": false,
"CACHE_TTL": 259200
}
20 changes: 19 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ const (
"ENABLE_EXTRA_PARAMS": false
"READ_BUFFER_SIZE": 4096,
"CONCURRENCY": 262144,
"DISABLE_KEEPALIVE": false
"DISABLE_KEEPALIVE": false,
"CACHE_TTL": 259200,
}`
)

Expand All @@ -52,6 +53,7 @@ var (
RemoteRaw = "./remote-raw"
Metadata = "./metadata"
LocalHostAlias = "local"
RemoteCache *cache.Cache
)

type MetaFile struct {
Expand All @@ -73,6 +75,7 @@ type WebpConfig struct {
ReadBufferSize int `json:"READ_BUFFER_SIZE"`
Concurrency int `json:"CONCURRENCY"`
DisableKeepalive bool `json:"DISABLE_KEEPALIVE"`
CacheTTL int `json:"CACHE_TTL"`
}

func NewWebPConfig() *WebpConfig {
Expand All @@ -89,6 +92,7 @@ func NewWebPConfig() *WebpConfig {
ReadBufferSize: 4096,
Concurrency: 262144,
DisableKeepalive: false,
CacheTTL: 259200,
}
}

Expand Down Expand Up @@ -184,6 +188,20 @@ func LoadConfig() {
log.Warnf("WEBP_DISABLE_KEEPALIVE is not a valid boolean, using value in config.json %t", Config.DisableKeepalive)
}
}
if os.Getenv("CACHE_TTL") != "" {
cacheTTL, err := strconv.Atoi(os.Getenv("CACHE_TTL"))
if err != nil {
log.Warnf("CACHE_TTL is not a valid integer, using value in config.json %d", Config.CacheTTL)
} else {
Config.CacheTTL = cacheTTL
}
}

if Config.CacheTTL == 0 {
RemoteCache = cache.New(cache.NoExpiration, 10*time.Minute)
} else {
RemoteCache = cache.New(time.Duration(Config.CacheTTL)*time.Minute, 10*time.Minute)
}

log.Debugln("Config init complete")
log.Debugln("Config", Config)
Expand Down
1 change: 1 addition & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestLoadConfig(t *testing.T) {
assert.Equal(t, Config.ImgPath, "./pics")
assert.Equal(t, Config.ImageMap, map[string]string{})
assert.Equal(t, Config.ExhaustPath, "./exhaust")
assert.Equal(t, Config.CacheTTL, 259200)
}

func TestSwitchProxyMode(t *testing.T) {
Expand Down
24 changes: 22 additions & 2 deletions handler/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/gofiber/fiber/v2"
"github.com/h2non/filetype"
"github.com/patrickmn/go-cache"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -78,8 +79,27 @@ func downloadFile(filepath string, url string) {
func fetchRemoteImg(url string, subdir string) config.MetaFile {
// url is https://test.webp.sh/mypic/123.jpg?someother=200&somebugs=200
// How do we know if the remote img is changed? we're using hash(etag+length)
log.Infof("Remote Addr is %s, pinging for info...", url)
etag := pingURL(url)
var etag string

cacheKey := subdir+":"+helper.HashString(url)

if val, found := config.RemoteCache.Get(cacheKey); found {
if etagVal, ok := val.(string); ok {
log.Infof("Using cache for remote addr: %s", url)
etag = etagVal
} else {
config.RemoteCache.Delete(cacheKey)
}
}

if etag == "" {
log.Infof("Remote Addr is %s, pinging for info...", url)
etag = pingURL(url)
if etag != "" {
config.RemoteCache.Set(cacheKey, etag, cache.DefaultExpiration)
}
}

metadata := helper.ReadMetadata(url, etag, subdir)
localRawImagePath := path.Join(config.RemoteRaw, subdir, metadata.Id)

Expand Down

0 comments on commit 123c96d

Please sign in to comment.