Skip to content

Commit

Permalink
add: cache for remote image requests
Browse files Browse the repository at this point in the history
  • Loading branch information
HolgerHuo committed Feb 6, 2024
1 parent 1ec981b commit 579f482
Show file tree
Hide file tree
Showing 4 changed files with 43 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
23 changes: 21 additions & 2 deletions handler/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path"
"path/filepath"
"strings"
"time"
"webp_server_go/config"
"webp_server_go/helper"

Expand Down Expand Up @@ -78,8 +79,26 @@ 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 cacheKey, etag string

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

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

if etag == "" {
log.Infof("Remote Addr is %s, pinging for info...", url)
etag = pingURL(url)
if cacheKey != "" {
config.RemoteCache.Set(cacheKey, etag, time.Duration(config.Config.CacheTTL) * time.Second)
}

}

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

Expand Down

0 comments on commit 579f482

Please sign in to comment.