🚚 Rename CaConn to Conn

This commit is contained in:
LittleSheep 2025-03-29 15:02:25 +08:00
parent 69012b9cc3
commit 2c4355257c
4 changed files with 15 additions and 15 deletions

View File

@ -4,6 +4,6 @@ import (
redis_store "github.com/eko/gocache/store/redis/v4" redis_store "github.com/eko/gocache/store/redis/v4"
) )
func (c *CaConn) GoCache() *redis_store.RedisStore { func (c *Conn) GoCache() *redis_store.RedisStore {
return redis_store.NewRedis(c.Rd) return redis_store.NewRedis(c.Rd)
} }

View File

@ -13,7 +13,7 @@ import (
// Provide a advanced tagging experience // Provide a advanced tagging experience
// At the same time, the advanced cache using client side marshaling to handle the advance data types // 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 { func Set[T any](c *Conn, key string, value T, ttl time.Duration, tags ...string) error {
raw, err := json.Marshal(value) raw, err := json.Marshal(value)
if err != nil { if err != nil {
return fmt.Errorf("unable to marshal value during caching: %v", err) return fmt.Errorf("unable to marshal value during caching: %v", err)
@ -27,7 +27,7 @@ func Set[T any](c *CaConn, key string, value T, ttl time.Duration, tags ...strin
// SetKA stands for Set Keep Alive // SetKA stands for Set Keep Alive
// Don't set a TTL for the value set via this function // 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 { func SetKA[T any](c *Conn, key string, value T, tags ...string) error {
raw, err := json.Marshal(value) raw, err := json.Marshal(value)
if err != nil { if err != nil {
return fmt.Errorf("unable to marshal value during caching: %v", err) return fmt.Errorf("unable to marshal value during caching: %v", err)
@ -39,7 +39,7 @@ func SetKA[T any](c *CaConn, key string, value T, tags ...string) error {
return cm.Set(ctx, key, string(raw), store.WithTags(tags)) return cm.Set(ctx, key, string(raw), store.WithTags(tags))
} }
func Get[T any](c *CaConn, key string) (T, error) { func Get[T any](c *Conn, key string) (T, error) {
var out T var out T
ctx, cancel := c.withTimeout() ctx, cancel := c.withTimeout()
@ -57,14 +57,14 @@ func Get[T any](c *CaConn, key string) (T, error) {
return out, nil return out, nil
} }
func Delete(c *CaConn, key string) error { func Delete(c *Conn, key string) error {
ctx, cancel := c.withTimeout() ctx, cancel := c.withTimeout()
defer cancel() defer cancel()
cm := cache.New[[]byte](c.GoCache()) cm := cache.New[[]byte](c.GoCache())
return cm.Delete(ctx, key) return cm.Delete(ctx, key)
} }
func DeleteByTags(c *CaConn, tags ...string) error { func DeleteByTags(c *Conn, tags ...string) error {
if len(tags) == 0 { if len(tags) == 0 {
return nil return nil
} }

View File

@ -5,28 +5,28 @@ import "time"
// The functions below are directly using the redis connection to operaete the redis // 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 // 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 { func (c *Conn) RSet(key string, value any, ttl time.Duration) error {
ctx, cancel := c.withTimeout() ctx, cancel := c.withTimeout()
defer cancel() defer cancel()
return c.Rd.Set(ctx, key, value, ttl).Err() return c.Rd.Set(ctx, key, value, ttl).Err()
} }
// Get retrieves a value from Redis by key // Get retrieves a value from Redis by key
func (c *CaConn) RGet(key string) (string, error) { func (c *Conn) RGet(key string) (string, error) {
ctx, cancel := c.withTimeout() ctx, cancel := c.withTimeout()
defer cancel() defer cancel()
return c.Rd.Get(ctx, key).Result() return c.Rd.Get(ctx, key).Result()
} }
// Delete removes a key from Redis // Delete removes a key from Redis
func (c *CaConn) RDelete(key string) error { func (c *Conn) RDelete(key string) error {
ctx, cancel := c.withTimeout() ctx, cancel := c.withTimeout()
defer cancel() defer cancel()
return c.Rd.Del(ctx, key).Err() return c.Rd.Del(ctx, key).Err()
} }
// Exists checks if a key exists in Redis // Exists checks if a key exists in Redis
func (c *CaConn) RExists(key string) (bool, error) { func (c *Conn) RExists(key string) (bool, error) {
ctx, cancel := c.withTimeout() ctx, cancel := c.withTimeout()
defer cancel() defer cancel()
exists, err := c.Rd.Exists(ctx, key).Result() exists, err := c.Rd.Exists(ctx, key).Result()
@ -37,7 +37,7 @@ func (c *CaConn) RExists(key string) (bool, error) {
} }
// ClearCacheByPrefix deletes all keys matching a given prefix // ClearCacheByPrefix deletes all keys matching a given prefix
func (c *CaConn) RDeleteByPrefix(prefix string) error { func (c *Conn) RDeleteByPrefix(prefix string) error {
ctx, cancel := c.withTimeout() ctx, cancel := c.withTimeout()
defer cancel() defer cancel()

View File

@ -9,14 +9,14 @@ import (
"github.com/redis/go-redis/v9" "github.com/redis/go-redis/v9"
) )
type CaConn struct { type Conn struct {
n *nex.Conn n *nex.Conn
Rd *redis.Client Rd *redis.Client
Timeout time.Duration Timeout time.Duration
} }
func NewCaConn(conn *nex.Conn, timeout time.Duration) (*CaConn, error) { func NewCaConn(conn *nex.Conn, timeout time.Duration) (*Conn, error) {
c := &CaConn{ c := &Conn{
n: conn, n: conn,
Timeout: timeout, Timeout: timeout,
} }
@ -33,6 +33,6 @@ func NewCaConn(conn *nex.Conn, timeout time.Duration) (*CaConn, error) {
return c, nil return c, nil
} }
func (c *CaConn) withTimeout() (context.Context, context.CancelFunc) { func (c *Conn) withTimeout() (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), c.Timeout) return context.WithTimeout(context.Background(), c.Timeout)
} }