cachefx

package
v0.6.27 Latest Latest
Warning

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

Go to latest
Published: May 22, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

README

ajan/cachefx

Overview

The cachefx package is a flexible caching package that provides a unified interface for different caching backends. Currently, it supports Redis as a caching backend.

The documentation below provides an overview of the package, its types, functions, and usage examples. For more detailed information, refer to the source code and tests.

Configuration

Configuration struct for the cache:

type Config struct {
  Caches map[string]ConfigCache `conf:"CACHES"`
}

type ConfigCache struct {
  Provider string `conf:"PROVIDER"`
  DSN      string `conf:"DSN"`
}

Example configuration:

config := &cachefx.Config{
  Caches: map[string]cachefx.ConfigCache{
    "default": {
      Provider: "redis",
      DSN:      "redis://localhost:6379",
    },
    "session": {
      Provider: "redis",
      DSN:      "redis://localhost:6380",
    },
  },
}

Features

  • Redis caching backend support
  • Configurable cache dialects
  • Registry pattern for managing multiple cache instances
  • Easy to extend for additional caching backends

API

Usage
import "github.com/eser/ajan/cachefx"

// Create a new Redis cache instance
cache, err := cachefx.NewRedisCache(ctx, cachefx.DialectRedis, "redis://localhost:6379")
if err != nil {
  log.Fatal(err)
}

// Set a value with expiration
err = cache.Set(ctx, "key", "value", time.Hour)

// Get a value
value, err := cache.Get(ctx, "key")

// Delete a value
err = cache.Delete(ctx, "key")

Documentation

Index

Constants

View Source
const DefaultCache = "default"

Variables

View Source
var (
	ErrUnknownProvider          = errors.New("unknown provider")
	ErrUnableToDetermineDialect = errors.New("unable to determine dialect")
)

Functions

This section is empty.

Types

type Cache

type Cache interface {
	GetDialect() Dialect

	Set(ctx context.Context, key string, value any, expiration time.Duration) error
	Get(ctx context.Context, key string) (string, error)
	Delete(ctx context.Context, key string) error
}

type Config

type Config struct {
	Caches map[string]ConfigCache `conf:"CACHES"`
}

type ConfigCache

type ConfigCache struct {
	Provider string `conf:"PROVIDER"`
	DSN      string `conf:"DSN"`
}

type Dialect

type Dialect string
const (
	DialectRedis Dialect = "redis"
)

func DetermineDialect

func DetermineDialect(provider string, dsn string) (Dialect, error)

type RedisCache

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

func NewRedisCache

func NewRedisCache(ctx context.Context, dialect Dialect, dsn string) (*RedisCache, error)

func (*RedisCache) Delete

func (cache *RedisCache) Delete(ctx context.Context, key string) error

func (*RedisCache) Get

func (cache *RedisCache) Get(ctx context.Context, key string) (string, error)

func (*RedisCache) GetDialect

func (cache *RedisCache) GetDialect() Dialect

func (*RedisCache) Set

func (cache *RedisCache) Set(ctx context.Context, key string, value any, expiration time.Duration) error

type Registry

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

func NewRegistry

func NewRegistry(logger *logfx.Logger) *Registry

func (*Registry) AddConnection

func (registry *Registry) AddConnection(ctx context.Context, name string, provider string, dsn string) error

func (*Registry) GetDefault

func (registry *Registry) GetDefault() Cache

func (*Registry) GetNamed

func (registry *Registry) GetNamed(name string) Cache

func (*Registry) LoadFromConfig

func (registry *Registry) LoadFromConfig(ctx context.Context, config *Config) error

Jump to

Keyboard shortcuts

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