README
¶
Go 缓存库
一个支持内存和 Redis 后端的 Go 缓存库
目录
📑 目录导航
✨ 核心特性
✅ 多存储后端:支持内存和 Redis 两种存储方式
✅ 简洁 API:提供 Get/Set/Delete 等基础操作
✅ 哈希表支持:支持 Redis 风格的哈希表操作
✅ 过期时间:可为每个缓存项设置生存时间(TTL)
📊 特性对比表
| 特性 | 内存缓存 | Redis 缓存 |
|---|---|---|
| 持久化 | ❌ | ✅ |
| 分布式支持 | ❌ | ✅ |
| 性能 | ⚡ 极快 | 🚀 快 |
| 内存限制 | 有 | 可配置 |
| 哈希表过期时间支持 | ✅ | ✅ |
| 批量操作支持 | ✅ | ✅ |
⚙️ 安装与要求
- Go 1.16+
- Redis Server 5.0+ (如果使用 Redis 后端)
go get github.com/zjguoxin/goscache
🚀 快速入门
基础缓存操作
package main
import (
"time"
"github.com/zjguoxin/goscache/cache"
)
package main
import (
"fmt"
"time"
"github.com/zjguoxin/goscache/cache"
)
func main() {
// 初始化内存缓存(默认5分钟过期,10分钟清理间隔)
memCache, err := cache.NewCache(cache.CacheTypeMemory)
if err != nil {
panic(err)
}
defer memCache.Close()
// 设置缓存(10分钟过期)
err = memCache.Set("username", "张三", 10*time.Minute)
if err != nil {
panic(err)
}
// 获取缓存
if val, exists, err := memCache.Get("username"); err == nil && exists {
fmt.Println("获取到:", val)
}
// 删除缓存
err = memCache.Delete("username")
if err != nil {
panic(err)
}
}
哈希表操作
// 初始化Redis缓存
redisCache, err := cache.NewCache(cache.CacheTypeRedis,
cache.WithRedisConfig("localhost:6379", "", "cache:", 0),
cache.WithHashExpiry(time.Hour),
)
if err != nil {
panic(err)
}
defer redisCache.Close()
// 设置哈希表(1小时过期)
userData := map[string]interface{}{
"name": "李四",
"email": "lisi@example.com",
"age": 28,
}
err = redisCache.SetHash("user:1001", userData, time.Hour)
if err != nil {
panic(err)
}
// 获取哈希字段
email, err := redisCache.GetHashField("user:1001", "email")
if err != nil {
panic(err)
}
fmt.Println("用户邮箱:", email)
// 获取整个哈希表
userInfo, err := redisCache.GetHash("user:1001")
if err != nil {
panic(err)
}
fmt.Println("用户信息:", userInfo)
// 删除哈希字段
err = redisCache.DelHash("user:1001", "email")
if err != nil {
panic(err)
}
🔧 高级配置
内存缓存配置
// 自定义默认过期时间和默认清理间隔
memCache, err := cache.NewCache(cache.CacheTypeMemory,
cache.WithExpiration(15*time.Minute, 30*time.Minute),
)
Redis 缓存配置
redisCache, err := cache.NewCache(cache.CacheTypeRedis,
cache.WithRedisConfig("redis.example.com:6379", "password", "app_prefix:", 1),
cache.WithPoolConfig(200, 20), // 连接池配置
cache.WithHashExpiry(2*time.Hour), // 哈希表默认过期时间
)
📋 API 参考
| 方法签名 | 描述 | 参数 | 返回值 |
|---|---|---|---|
Set(key string, value interface{}, expiration time.Duration) error |
设置键值对 | key: 键名value: 存储值expiration: 过期时间(-1 表示永不过期) |
error: 错误信息 |
Get(key string) (interface{}, bool) |
获取键值 | key: 键名 |
interface{}: 获取的值bool: 是否存在 |
Delete(key string) |
删除键值 | key: 键名 |
- |
SetHash(key string, value map[string]interface{}) error |
设置哈希表 | key: 哈希表键名value: 哈希表数据(map) |
error: 错误信息 |
GetHashField(key string, field string) (string, error) |
获取哈希字段值 | key: 哈希表键名field: 字段名 |
string: 字段值error: 错误信息 |
DelHash(key, field string) error |
删除哈希字段 | key: 哈希表键名field: 字段名 |
error: 错误信息 |
ExistHash(key, field string) bool |
检查哈希字段是否存在 | key: 哈希表键名field: 字段名 |
bool: 是否存在 |
注意:所有方法都是线程安全的
💡 最佳实践
func ExampleUserSession() {
// 初始化Redis缓存
c, err := cache.InitCache("redis", "localhost:6379", "", 1, "myproject_cache:")
if err != nil {
panic(err)
}
// 用户登录
session := map[string]interface{}{
"userID": 1001,
"token": "abc123xyz",
"expire": time.Now().Add(24*time.Hour).Unix(),
}
// 存储会话(30分钟过期)
if err := c.SetHash("session:abc123", session); err != nil {
panic(err)
}
// 获取会话
if token, err := c.GetHashField("session:abc123", "token"); err == nil {
fmt.Println("当前会话token:", token)
}
}
❓ 常见问题
如何选择内存缓存还是 Redis 缓存?
- 内存缓存:适合单机应用、临时数据缓存、高性能场景
- Redis 缓存:适合分布式系统、需要持久化的数据、多服务共享缓存
为什么 Get 返回 interface{}类型?
- 为了支持存储任意类型值,使用时需要进行类型断言:
if val, exists, err := cache.Get("key"); exists && err == nil {
if str, ok := val.(string); ok {
// 使用字符串值
}
}
🧪 测试指南
// 运行测试
go test -v ./...
//性能基准测试
go test -bench=. -benchmem
📜 许可证
MIT© zjguoxin
作者
Click to show internal directories.
Click to hide internal directories.