cache

package module
v0.13.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 15, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsNil

func IsNil(err error) bool

IsNil returns true if the error is a cache miss.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache wraps Redis client with convenience methods.

func Open

func Open(ctx context.Context, addr string) (*Cache, error)

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

func OpenURL(ctx context.Context, url string) (*Cache, error)

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

func OpenURLWithPrefix(ctx context.Context, url, prefix string) (*Cache, error)

OpenURLWithPrefix opens a Redis connection using a URL and applies a key prefix. All cache operations will prepend prefix to every key.

func OpenWithConfig

func OpenWithConfig(ctx context.Context, cfg Config) (*Cache, error)

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) Client

func (c *Cache) Client() *redis.Client

Client returns the underlying Redis client.

func (*Cache) Close

func (c *Cache) Close() error

Close closes the Redis connection.

func (*Cache) GetJSON

func (c *Cache) GetJSON(ctx context.Context, key string, dest interface{}) error

GetJSON retrieves and unmarshals a JSON value.

func (*Cache) Key added in v0.11.0

func (c *Cache) Key(key string) string

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)
})

func (*Cache) SetJSON

func (c *Cache) SetJSON(ctx context.Context, key string, value interface{}, ttl time.Duration) error

SetJSON marshals and stores a value as JSON.

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.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns production-ready defaults.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL