Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/8treenet/gcache
gcache是gorm的中间件,插入后gorm即刻拥有缓存。
https://github.com/8treenet/gcache
cache gcache gorm gorm-cache
Last synced: 5 days ago
JSON representation
gcache是gorm的中间件,插入后gorm即刻拥有缓存。
- Host: GitHub
- URL: https://github.com/8treenet/gcache
- Owner: 8treenet
- License: apache-2.0
- Created: 2019-09-13T11:27:41.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-11-17T06:42:50.000Z (almost 3 years ago)
- Last Synced: 2024-10-31T08:37:34.592Z (12 days ago)
- Topics: cache, gcache, gorm, gorm-cache
- Language: Go
- Homepage:
- Size: 96.7 KB
- Stars: 386
- Watchers: 10
- Forks: 52
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gcache
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/8treenet/freedom/blob/master/LICENSE) [![Go Report Card](https://goreportcard.com/badge/github.com/8treenet/freedom)](https://goreportcard.com/report/github.com/8treenet/freedom) [![Build Status](https://travis-ci.org/8treenet/gotree.svg?branch=master)](https://travis-ci.org/8treenet/gotree) [![GoDoc](https://godoc.org/github.com/8treenet/freedom?status.svg)](https://godoc.org/github.com/8treenet/freedom)###### gcache是gorm的中间件,插入后gorm即刻拥有缓存。
## Overview
- 即插即用
- 旁路缓存
- 数据源使用 Redis
- 防击穿
- 防穿透#### 安装
```sh
$ go get github.com/8treenet/gcache
```
#### 快速使用
```go
import (
"github.com/8treenet/gcache"
"github.com/jinzhu/gorm"
"github.com/8treenet/gcache/option""
)func init() {
//创建 gorm.DB
db, _ = gorm.Open("mysql", "")opt := option.DefaultOption{}
opt.Expires = 300 //缓存时间, 默认120秒。范围30-43200
opt.Level = option.LevelSearch //缓存级别,默认LevelSearch。LevelDisable:关闭缓存,LevelModel:模型缓存, LevelSearch:查询缓存
opt.AsyncWrite = false //异步缓存更新, 默认false。 insert update delete 成功后是否异步更新缓存。 ps: affected如果未0,不触发更新。
opt.PenetrationSafe = false //开启防穿透, 默认false。 ps:防击穿强制全局开启。
//缓存中间件附加到gorm.DB
gcache.AttachDB(db, &opt, &option.RedisOption{Addr:"localhost:6379"})
}
```#### 约定
- 模型必须指明主键 `gorm:"primary_key"`
- 不支持 Group
- 不支持 Having
- 查询条件和查询参数分离#### [Example](https://github.com/8treenet/gcache/blob/master/example/example_test.go)
```shell script
#查看 example_test.go 了解更多。
more src/github.com/8treenet/gcache/example/example_test.go
```