cache

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCacheMiss is returned when a key is not found
	ErrCacheMiss = errors.New("cache miss")
)

Functions

func CacheJSON

func CacheJSON(cache Cache, key string, data interface{}, ttl time.Duration) error

CacheJSON caches a JSON response manually

func CacheKeyWithParams

func CacheKeyWithParams(c *goexpress.Context, params ...string) string

Helper function to create a cache key with parameters

func GenerateCacheKey

func GenerateCacheKey(c *goexpress.Context) string

GenerateCacheKey generates a cache key from method, path, and query params

func Invalidate

func Invalidate(cache Cache, keys ...string) error

Invalidate removes specific keys from cache

func InvalidatePattern

func InvalidatePattern(cache *RedisCache, pattern string) error

InvalidatePattern removes keys matching a pattern (Redis only)

func Middleware

func Middleware(config CacheConfig) goexpress.Middleware

Middleware returns a cache middleware for GoExpress

Types

type Cache

type Cache interface {
	// Get retrieves a value from cache
	Get(key string, dest interface{}) error

	// Set stores a value in cache
	Set(key string, value interface{}, ttl time.Duration) error

	// Delete removes a value from cache
	Delete(key string) error

	// Exists checks if a key exists
	Exists(key string) (bool, error)

	// Clear removes all cached items
	Clear() error

	// Close closes the cache connection
	Close() error
}

Cache is the interface for cache operations

type CacheConfig

type CacheConfig struct {
	Cache      Cache
	TTL        time.Duration
	KeyFunc    func(*goexpress.Context) string
	SkipFunc   func(*goexpress.Context) bool
	OnlyStatus []int
}

CacheConfig holds cache middleware configuration

func DefaultCacheConfig

func DefaultCacheConfig(cache Cache) CacheConfig

DefaultCacheConfig returns a default cache configuration

type CachedResponse

type CachedResponse struct {
	Status  int               `json:"status"`
	Headers map[string]string `json:"headers"`
	Body    []byte            `json:"body"`
}

CachedResponse holds a cached HTTP response

type RedisCache

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

RedisCache implements a Redis-based cache

func NewRedisCache

func NewRedisCache(config RedisConfig) (*RedisCache, error)

NewRedisCache creates a new Redis cache

func (*RedisCache) Clear

func (r *RedisCache) Clear() error

Clear removes all cached items with the prefix

func (*RedisCache) Close

func (r *RedisCache) Close() error

Close closes the Redis connection

func (*RedisCache) Decrement

func (r *RedisCache) Decrement(key string) (int64, error)

Decrement decrements a numeric value

func (*RedisCache) Delete

func (r *RedisCache) Delete(key string) error

Delete removes a value from cache

func (*RedisCache) DeleteMany

func (r *RedisCache) DeleteMany(keys ...string) error

DeleteMany removes multiple keys from cache

func (*RedisCache) Exists

func (r *RedisCache) Exists(key string) (bool, error)

Exists checks if a key exists

func (*RedisCache) Expire

func (r *RedisCache) Expire(key string, ttl time.Duration) error

Expire sets a timeout on a key

func (*RedisCache) Get

func (r *RedisCache) Get(key string, dest interface{}) error

Get retrieves a value from cache

func (*RedisCache) GetBytes

func (r *RedisCache) GetBytes(key string) ([]byte, error)

GetBytes retrieves raw bytes from cache

func (*RedisCache) GetClient

func (r *RedisCache) GetClient() *redis.Client

GetClient returns the underlying Redis client

func (*RedisCache) GetString

func (r *RedisCache) GetString(key string) (string, error)

GetString retrieves a string value from cache

func (*RedisCache) Increment

func (r *RedisCache) Increment(key string) (int64, error)

Increment increments a numeric value

func (*RedisCache) IncrementBy

func (r *RedisCache) IncrementBy(key string, value int64) (int64, error)

IncrementBy increments by a specific amount

func (*RedisCache) Remember

func (r *RedisCache) Remember(key string, ttl time.Duration, fn func() (interface{}, error), dest interface{}) error

Remember retrieves from cache or executes a function and stores the result

func (*RedisCache) Set

func (r *RedisCache) Set(key string, value interface{}, ttl time.Duration) error

Set stores a value in cache

func (*RedisCache) SetBytes

func (r *RedisCache) SetBytes(key string, value []byte, ttl time.Duration) error

SetBytes stores raw bytes in cache

func (*RedisCache) SetString

func (r *RedisCache) SetString(key string, value string, ttl time.Duration) error

SetString stores a string value in cache

func (*RedisCache) TTL

func (r *RedisCache) TTL(key string) (time.Duration, error)

TTL returns the remaining time to live for a key

func (*RedisCache) Tags

func (r *RedisCache) Tags(tags ...string) *TaggedCache

Tags creates a tagged cache instance

type RedisConfig

type RedisConfig struct {
	Addr     string
	Password string
	DB       int
	Prefix   string
}

RedisConfig holds Redis cache configuration

type TaggedCache

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

Tags support for cache invalidation

func (*TaggedCache) Flush

func (t *TaggedCache) Flush() error

Flush removes all cached items with the specified tags

func (*TaggedCache) Set

func (t *TaggedCache) Set(key string, value interface{}, ttl time.Duration) error

Set stores a value with tags

Jump to

Keyboard shortcuts

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