✨ Advance cachekit API built with gocache/v4
This commit is contained in:
9
pkg/nex/cachekit/adaptar.go
Normal file
9
pkg/nex/cachekit/adaptar.go
Normal file
@ -0,0 +1,9 @@
|
||||
package cachekit
|
||||
|
||||
import (
|
||||
redis_store "github.com/eko/gocache/store/redis/v4"
|
||||
)
|
||||
|
||||
func (c *CaConn) GoCache() *redis_store.RedisStore {
|
||||
return redis_store.NewRedis(c.Rd)
|
||||
}
|
75
pkg/nex/cachekit/io.go
Normal file
75
pkg/nex/cachekit/io.go
Normal file
@ -0,0 +1,75 @@
|
||||
package cachekit
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/eko/gocache/lib/v4/cache"
|
||||
"github.com/eko/gocache/lib/v4/store"
|
||||
"github.com/goccy/go-json"
|
||||
)
|
||||
|
||||
// The functions below are operating the redis via the gocache
|
||||
// Provide a advanced tagging experience
|
||||
// At the same time, the advanced cache using client side marshaling to handle the advance data types
|
||||
|
||||
func Set[T any](c *CaConn, key string, value T, ttl time.Duration, tags ...string) error {
|
||||
raw, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to marshal value during caching: %v", err)
|
||||
}
|
||||
|
||||
ctx, cancel := c.withTimeout()
|
||||
defer cancel()
|
||||
cm := cache.New[[]byte](c.GoCache())
|
||||
return cm.Set(ctx, key, raw, store.WithTags(tags), store.WithExpiration(ttl))
|
||||
}
|
||||
|
||||
// SetKA stands for Set Keep Alive
|
||||
// Don't set a TTL for the value set via this function
|
||||
func SetKA[T any](c *CaConn, key string, value T, tags ...string) error {
|
||||
raw, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to marshal value during caching: %v", err)
|
||||
}
|
||||
|
||||
ctx, cancel := c.withTimeout()
|
||||
defer cancel()
|
||||
cm := cache.New[[]byte](c.GoCache())
|
||||
return cm.Set(ctx, key, raw, store.WithTags(tags))
|
||||
}
|
||||
|
||||
func Get[T any](c *CaConn, key string) (T, error) {
|
||||
var out T
|
||||
|
||||
ctx, cancel := c.withTimeout()
|
||||
defer cancel()
|
||||
cm := cache.New[[]byte](c.GoCache())
|
||||
raw, err := cm.Get(ctx, key)
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(raw, &out); err != nil {
|
||||
return out, fmt.Errorf("unable to unmarshal value during caching: %v", err)
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func Delete(c *CaConn, key string) error {
|
||||
ctx, cancel := c.withTimeout()
|
||||
defer cancel()
|
||||
cm := cache.New[[]byte](c.GoCache())
|
||||
return cm.Delete(ctx, key)
|
||||
}
|
||||
|
||||
func DeleteByTags(c *CaConn, tags ...string) error {
|
||||
if len(tags) == 0 {
|
||||
return nil
|
||||
}
|
||||
ctx, cancel := c.withTimeout()
|
||||
defer cancel()
|
||||
cm := cache.New[[]byte](c.GoCache())
|
||||
return cm.Invalidate(ctx, store.WithInvalidateTags(tags))
|
||||
}
|
54
pkg/nex/cachekit/raw_io.go
Normal file
54
pkg/nex/cachekit/raw_io.go
Normal file
@ -0,0 +1,54 @@
|
||||
package cachekit
|
||||
|
||||
import "time"
|
||||
|
||||
// The functions below are directly using the redis connection to operaete the redis
|
||||
|
||||
// Set stores a key-value pair in Redis with an optional expiration time
|
||||
func (c *CaConn) RSet(key string, value any, ttl time.Duration) error {
|
||||
ctx, cancel := c.withTimeout()
|
||||
defer cancel()
|
||||
return c.Rd.Set(ctx, key, value, ttl).Err()
|
||||
}
|
||||
|
||||
// Get retrieves a value from Redis by key
|
||||
func (c *CaConn) RGet(key string) (string, error) {
|
||||
ctx, cancel := c.withTimeout()
|
||||
defer cancel()
|
||||
return c.Rd.Get(ctx, key).Result()
|
||||
}
|
||||
|
||||
// Delete removes a key from Redis
|
||||
func (c *CaConn) RDelete(key string) error {
|
||||
ctx, cancel := c.withTimeout()
|
||||
defer cancel()
|
||||
return c.Rd.Del(ctx, key).Err()
|
||||
}
|
||||
|
||||
// Exists checks if a key exists in Redis
|
||||
func (c *CaConn) RExists(key string) (bool, error) {
|
||||
ctx, cancel := c.withTimeout()
|
||||
defer cancel()
|
||||
exists, err := c.Rd.Exists(ctx, key).Result()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return exists > 0, nil
|
||||
}
|
||||
|
||||
// ClearCacheByPrefix deletes all keys matching a given prefix
|
||||
func (c *CaConn) RDeleteByPrefix(prefix string) error {
|
||||
ctx, cancel := c.withTimeout()
|
||||
defer cancel()
|
||||
|
||||
iter := c.Rd.Scan(ctx, 0, prefix+"*", 0).Iterator()
|
||||
for iter.Next(ctx) {
|
||||
if err := c.Rd.Del(ctx, iter.Val()).Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := iter.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
@ -36,52 +36,3 @@ func NewCaConn(conn *nex.Conn, timeout time.Duration) (*CaConn, error) {
|
||||
func (c *CaConn) withTimeout() (context.Context, context.CancelFunc) {
|
||||
return context.WithTimeout(context.Background(), c.Timeout)
|
||||
}
|
||||
|
||||
// Set stores a key-value pair in Redis with an optional expiration time
|
||||
func (c *CaConn) Set(key string, value any, ttl time.Duration) error {
|
||||
ctx, cancel := c.withTimeout()
|
||||
defer cancel()
|
||||
return c.Rd.Set(ctx, key, value, ttl).Err()
|
||||
}
|
||||
|
||||
// Get retrieves a value from Redis by key
|
||||
func (c *CaConn) Get(key string) (string, error) {
|
||||
ctx, cancel := c.withTimeout()
|
||||
defer cancel()
|
||||
return c.Rd.Get(ctx, key).Result()
|
||||
}
|
||||
|
||||
// Delete removes a key from Redis
|
||||
func (c *CaConn) Delete(key string) error {
|
||||
ctx, cancel := c.withTimeout()
|
||||
defer cancel()
|
||||
return c.Rd.Del(ctx, key).Err()
|
||||
}
|
||||
|
||||
// Exists checks if a key exists in Redis
|
||||
func (c *CaConn) Exists(key string) (bool, error) {
|
||||
ctx, cancel := c.withTimeout()
|
||||
defer cancel()
|
||||
exists, err := c.Rd.Exists(ctx, key).Result()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return exists > 0, nil
|
||||
}
|
||||
|
||||
// ClearCacheByPrefix deletes all keys matching a given prefix
|
||||
func (c *CaConn) DeleteByPrefix(prefix string) error {
|
||||
ctx, cancel := c.withTimeout()
|
||||
defer cancel()
|
||||
|
||||
iter := c.Rd.Scan(ctx, 0, prefix+"*", 0).Iterator()
|
||||
for iter.Next(ctx) {
|
||||
if err := c.Rd.Del(ctx, iter.Val()).Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := iter.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user