Documentation
¶
Index ¶
- func IsNil(err error) bool
- type Cache
- func (c *Cache) Client() *redis.Client
- func (c *Cache) Close() error
- func (c *Cache) GetJSON(ctx context.Context, key string, dest interface{}) error
- func (c *Cache) Key(key string) string
- func (c *Cache) Remember(ctx context.Context, key string, ttl time.Duration, ...) (string, error)
- func (c *Cache) RememberJSON(ctx context.Context, key string, ttl time.Duration, dest interface{}, ...) error
- func (c *Cache) SetJSON(ctx context.Context, key string, value interface{}, ttl time.Duration) error
- type Config
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache wraps Redis client with convenience methods.
func Open ¶
Open opens a Redis connection with default settings.
Example:
c, err := cache.Open(ctx, "localhost:6379")
if err != nil {
log.Fatal(err)
}
defer c.Close()
func OpenURL ¶
OpenURL opens a Redis connection using a URL.
The returned cache has no key prefix. To apply a key prefix, use OpenURLWithPrefix or OpenWithConfig with Config.KeyPrefix set.
Example:
c, err := cache.OpenURL(ctx, "redis://:password@localhost:6379/0")
func OpenURLWithPrefix ¶ added in v0.10.1
OpenURLWithPrefix opens a Redis connection using a URL and applies a key prefix. All cache operations will prepend prefix to every key.
func OpenWithConfig ¶
OpenWithConfig opens a Redis connection with custom settings.
Example:
c, err := cache.OpenWithConfig(ctx, cache.Config{
Addr: "localhost:6379",
Password: "secret",
KeyPrefix: "myapp:",
})
func (*Cache) Key ¶ added in v0.11.0
Key applies the configured prefix to key for direct go-redis operations.
func (*Cache) Remember ¶
func (c *Cache) Remember(ctx context.Context, key string, ttl time.Duration, fn func() (interface{}, error)) (string, error)
Remember gets a value or sets it using the provided function.
Example:
user, err := c.Remember(ctx, "user:123", time.Hour, func() (interface{}, error) {
return db.GetUser(ctx, 123)
})
func (*Cache) RememberJSON ¶
func (c *Cache) RememberJSON(ctx context.Context, key string, ttl time.Duration, dest interface{}, fn func() (interface{}, error)) error
RememberJSON gets a value or computes and caches it as JSON. Unlike Remember, this preserves type information for GetJSON retrieval.
Example:
var user User
err := c.RememberJSON(ctx, "user:123", time.Hour, &user, func() (interface{}, error) {
return db.GetUser(ctx, 123)
})
type Config ¶
type Config struct {
// URL is the Redis connection string.
// Format: redis://:password@host:port/db or redis://host:port
URL string
// Addr is the Redis server address (alternative to URL).
// Default: localhost:6379
Addr string
// Password for Redis authentication.
Password string
// DB is the Redis database number.
// Default: 0
DB int
// PoolSize is the maximum number of connections.
// Default: 10
PoolSize int
// MinIdleConns is the minimum number of idle connections.
// Default: 2
MinIdleConns int
// DialTimeout is the timeout for establishing new connections.
// Default: 5 seconds
DialTimeout time.Duration
// ReadTimeout is the timeout for socket reads.
// Default: 3 seconds
ReadTimeout time.Duration
// WriteTimeout is the timeout for socket writes.
// Default: 3 seconds
WriteTimeout time.Duration
// KeyPrefix is prepended to all keys.
KeyPrefix string
}
Config configures Redis connection.