Documentation
¶
Overview ¶
Package cache provides in-memory cache implementation. The memory cache uses a simple map with TTL support. It is thread-safe via RWMutex. Note: Memory cache is NOT suitable for distributed systems or production environments requiring data persistence. Use Redis cache driver instead.
本文件提供内存缓存实现,适用于单机开发测试或无需持久化的场景。 内存缓存使用简单的 map 实现,支持 TTL 过期,通过 RWMutex 保证线程安全。 注意:内存缓存不适用于分布式系统或需要数据持久化的生产环境,请使用 Redis 驱动。
Package cache provides a unified cache service abstraction for gorp framework. The package implements a cache service that can be configured via "cache.driver" config key or CACHE_DRIVER environment variable. Supported drivers: - "redis" (default): Redis-based distributed cache - "memory"/"mem"/"inmemory": In-memory local cache Eg:
缓存服务包,提供统一的缓存抽象,支持 Redis 和内存两种驱动。 Eg:
// 在 bootstrap 中注册缓存 Provider app.Register(cache.NewProvider()) // 通过配置指定驱动 // config.yaml: // cache: // driver: redis // 获取缓存服务 cacheSvc := c.MustMake(datacontract.CacheKey).(datacontract.Cache) cacheSvc.Set(ctx, "user:123", "value", 10*time.Minute)
Package cache provides Redis-based cache implementation. redisCache wraps datacontract.Redis to implement cacheDriver interface. Note: This implementation relies on Redis provider being registered first.
本文件提供基于 Redis 的缓存实现,适用于分布式系统或生产环境。 redisCache 封装 datacontract.Redis 实现 cacheDriver 接口。 注意:此实现依赖 Redis Provider 先注册。
Index ¶
- type BinaryCacheProvider
- func (p *BinaryCacheProvider) Boot(runtimecontract.Container) error
- func (p *BinaryCacheProvider) DependsOn() []string
- func (p *BinaryCacheProvider) IsDefer() bool
- func (p *BinaryCacheProvider) Name() string
- func (p *BinaryCacheProvider) Provides() []string
- func (p *BinaryCacheProvider) Register(c runtimecontract.Container) error
- type Provider
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BinaryCacheProvider ¶
type BinaryCacheProvider struct{}
BinaryCacheProvider registers the binary cache capability.
BinaryCacheProvider 注册二进制缓存能力。
func NewBinaryCacheProvider ¶
func NewBinaryCacheProvider() *BinaryCacheProvider
NewBinaryCacheProvider creates a new binary cache provider instance.
NewBinaryCacheProvider 创建新的二进制缓存 Provider 实例。
func (*BinaryCacheProvider) Boot ¶
func (p *BinaryCacheProvider) Boot(runtimecontract.Container) error
Boot is a no-op for binary cache provider.
Boot 二进制缓存 Provider 无启动逻辑。
func (*BinaryCacheProvider) DependsOn ¶
func (p *BinaryCacheProvider) DependsOn() []string
DependsOn returns the keys this provider depends on. BinaryCacheProvider depends on Config for cache configuration.
DependsOn 返回该 provider 依赖的 key。 BinaryCacheProvider 依赖 Config 获取缓存配置。
func (*BinaryCacheProvider) IsDefer ¶
func (p *BinaryCacheProvider) IsDefer() bool
IsDefer returns false, binary cache should be initialized immediately.
IsDefer 返回 false,二进制缓存服务应立即初始化。
func (*BinaryCacheProvider) Name ¶
func (p *BinaryCacheProvider) Name() string
Name returns the provider name "binary_cache".
Name 返回 Provider 名称 "binary_cache"。
func (*BinaryCacheProvider) Provides ¶
func (p *BinaryCacheProvider) Provides() []string
Provides returns the binary cache contract key.
Provides 返回二进制缓存契约键。
func (*BinaryCacheProvider) Register ¶
func (p *BinaryCacheProvider) Register(c runtimecontract.Container) error
Register binds the binary cache service factory to the container.
Register 将二进制缓存服务工厂绑定到容器。
type Provider ¶
type Provider struct{}
Provider registers the unified cache contract.
Provider 注册统一缓存契约,支持 Redis 和内存两种驱动。
func NewProvider ¶
func NewProvider() *Provider
NewProvider creates a new cache provider instance.
NewProvider 创建新的缓存 Provider 实例。
func (*Provider) Boot ¶
func (p *Provider) Boot(runtimecontract.Container) error
Boot is a no-op for cache provider.
Boot 缓存 Provider 无启动逻辑。
func (*Provider) DependsOn ¶
DependsOn returns the keys this provider depends on. Cache provider depends on Config for cache configuration.
DependsOn 返回该 provider 依赖的 key。 Cache provider 依赖 Config 获取缓存配置。
func (*Provider) IsDefer ¶
IsDefer returns false, cache should be initialized immediately.
IsDefer 返回 false,缓存服务应立即初始化。
func (*Provider) Register ¶
func (p *Provider) Register(c runtimecontract.Container) error
Register binds the cache service factory to the container. It reads "cache.driver" from config or CACHE_DRIVER env, defaults to "redis".
Register 将缓存服务工厂绑定到容器。 从配置读取 "cache.driver" 或环境变量 CACHE_DRIVER,默认为 "redis"。