gcache

package module
v1.1.7-beta.1 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2020 License: Apache-2.0 Imports: 3 Imported by: 0

README

gcache

License Go Report Card Build Status GoDoc

gcache是gorm的中间件,插入后gorm即刻拥有缓存。

Overview

  • 即插即用
  • 旁路缓存
  • 数据源使用 Redis
  • 防击穿
  • 防穿透
安装
$ go get github.com/8treenet/gcache
快速使用
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
    #查看 example_test.go 了解更多。
    more src/github.com/8treenet/gcache/example/example_test.go

Documentation

Index

Constants

View Source
const (
	LevelDisable = option.LevelDisable
	LevelModel   = option.LevelModel
	LevelSearch  = option.LevelSearch
	MinExpires   = option.MinExpires
	MaxExpires   = option.MaxExpires
)

Variables

This section is empty.

Functions

This section is empty.

Types

type DefaultOption

type DefaultOption = option.DefaultOption

DefaultOption .

type ModelOption

type ModelOption = option.ModelOption

ModelOption .

type Plugin

type Plugin interface {
	//清库
	FlushDB() error
	//删除模型缓存
	DeleteModel(model interface{}, primarys ...interface{}) error
	//删除查询缓存
	DeleteSearch(model interface{}) error
	//insert select update delete 都会跳过缓存处理
	SkipCache() *gorm.DB

	//join 和 子查询, 需要传入模型。
	CreateRelative(...interface{}) *gorm.DB
	SetRelative(*gorm.DB, ...interface{}) *gorm.DB

	//tag
	CreateTag(...interface{}) *gorm.DB
	SetTag(*gorm.DB, ...interface{}) *gorm.DB
	Debug()
}

Plugin .

func AttachDB

func AttachDB(db *gorm.DB, opt *option.DefaultOption, redisOption *option.RedisOption) Plugin

AttachDB .

type RedisOption

type RedisOption = option.RedisOption

RedisOption .

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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