From 0b6fc10f2c35f3376df9b32a70c981dd0ec36c86 Mon Sep 17 00:00:00 2001 From: Nathan13888 <29968201+Nathan13888@users.noreply.github.com> Date: Wed, 19 Jul 2023 12:53:51 -0400 Subject: [PATCH] remove deprecated cache Signed-off-by: Nathan13888 <29968201+Nathan13888@users.noreply.github.com> --- badges.go | 31 +----------------- cache.go | 96 ------------------------------------------------------- go.mod | 2 +- server.go | 2 -- 4 files changed, 2 insertions(+), 129 deletions(-) delete mode 100644 cache.go diff --git a/badges.go b/badges.go index 774bd6f..60c0407 100644 --- a/badges.go +++ b/badges.go @@ -1,11 +1,9 @@ package main import ( - "encoding/json" "fmt" "io/ioutil" "net/http" - "strconv" "github.com/rs/zerolog/log" ) @@ -103,36 +101,9 @@ func getErrorBadge() []byte { } func updateCounter(useCache bool, hash string) string { - if !useCache { - // return updateCountAPI(hash) - return updateRedis(hash) - } - // look up cache - return updateCachedCount(hash) // note that this will return the CountAPI count if hash does not exist + return updateRedis(hash) } func updateRedis(hash string) string { return QueryHash(hash) } - -// DEPRECATED: -func updateCountAPI(hash string) string { - url := "https://api.countapi.xyz/hit/visitor-badge/" + hash - res, err := http.Get(url) - // TODO: fix header content check - // cont := res.Header.Get("Content-Type") - // if cont != "application/json" { - // log.Fatal("Did not receive expected JSON response. Received: " + cont) - // } - if err != nil { - logError(err) - } - defer res.Body.Close() - - var obj CountAPIResponse - decoder := json.NewDecoder(res.Body) - decoder.DisallowUnknownFields() - decoder.Decode(&obj) - - return strconv.Itoa(obj.Value) -} diff --git a/cache.go b/cache.go deleted file mode 100644 index f7b8f6f..0000000 --- a/cache.go +++ /dev/null @@ -1,96 +0,0 @@ -package main - -import ( - "time" - - "github.com/allegro/bigcache" - "github.com/rs/zerolog/log" -) - -var cacheConfig = bigcache.Config{ - Shards: 1024, // power of two - LifeWindow: 7 * 24 * time.Hour, // expiry time - CleanWindow: 5 * time.Minute, - // rps * lifeWindow, used only in initial memory allocation - MaxEntriesInWindow: 1000 * 10 * 60, - // max entry size in bytes, used only in initial memory allocation - MaxEntrySize: 1000, - // prints information about additional memory allocation - Verbose: false, - // cache will not allocate more memory than this limit, value in MB - // if value is reached then the oldest entries can be overridden for the new ones - // 0 value means no size limit - HardMaxCacheSize: 4096, - // callback fired when the oldest entry is removed because of its expiration time or no space left - // for the new entry, or because delete was called. A bitmask representing the reason will be returned. - // Default value is nil which means no callback and it prevents from unwrapping the oldest entry. - OnRemove: logEntryOverwriten, - // OnRemoveWithReason is a callback fired when the oldest entry is removed because of its expiration time or no space left - // for the new entry, or because delete was called. A constant representing the reason will be passed through. - // Default value is nil which means no callback and it prevents from unwrapping the oldest entry. - // Ignored if OnRemove is specified. - OnRemoveWithReason: nil, -} - -var cache *bigcache.BigCache - -func initCache() { - c, err := bigcache.NewBigCache(cacheConfig) - if err != nil { - logError(err) - } - cache = c -} - -func updateCachedCount(hash string) string { - entry, err := cache.Get(hash) - // return CountAPI value if entry does not exist in cache - if err != nil { - capi := updateRedis(hash) - if err == bigcache.ErrEntryNotFound { - // update cached value - log.Info().Str("page", hash).Msg("Cached") - cache.Set(hash, []byte(capi)) - } else { - logError(err) - } - return capi - } - if len(entry) == 0 { - return "-1" - } - newCount := tallyByteSlice(entry) - setCachedCount(hash, newCount) - - // update CountAPI count in a separate go routine - go func() { - res := updateRedis(hash) - // "sync" with CountAPI every so often - if res[len(res)-1] == '0' { - setCachedCount(hash, []byte(res)) - } - }() - - return string(newCount) -} - -func setCachedCount(hash string, count []byte) error { - err := cache.Set(hash, count) - if err != nil { - logError(err) - } - - return err -} - -func tallyByteSlice(slice []byte) []byte { - n := len(slice) - // increment last - last := n - 1 - slice[last]++ - if slice[last] > '9' { - return append(tallyByteSlice(slice[:last]), '0') - } - - return slice -} diff --git a/go.mod b/go.mod index 03dd208..7e99709 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.15 require ( github.com/allegro/bigcache v1.2.1 github.com/gorilla/mux v1.8.0 - github.com/redis/go-redis/v9 v9.0.5 // indirect + github.com/redis/go-redis/v9 v9.0.5 github.com/rs/zerolog v1.26.0 github.com/stretchr/testify v1.7.0 // indirect ) diff --git a/server.go b/server.go index 062a572..05f7dae 100644 --- a/server.go +++ b/server.go @@ -84,7 +84,6 @@ func main() { r.Use(loggingMiddleware) log.Info().Msg("Configuring cache") - initCache() startTime = time.Now() // Run our server in a goroutine so that it doesn't block. @@ -226,7 +225,6 @@ func getPing(w http.ResponseWriter, r *http.Request) { func getStatus(w http.ResponseWriter, r *http.Request) { // TODO: include redis res := StatusResponse{ - CachedHashes: cache.Len(), ProcessedRequests: processedBadges, Uptime: int64(time.Since(startTime).Seconds()), CodeRepository: "https://github.com/Nathan13888/VisitorBadgeReloaded",