Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Pacific73/gorm-cache
gorm v2的即插即用、无需修改代码的旁路缓存。An easy-to-use look-aside cache solution for gorm v2 users.
https://github.com/Pacific73/gorm-cache
cache golang gorm
Last synced: 3 months ago
JSON representation
gorm v2的即插即用、无需修改代码的旁路缓存。An easy-to-use look-aside cache solution for gorm v2 users.
- Host: GitHub
- URL: https://github.com/Pacific73/gorm-cache
- Owner: Pacific73
- License: apache-2.0
- Created: 2022-04-15T13:19:16.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-06-14T07:20:14.000Z (5 months ago)
- Last Synced: 2024-06-21T01:53:54.517Z (5 months ago)
- Topics: cache, golang, gorm
- Language: Go
- Homepage:
- Size: 55.7 KB
- Stars: 104
- Watchers: 1
- Forks: 29
- Open Issues: 10
-
Metadata Files:
- Readme: README.ZH_CN.md
- License: LICENSE
Awesome Lists containing this project
README
[![GoVersion](https://img.shields.io/github/go-mod/go-version/Pacific73/gorm-cache)](https://github.com/Pacific73/gorm-cache/blob/master/go.mod)
[![Release](https://img.shields.io/github/v/release/Pacific73/gorm-cache)](https://github.com/Pacific73/gorm-cache/releases)
[![Apache-2.0 license](https://img.shields.io/badge/license-Apache2.0-brightgreen.svg)](https://opensource.org/licenses/Apache-2.0)[English Version](./README.md) | [中文版本](./README.ZH_CN.md)
`gorm-cache` 旨在为gorm v2用户提供一个即插即用的旁路缓存解决方案。本缓存只适用于数据库表单主键时的场景。
我们提供2种存储介质:
1. 内存 (所有数据存储在单服务器的内存中)
2. Redis (所有数据存储在redis中,如果你有多个实例使用本缓存,那么他们不共享redis存储空间)## 使用说明
```go
import (
"context"
"github.com/Pacific73/gorm-cache/cache"
"github.com/go-redis/redis"
)func main() {
dsn := "user:pass@tcp(127.0.0.1:3306)/database_name?charset=utf8mb4"
db, _ := gorm.Open(mysql.Open(dsn), &gorm.Config{})
redisClient := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
cache, _ := cache.NewGorm2Cache(&config.CacheConfig{
CacheLevel: config.CacheLevelAll,
CacheStorage: config.CacheStorageRedis,
RedisConfig: cache.NewRedisConfigWithClient(redisClient),
InvalidateWhenUpdate: true, // when you create/update/delete objects, invalidate cache
CacheTTL: 5000, // 5000 ms
CacheMaxItemCnt: 5, // if length of objects retrieved one single time
// exceeds this number, then don't cache
})
// More options in `config.config.go`
db.Use(cache) // use gorm plugin
// cache.AttachToDB(db)var users []User
db.Where("value > ?", 123).Find(&users) // search cache not hit, objects cached
db.Where("value > ?", 123).Find(&users) // search cache hit
db.Where("id IN (?)", []int{1, 2, 3}).Find(&users) // primary key cache not hit, users cached
db.Where("id IN (?)", []int{1, 3}).Find(&users) // primary key cache hit
}
```在gorm中主要有5种操作(括号中是gorm中对应函数名):
1. Query (First/Take/Last/Find/FindInBatches/FirstOrInit/FirstOrCreate/Count/Pluck)
2. Create (Create/CreateInBatches/Save)
3. Delete (Delete)
4. Update (Update/Updates/UpdateColumn/UpdateColumns/Save)
5. Row (Row/Rows/Scan)我们不支持Row操作的缓存。