Skip to content

Commit 123c96d

Browse files
authored
add: cache for remote images (#310)
* 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
1 parent 12d4cc1 commit 123c96d

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
"ENABLE_EXTRA_PARAMS": false,
1111
"READ_BUFFER_SIZE": 4096,
1212
"CONCURRENCY": 262144,
13-
"DISABLE_KEEPALIVE": false
13+
"DISABLE_KEEPALIVE": false,
14+
"CACHE_TTL": 259200
1415
}

config/config.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ const (
3333
"ENABLE_EXTRA_PARAMS": false
3434
"READ_BUFFER_SIZE": 4096,
3535
"CONCURRENCY": 262144,
36-
"DISABLE_KEEPALIVE": false
36+
"DISABLE_KEEPALIVE": false,
37+
"CACHE_TTL": 259200,
3738
}`
3839
)
3940

@@ -52,6 +53,7 @@ var (
5253
RemoteRaw = "./remote-raw"
5354
Metadata = "./metadata"
5455
LocalHostAlias = "local"
56+
RemoteCache *cache.Cache
5557
)
5658

5759
type MetaFile struct {
@@ -73,6 +75,7 @@ type WebpConfig struct {
7375
ReadBufferSize int `json:"READ_BUFFER_SIZE"`
7476
Concurrency int `json:"CONCURRENCY"`
7577
DisableKeepalive bool `json:"DISABLE_KEEPALIVE"`
78+
CacheTTL int `json:"CACHE_TTL"`
7679
}
7780

7881
func NewWebPConfig() *WebpConfig {
@@ -89,6 +92,7 @@ func NewWebPConfig() *WebpConfig {
8992
ReadBufferSize: 4096,
9093
Concurrency: 262144,
9194
DisableKeepalive: false,
95+
CacheTTL: 259200,
9296
}
9397
}
9498

@@ -184,6 +188,20 @@ func LoadConfig() {
184188
log.Warnf("WEBP_DISABLE_KEEPALIVE is not a valid boolean, using value in config.json %t", Config.DisableKeepalive)
185189
}
186190
}
191+
if os.Getenv("CACHE_TTL") != "" {
192+
cacheTTL, err := strconv.Atoi(os.Getenv("CACHE_TTL"))
193+
if err != nil {
194+
log.Warnf("CACHE_TTL is not a valid integer, using value in config.json %d", Config.CacheTTL)
195+
} else {
196+
Config.CacheTTL = cacheTTL
197+
}
198+
}
199+
200+
if Config.CacheTTL == 0 {
201+
RemoteCache = cache.New(cache.NoExpiration, 10*time.Minute)
202+
} else {
203+
RemoteCache = cache.New(time.Duration(Config.CacheTTL)*time.Minute, 10*time.Minute)
204+
}
187205

188206
log.Debugln("Config init complete")
189207
log.Debugln("Config", Config)

config/config_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func TestLoadConfig(t *testing.T) {
2121
assert.Equal(t, Config.ImgPath, "./pics")
2222
assert.Equal(t, Config.ImageMap, map[string]string{})
2323
assert.Equal(t, Config.ExhaustPath, "./exhaust")
24+
assert.Equal(t, Config.CacheTTL, 259200)
2425
}
2526

2627
func TestSwitchProxyMode(t *testing.T) {

handler/remote.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/gofiber/fiber/v2"
1414
"github.com/h2non/filetype"
15+
"github.com/patrickmn/go-cache"
1516
log "github.com/sirupsen/logrus"
1617
)
1718

@@ -78,8 +79,27 @@ func downloadFile(filepath string, url string) {
7879
func fetchRemoteImg(url string, subdir string) config.MetaFile {
7980
// url is https://test.webp.sh/mypic/123.jpg?someother=200&somebugs=200
8081
// How do we know if the remote img is changed? we're using hash(etag+length)
81-
log.Infof("Remote Addr is %s, pinging for info...", url)
82-
etag := pingURL(url)
82+
var etag string
83+
84+
cacheKey := subdir+":"+helper.HashString(url)
85+
86+
if val, found := config.RemoteCache.Get(cacheKey); found {
87+
if etagVal, ok := val.(string); ok {
88+
log.Infof("Using cache for remote addr: %s", url)
89+
etag = etagVal
90+
} else {
91+
config.RemoteCache.Delete(cacheKey)
92+
}
93+
}
94+
95+
if etag == "" {
96+
log.Infof("Remote Addr is %s, pinging for info...", url)
97+
etag = pingURL(url)
98+
if etag != "" {
99+
config.RemoteCache.Set(cacheKey, etag, cache.DefaultExpiration)
100+
}
101+
}
102+
83103
metadata := helper.ReadMetadata(url, etag, subdir)
84104
localRawImagePath := path.Join(config.RemoteRaw, subdir, metadata.Id)
85105

0 commit comments

Comments
 (0)