1
0
Fork 0
mirror of https://github.com/FrankerFaceZ/FrankerFaceZ.git synced 2025-09-03 03:40:56 +00:00

Revert switching cache implementation

This commit is contained in:
Kane York 2017-09-25 15:24:58 -07:00
parent 7657357164
commit 8767206105
2 changed files with 9 additions and 27 deletions

View file

@ -16,7 +16,7 @@ import (
"time" "time"
"github.com/FrankerFaceZ/FrankerFaceZ/socketserver/server/naclform" "github.com/FrankerFaceZ/FrankerFaceZ/socketserver/server/naclform"
"github.com/karlseguin/ccache" cache "github.com/patrickmn/go-cache"
"golang.org/x/crypto/nacl/box" "golang.org/x/crypto/nacl/box"
"golang.org/x/sync/singleflight" "golang.org/x/sync/singleflight"
) )
@ -29,7 +29,7 @@ const bPathOtherCommand = "/cmd/"
type backendInfo struct { type backendInfo struct {
HTTPClient http.Client HTTPClient http.Client
baseURL string baseURL string
responseCache *ccache.Cache responseCache *cache.Cache
reloadGroup singleflight.Group reloadGroup singleflight.Group
postStatisticsURL string postStatisticsURL string
@ -52,7 +52,7 @@ func setupBackend(config *ConfigFile) *backendInfo {
b.HTTPClient.Timeout = 60 * time.Second b.HTTPClient.Timeout = 60 * time.Second
b.baseURL = config.BackendURL b.baseURL = config.BackendURL
// size in bytes of string payload // size in bytes of string payload
b.responseCache = ccache.New(ccache.Configure().MaxSize(250 * 1000 * 1024)) b.responseCache = cache.New(60*time.Second, 10*time.Minute)
b.announceStartupURL = fmt.Sprintf("%s%s", b.baseURL, bPathAnnounceStartup) b.announceStartupURL = fmt.Sprintf("%s%s", b.baseURL, bPathAnnounceStartup)
b.addTopicURL = fmt.Sprintf("%s%s", b.baseURL, bPathAddTopic) b.addTopicURL = fmt.Sprintf("%s%s", b.baseURL, bPathAddTopic)
@ -80,18 +80,6 @@ func getCacheKey(remoteCommand, data string) string {
return fmt.Sprintf("%s/%s", remoteCommand, data) return fmt.Sprintf("%s/%s", remoteCommand, data)
} }
type cachedResponseStr string
// implements ccache.Sized
func (c cachedResponseStr) Size() int64 {
return int64(len(string(c)))
}
// implements Stringer
func (c cachedResponseStr) String() string {
return string(c)
}
// ErrForwardedFromBackend is an error returned by the backend server. // ErrForwardedFromBackend is an error returned by the backend server.
type ErrForwardedFromBackend struct { type ErrForwardedFromBackend struct {
JSONError interface{} JSONError interface{}
@ -114,16 +102,9 @@ var ErrAuthorizationNeeded = errors.New("Must authenticate Twitch username to us
// and the cache is updated in the background. // and the cache is updated in the background.
func (backend *backendInfo) SendRemoteCommandCached(remoteCommand, data string, auth AuthInfo) (string, error) { func (backend *backendInfo) SendRemoteCommandCached(remoteCommand, data string, auth AuthInfo) (string, error) {
cacheKey := getCacheKey(remoteCommand, data) cacheKey := getCacheKey(remoteCommand, data)
item := backend.responseCache.Get(cacheKey) cached, ok := backend.responseCache.Get(cacheKey)
if item != nil { if ok {
if item.Expired() { return cached.(string), nil
// reload in background
go backend.reloadGroup.Do(cacheKey, func() (interface{}, error) {
backend.SendRemoteCommand(remoteCommand, data, auth)
return nil, nil
})
}
return item.Value().(cachedResponseStr).String(), nil
} }
return backend.SendRemoteCommand(remoteCommand, data, auth) return backend.SendRemoteCommand(remoteCommand, data, auth)
} }
@ -208,7 +189,7 @@ func (backend *backendInfo) SendRemoteCommand(remoteCommand, data string, auth A
duration := time.Duration(durSecs) * time.Second duration := time.Duration(durSecs) * time.Second
backend.responseCache.Set( backend.responseCache.Set(
getCacheKey(remoteCommand, data), getCacheKey(remoteCommand, data),
cachedResponseStr(responseStr), responseStr,
duration, duration,
) )
} }

View file

@ -38,7 +38,7 @@ type StatsData struct {
MemoryInUseKB uint64 MemoryInUseKB uint64
MemoryRSSKB uint64 MemoryRSSKB uint64
ResponseCacheItems uint64 ResponseCacheItems int
MemPerClientBytes uint64 MemPerClientBytes uint64
CpuUsagePct float64 CpuUsagePct float64
@ -169,6 +169,7 @@ func updatePeriodicStats() {
{ {
Statistics.Uptime = nowUpdate.Sub(Statistics.StartTime).String() Statistics.Uptime = nowUpdate.Sub(Statistics.StartTime).String()
Statistics.ResponseCacheItems = Backend.responseCache.ItemCount()
} }
{ {