Documentation
¶
Index ¶
- Variables
- func CacheJSON(cache Cache, key string, data interface{}, ttl time.Duration) error
- func CacheKeyWithParams(c *goexpress.Context, params ...string) string
- func GenerateCacheKey(c *goexpress.Context) string
- func Invalidate(cache Cache, keys ...string) error
- func InvalidatePattern(cache *RedisCache, pattern string) error
- func Middleware(config CacheConfig) goexpress.Middleware
- type Cache
- type CacheConfig
- type CachedResponse
- type RedisCache
- func (r *RedisCache) Clear() error
- func (r *RedisCache) Close() error
- func (r *RedisCache) Decrement(key string) (int64, error)
- func (r *RedisCache) Delete(key string) error
- func (r *RedisCache) DeleteMany(keys ...string) error
- func (r *RedisCache) Exists(key string) (bool, error)
- func (r *RedisCache) Expire(key string, ttl time.Duration) error
- func (r *RedisCache) Get(key string, dest interface{}) error
- func (r *RedisCache) GetBytes(key string) ([]byte, error)
- func (r *RedisCache) GetClient() *redis.Client
- func (r *RedisCache) GetString(key string) (string, error)
- func (r *RedisCache) Increment(key string) (int64, error)
- func (r *RedisCache) IncrementBy(key string, value int64) (int64, error)
- func (r *RedisCache) Remember(key string, ttl time.Duration, fn func() (interface{}, error), ...) error
- func (r *RedisCache) Set(key string, value interface{}, ttl time.Duration) error
- func (r *RedisCache) SetBytes(key string, value []byte, ttl time.Duration) error
- func (r *RedisCache) SetString(key string, value string, ttl time.Duration) error
- func (r *RedisCache) TTL(key string) (time.Duration, error)
- func (r *RedisCache) Tags(tags ...string) *TaggedCache
- type RedisConfig
- type TaggedCache
Constants ¶
This section is empty.
Variables ¶
var ( // ErrCacheMiss is returned when a key is not found ErrCacheMiss = errors.New("cache miss") )
Functions ¶
func CacheKeyWithParams ¶
Helper function to create a cache key with parameters
func GenerateCacheKey ¶
GenerateCacheKey generates a cache key from method, path, and query params
func Invalidate ¶
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) 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) 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 ¶
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