Documentation
¶
Overview ¶
Example ¶
The common case: JSON round-trip behind a key prefix.
package main
import (
"context"
"fmt"
"time"
"github.com/soulteary/redis-kit/cache"
"github.com/soulteary/redis-kit/testutil"
)
type user struct {
Name string `json:"name"`
Age int `json:"age"`
}
func main() {
client, _ := testutil.NewMockRedisClient() // in real code, your own client
defer func() { _ = client.Close() }()
c := cache.NewCache(client, "app:")
ctx := context.Background()
if err := c.Set(ctx, "user:1", user{Name: "ada", Age: 36}, time.Minute); err != nil {
fmt.Println("set:", err)
return
}
var got user
if err := c.Get(ctx, "user:1", &got); err != nil {
fmt.Println("get:", err)
return
}
fmt.Printf("%s is %d\n", got.Name, got.Age)
}
Output: ada is 36
Index ¶
- Variables
- type Cache
- type RedisCache
- func (c *RedisCache) Del(ctx context.Context, key string) error
- func (c *RedisCache) Exists(ctx context.Context, key string) (bool, error)
- func (c *RedisCache) Expire(ctx context.Context, key string, ttl time.Duration) error
- func (c *RedisCache) Get(ctx context.Context, key string, dest interface{}) error
- func (c *RedisCache) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
- func (c *RedisCache) TTL(ctx context.Context, key string) (time.Duration, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrKeyNotFound = errors.New("key not found")
ErrKeyNotFound identifies a Get miss when the key is absent or has expired.
The error returned by Get matches both ErrKeyNotFound and redis.Nil through errors.Is. ErrKeyNotFound itself is a plain sentinel; it does not wrap redis.Nil.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache interface {
// Set stores a value in the cache with the given TTL
Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
// Get retrieves a value from the cache
// The dest parameter should be a pointer to the type you want to unmarshal into
Get(ctx context.Context, key string, dest interface{}) error
// Del deletes a key from the cache
Del(ctx context.Context, key string) error
// Exists checks if a key exists in the cache
Exists(ctx context.Context, key string) (bool, error)
// TTL returns the remaining time-to-live of a key
TTL(ctx context.Context, key string) (time.Duration, error)
// Expire sets the expiration time for a key
Expire(ctx context.Context, key string, ttl time.Duration) error
}
Cache provides a generic caching interface
type RedisCache ¶
type RedisCache struct {
// contains filtered or unexported fields
}
RedisCache provides a Redis-based cache implementation
func NewCache ¶
func NewCache(client redis.UniversalClient, keyPrefix string) *RedisCache
NewCache creates a new Redis cache with the given client and key prefix.
client is a redis.UniversalClient, so *redis.Client, *redis.ClusterClient, *redis.Ring and a Sentinel-backed failover client from redis.NewFailoverClient all work here -- a cluster no longer needs a cache of its own. A nil client is reported as "redis client is nil" by every method, including a typed nil such as an unassigned *redis.Client field.
Example (ClusterClient) ¶
NewCache takes a redis.UniversalClient, so a cluster, a ring or a Sentinel-backed failover client goes in exactly where a standalone client does. This example is compiled but not run: there is no cluster here to talk to.
package main
import (
"context"
"time"
"github.com/redis/go-redis/v9"
"github.com/soulteary/redis-kit/cache"
)
type user struct {
Name string `json:"name"`
Age int `json:"age"`
}
func main() {
cluster := redis.NewClusterClient(&redis.ClusterOptions{
Addrs: []string{"10.0.0.1:6379", "10.0.0.2:6379", "10.0.0.3:6379"},
})
defer func() { _ = cluster.Close() }()
c := cache.NewCache(cluster, "app:")
_ = c.Set(context.Background(), "user:1", user{Name: "ada", Age: 36}, time.Minute)
failover := redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: "mymaster",
SentinelAddrs: []string{"10.0.0.1:26379", "10.0.0.2:26379"},
})
defer func() { _ = failover.Close() }()
_ = cache.NewCache(failover, "app:")
}
Output:
func (*RedisCache) Del ¶
func (c *RedisCache) Del(ctx context.Context, key string) error
Del deletes a key from Redis
func (*RedisCache) Get ¶
func (c *RedisCache) Get(ctx context.Context, key string, dest interface{}) error
Get retrieves a value from Redis