This repository has been archived by the owner on Dec 17, 2023. It is now read-only.
forked from plantimals/rsslay
-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement redis cache if available
- Loading branch information
Showing
13 changed files
with
154 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,104 @@ | ||
package custom_cache | ||
|
||
import ( | ||
"context" | ||
"github.com/allegro/bigcache" | ||
"github.com/eko/gocache/lib/v4/cache" | ||
"github.com/eko/gocache/lib/v4/store" | ||
bigcache_store "github.com/eko/gocache/store/bigcache/v4" | ||
redis_store "github.com/eko/gocache/store/redis/v4" | ||
"github.com/piraces/rsslay/pkg/metrics" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/redis/go-redis/v9" | ||
"log" | ||
"time" | ||
) | ||
|
||
type RedisConfig struct { | ||
Address string | ||
Username string | ||
Password string | ||
} | ||
|
||
var MainCache *cache.Cache[[]byte] | ||
var MainCacheRedis *cache.Cache[string] | ||
var Initialized = false | ||
var RedisConfiguration *RedisConfig | ||
|
||
func InitializeCache() { | ||
bigcacheClient, _ := bigcache.NewBigCache(bigcache.DefaultConfig(60 * time.Minute)) | ||
if RedisConfiguration != nil { | ||
initializeRedisCache() | ||
return | ||
} | ||
initializeBigCache() | ||
} | ||
|
||
func Get(key string) (string, error) { | ||
if !Initialized { | ||
InitializeCache() | ||
} | ||
if MainCacheRedis != nil { | ||
return getFromRedis(key) | ||
} | ||
return getFromBigCache(key) | ||
} | ||
|
||
func Set(key string, value string) error { | ||
if !Initialized { | ||
InitializeCache() | ||
} | ||
if MainCacheRedis != nil { | ||
return setToRedis(key, value) | ||
} | ||
|
||
return setToBigCache(key, value) | ||
} | ||
|
||
func initializeBigCache() { | ||
bigcacheClient, _ := bigcache.NewBigCache(bigcache.DefaultConfig(30 * time.Minute)) | ||
bigcacheStore := bigcache_store.NewBigcache(bigcacheClient) | ||
|
||
MainCache = cache.New[[]byte](bigcacheStore) | ||
Initialized = true | ||
} | ||
|
||
func initializeRedisCache() { | ||
redisStore := redis_store.NewRedis(redis.NewClient(&redis.Options{ | ||
Addr: RedisConfiguration.Address, | ||
Username: RedisConfiguration.Username, | ||
Password: RedisConfiguration.Password, | ||
})) | ||
|
||
MainCacheRedis = cache.New[string](redisStore) | ||
} | ||
|
||
func getFromRedis(key string) (string, error) { | ||
value, err := MainCacheRedis.Get(context.Background(), key) | ||
switch err { | ||
case nil: | ||
log.Printf("[DEBUG] Get the key '%s' from the redis cache.", key) | ||
case redis.Nil: | ||
log.Printf("[DEBUG] Failed to find the key '%s' from the redis cache.", key) | ||
default: | ||
log.Printf("[ERROR] Failed to get the value from the redis cache with key '%s': %v", key, err) | ||
metrics.AppErrors.With(prometheus.Labels{"type": "CACHE"}).Inc() | ||
} | ||
return value, err | ||
} | ||
|
||
func setToRedis(key string, value string) error { | ||
return MainCacheRedis.Set(context.Background(), key, value, store.WithExpiration(30*time.Minute)) | ||
} | ||
|
||
func getFromBigCache(key string) (string, error) { | ||
valueBytes, err := MainCache.Get(context.Background(), key) | ||
if err != nil { | ||
log.Printf("[DEBUG] Failed to find the key '%s' from the cache. Error: %v", key, err) | ||
return "", err | ||
} | ||
return string(valueBytes), nil | ||
} | ||
|
||
func setToBigCache(key string, value string) error { | ||
return MainCache.Set(context.Background(), key, []byte(value)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.