add post-flush reheat action

This commit is contained in:
Umputun
2018-02-13 11:46:16 -06:00
parent b78cc1284a
commit 4639e14146
2 changed files with 18 additions and 4 deletions
+11 -1
View File
@@ -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!")
}
+7 -3
View File
@@ -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()
}
}