diff --git a/app/main.go b/app/main.go index b65b9210..fb2b409c 100644 --- a/app/main.go +++ b/app/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" "log" + "net/http" "os" "time" @@ -101,10 +102,19 @@ func main() { Admins: opts.Admins, DevMode: opts.DevMode, Exporter: &exporter, - Cache: common.NewLoadingCache(4*time.Hour, 15*time.Minute), AuthProviders: makeAuthProviders(sessionStore), } + srv.Cache = common.NewLoadingCache(4*time.Hour, 15*time.Minute, func() { + // set post-flush callback + resp, err := http.Get(opts.RemarkURL + "/api/v1/list") + if err != nil { + log.Printf("[WARN] failed to refresh cached list, %s", err) + return + } + resp.Body.Close() + }) + if opts.DevMode { log.Printf("[WARN] running in dev mode, no auth!") } diff --git a/app/rest/common/cache.go b/app/rest/common/cache.go index 5c7e6017..edd87504 100644 --- a/app/rest/common/cache.go +++ b/app/rest/common/cache.go @@ -15,12 +15,13 @@ type LoadingCache interface { // loadingCache implements LoadingCache interface on top of cache.Cache type loadingCache struct { - bytesCache *cache.Cache + bytesCache *cache.Cache + postFlushFn func() } // NewLoadingCache makes loadingCache implementation -func NewLoadingCache(defaultExpiration, cleanupInterval time.Duration) LoadingCache { - return &loadingCache{bytesCache: cache.New(defaultExpiration, cleanupInterval)} +func NewLoadingCache(defaultExpiration, cleanupInterval time.Duration, postFlushFn func()) LoadingCache { + return &loadingCache{bytesCache: cache.New(defaultExpiration, cleanupInterval), postFlushFn: postFlushFn} } func (lc *loadingCache) Get(key string, ttl time.Duration, fn func() ([]byte, error)) (data []byte, err error) { @@ -39,4 +40,7 @@ func (lc *loadingCache) Get(key string, ttl time.Duration, fn func() ([]byte, er func (lc *loadingCache) Flush() { lc.bytesCache.Flush() + if lc.postFlushFn != nil { + lc.postFlushFn() + } }