Documentation
¶
Index ¶
- func EnableCache() func(*gorm.DB) *gorm.DB
- func SkipCache() func(*gorm.DB) *gorm.DB
- func SkipCacheContext(ctx context.Context) context.Context
- func WithSkipCache(ctx context.Context, skip bool) context.Context
- type Adapter
- type CachePlugin
- type Config
- type ErrCacheHit
- type JSONSerializer
- type MemoryAdapter
- func (m *MemoryAdapter) Clear(ctx context.Context) error
- func (m *MemoryAdapter) Close() error
- func (m *MemoryAdapter) Delete(ctx context.Context, key string) error
- func (m *MemoryAdapter) DeletePattern(ctx context.Context, pattern string) error
- func (m *MemoryAdapter) Get(ctx context.Context, key string) ([]byte, error)
- func (m *MemoryAdapter) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
- type MsgPackSerializer
- type RedisAdapter
- func (r *RedisAdapter) Clear(ctx context.Context) error
- func (r *RedisAdapter) Close() error
- func (r *RedisAdapter) Delete(ctx context.Context, key string) error
- func (r *RedisAdapter) DeletePattern(ctx context.Context, pattern string) error
- func (r *RedisAdapter) Get(ctx context.Context, key string) ([]byte, error)
- func (r *RedisAdapter) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
- type RedisAdapterConfig
- type Serializer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EnableCache ¶
EnableCache is a scope helper function to explicitly enable cache for a query Usage: db.Scopes(gormcache.EnableCache()).Find(&users)
func SkipCache ¶
SkipCache is a scope helper function to skip cache for a specific query Usage: db.Scopes(gormcache.SkipCache()).Find(&users)
func SkipCacheContext ¶
SkipCacheContext returns a new context that will skip cache for queries
Types ¶
type Adapter ¶
type Adapter interface {
// Get retrieves a value from cache by key
Get(ctx context.Context, key string) ([]byte, error)
// Set stores a value in cache with the given key and TTL
Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
// Delete removes a value from cache by key
Delete(ctx context.Context, key string) error
// DeletePattern removes all keys matching the pattern
DeletePattern(ctx context.Context, pattern string) error
// Clear removes all cached data
Clear(ctx context.Context) error
// Close closes the adapter connection
Close() error
}
Adapter defines the interface for cache storage implementations
type CachePlugin ¶
type CachePlugin struct {
// contains filtered or unexported fields
}
CachePlugin is a GORM plugin that provides caching functionality
func New ¶
func New(config Config) *CachePlugin
New creates a new cache plugin with the given configuration
func (*CachePlugin) Initialize ¶
func (p *CachePlugin) Initialize(db *gorm.DB) error
Initialize initializes the plugin with GORM
type Config ¶
type Config struct {
// Adapter is the cache storage implementation
Adapter Adapter
// TTL is the default time-to-live for cached data
TTL time.Duration
// CacheModels defines which models should be cached
// If empty, all models will be cached
CacheModels []interface{}
// InvalidateOnUpdate determines if cache should be cleared on UPDATE operations
InvalidateOnUpdate bool
// InvalidateOnCreate determines if cache should be cleared on CREATE operations
InvalidateOnCreate bool
// InvalidateOnDelete determines if cache should be cleared on DELETE operations
InvalidateOnDelete bool
// KeyPrefix is the prefix for all cache keys
KeyPrefix string
// SkipCacheCondition is a function to determine if cache should be skipped for a query
// Example: func(db *gorm.DB) bool { return db.Statement.Context.Value("skip_cache") == true }
SkipCacheCondition func(*gorm.DB) bool
// CacheKeyGenerator allows custom cache key generation
// If nil, default key generator will be used
CacheKeyGenerator func(*gorm.DB) string
// Serializer is the data serialization implementation
// If nil, default JSON serializer will be used
Serializer Serializer
}
Config holds the configuration for the cache plugin
type ErrCacheHit ¶
type ErrCacheHit struct {
RowsAffected int64
}
ErrCacheHit 是一个内部使用的 Error,用于在缓存命中时跳过数据库查询 这个 Error 会在 afterQueryCallback 中被自动移除,用户不会看到它
func (*ErrCacheHit) Error ¶
func (e *ErrCacheHit) Error() string
type JSONSerializer ¶ added in v0.1.0
type JSONSerializer struct{}
JSONSerializer implements JSON serialization
func (*JSONSerializer) Marshal ¶ added in v0.1.0
func (j *JSONSerializer) Marshal(v interface{}) ([]byte, error)
Marshal serializes v to JSON bytes
func (*JSONSerializer) Unmarshal ¶ added in v0.1.0
func (j *JSONSerializer) Unmarshal(data []byte, v interface{}) error
Unmarshal deserializes JSON bytes to v
type MemoryAdapter ¶
type MemoryAdapter struct {
// contains filtered or unexported fields
}
MemoryAdapter is an in-memory cache implementation
func NewMemoryAdapter ¶
func NewMemoryAdapter() *MemoryAdapter
NewMemoryAdapter creates a new in-memory cache adapter
func (*MemoryAdapter) Clear ¶
func (m *MemoryAdapter) Clear(ctx context.Context) error
Clear removes all cached data
func (*MemoryAdapter) Delete ¶
func (m *MemoryAdapter) Delete(ctx context.Context, key string) error
Delete removes a value from memory cache
func (*MemoryAdapter) DeletePattern ¶
func (m *MemoryAdapter) DeletePattern(ctx context.Context, pattern string) error
DeletePattern removes all keys matching the pattern
type MsgPackSerializer ¶ added in v0.1.0
type MsgPackSerializer struct{}
MsgPackSerializer implements MessagePack serialization
func (*MsgPackSerializer) Marshal ¶ added in v0.1.0
func (m *MsgPackSerializer) Marshal(v interface{}) ([]byte, error)
Marshal serializes v to MessagePack bytes
func (*MsgPackSerializer) Unmarshal ¶ added in v0.1.0
func (m *MsgPackSerializer) Unmarshal(data []byte, v interface{}) error
Unmarshal deserializes MessagePack bytes to v
type RedisAdapter ¶
type RedisAdapter struct {
// contains filtered or unexported fields
}
RedisAdapter is a Redis cache implementation
func NewRedisAdapter ¶
func NewRedisAdapter(config RedisAdapterConfig) *RedisAdapter
NewRedisAdapter creates a new Redis cache adapter
func NewRedisAdapterWithClient ¶
func NewRedisAdapterWithClient(client *redis.Client) *RedisAdapter
NewRedisAdapterWithClient creates a new Redis adapter with existing client
func (*RedisAdapter) Clear ¶
func (r *RedisAdapter) Clear(ctx context.Context) error
Clear removes all cached data in the current database
func (*RedisAdapter) Delete ¶
func (r *RedisAdapter) Delete(ctx context.Context, key string) error
Delete removes a value from Redis cache
func (*RedisAdapter) DeletePattern ¶
func (r *RedisAdapter) DeletePattern(ctx context.Context, pattern string) error
DeletePattern removes all keys matching the pattern
type RedisAdapterConfig ¶
type RedisAdapterConfig struct {
Addr string // Redis server address (default: "localhost:6379")
Password string // Redis password (default: "")
DB int // Redis database (default: 0)
}
RedisAdapterConfig holds configuration for Redis adapter
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
basic
command
|
|
|
context_usage
command
|
|
|
model_selection
command
|
|
|
redis
command
|
|
|
skip_cache
command
|