cache

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

*

  • @Author: guxline zjguoxin@163.com
  • @Date: 2025/7/1 16:21:29
  • @LastEditors: guxline zjguoxin@163.com
  • @LastEditTime: 2025/7/1 16:21:29
  • Description:
  • Copyright: Copyright (©) 2025 中易综服. All rights reserved.

*

  • @Author: guxline zjguoxin@163.com
  • @Date: 2025/7/1 16:22:40
  • @LastEditors: guxline zjguoxin@163.com
  • @LastEditTime: 2025/7/1 16:22:40
  • Description: 内存缓存实现
  • Copyright: Copyright (©) 2025 中易综服. All rights reserved.

*

  • @Author: guxline zjguoxin@163.com
  • @Date: 2025/7/1 16:29:58
  • @LastEditors: guxline zjguoxin@163.com
  • @LastEditTime: 2025/7/1 16:29:58
  • Description: Redis缓存实现
  • Copyright: Copyright (©) 2025 中易综服. All rights reserved.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheConfig

type CacheConfig struct {
	Type          string        `json:"type"`            // 缓存类型: memory 或 redis
	URL           string        `json:"url"`             // Redis连接地址
	Password      string        `json:"password"`        // Redis密码
	DB            int           `json:"db"`              // Redis数据库索引
	Prefix        string        `json:"prefix"`          // Redis键前缀
	DefaultExp    time.Duration `json:"default_exp"`     // 默认过期时间
	CleanupInt    time.Duration `json:"cleanup_int"`     // 清理间隔(仅内存缓存)
	PoolSize      int           `json:"pool_size"`       // Redis连接池大小
	MinIdleConns  int           `json:"min_idle_conns"`  // Redis最小空闲连接数
	HashKeyExpiry time.Duration `json:"hash_key_expiry"` // 哈希表过期时间
}

type CacheInterface

type CacheInterface interface {
	// 基础操作
	Get(key string) (interface{}, bool, error)
	Set(key string, value interface{}, expiration time.Duration) error
	Delete(key string) error
	Close() error

	// 哈希表操作
	SetHash(key string, value map[string]interface{}, expiration time.Duration) error
	GetHash(key string) (map[string]string, error)
	GetHashField(key, field string) (string, error)
	DelHash(key, field string) error
	ExistHash(key, field string) (bool, error)
	ExpireHash(key string, expiration time.Duration) error

	// 批量操作
	MSet(values map[string]interface{}, expiration time.Duration) error
	MGet(keys []string) (map[string]interface{}, error)
}

func NewCache

func NewCache(cacheType CacheType, opts ...Option) (CacheInterface, error)

InitCache 初始化缓存 参数: - 第一个参数: 缓存类型 (memory/redis),可以是CacheType或字符串 - 后续参数可以是以下任意组合:

  • Redis连接URL (string)
  • Redis密码 (string)
  • 默认过期时间 (time.Duration)
  • 清理间隔 (time.Duration)

NewCache 创建缓存实例

type CacheType

type CacheType string
const (
	CacheTypeMemory CacheType = "memory"
	CacheTypeRedis  CacheType = "redis"
)

type MemoryCache

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

MemoryCache 内存缓存实现

func NewMemoryCache

func NewMemoryCache(config *CacheConfig) (*MemoryCache, error)

NewMemoryCache 创建新的内存缓存实例

func (*MemoryCache) Close

func (m *MemoryCache) Close() error

Close 关闭缓存,释放资源

func (*MemoryCache) DelHash

func (m *MemoryCache) DelHash(key, field string) error

DelHash 删除哈希表字段

func (*MemoryCache) Delete

func (m *MemoryCache) Delete(key string) error

Delete 删除缓存值

func (*MemoryCache) ExistHash

func (m *MemoryCache) ExistHash(key, field string) (bool, error)

ExistHash 检查哈希表字段是否存在

func (*MemoryCache) ExpireHash

func (m *MemoryCache) ExpireHash(key string, expiration time.Duration) error

ExpireHash 设置哈希表过期时间

func (*MemoryCache) Get

func (m *MemoryCache) Get(key string) (interface{}, bool, error)

Get 获取缓存值

func (*MemoryCache) GetHash

func (m *MemoryCache) GetHash(key string) (map[string]string, error)

GetHash 获取整个哈希表

func (*MemoryCache) GetHashField

func (m *MemoryCache) GetHashField(key, field string) (string, error)

GetHashField 获取哈希表字段

func (*MemoryCache) MGet

func (m *MemoryCache) MGet(keys []string) (map[string]interface{}, error)

MGet 批量获取缓存值

func (*MemoryCache) MSet

func (m *MemoryCache) MSet(values map[string]interface{}, expiration time.Duration) error

MSet 批量设置缓存值

func (*MemoryCache) Set

func (m *MemoryCache) Set(key string, value interface{}, expiration time.Duration) error

Set 设置缓存值

func (*MemoryCache) SetHash

func (m *MemoryCache) SetHash(key string, value map[string]interface{}, expiration time.Duration) error

SetHash 设置哈希表

type Option

type Option func(*CacheConfig)

Option 配置选项函数类型

func WithExpiration

func WithExpiration(defaultExp, cleanupInt time.Duration) Option

WithExpiration 过期时间配置选项

func WithHashExpiry

func WithHashExpiry(expiry time.Duration) Option

WithHashExpiry 哈希表过期时间配置选项

func WithPoolConfig

func WithPoolConfig(poolSize, minIdleConns int) Option

WithPoolConfig 连接池配置选项

func WithRedisConfig

func WithRedisConfig(url, password, prefix string, db int) Option

WithRedisConfig Redis配置选项

type RedisCache

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

RedisCache Redis缓存实现

func NewRedisCache

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

NewRedisCache 创建Redis缓存实例

func (*RedisCache) Close

func (r *RedisCache) Close() error

Close 关闭Redis连接

func (*RedisCache) DelHash

func (r *RedisCache) DelHash(key, field string) error

DelHash 删除哈希表字段

func (*RedisCache) Delete

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

Delete 删除缓存值

func (*RedisCache) ExistHash

func (r *RedisCache) ExistHash(key, field string) (bool, error)

ExistHash 检查哈希表字段是否存在

func (*RedisCache) ExpireHash

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

ExpireHash 设置哈希表过期时间

func (*RedisCache) Get

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

Get 获取缓存值

func (*RedisCache) GetHash

func (r *RedisCache) GetHash(key string) (map[string]string, error)

GetHash 获取整个哈希表

func (*RedisCache) GetHashField

func (r *RedisCache) GetHashField(key, field string) (string, error)

GetHashField 获取哈希表字段

func (*RedisCache) MGet

func (r *RedisCache) MGet(keys []string) (map[string]interface{}, error)

MGet 批量获取缓存值

func (*RedisCache) MSet

func (r *RedisCache) MSet(values map[string]interface{}, expiration time.Duration) error

MSet 批量设置缓存值

func (*RedisCache) Set

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

Set 设置缓存值

func (*RedisCache) SetHash

func (r *RedisCache) SetHash(key string, value map[string]interface{}, expiration time.Duration) error

SetHash 设置哈希表

Jump to

Keyboard shortcuts

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