g4cache

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2022 License: Apache-2.0 Imports: 13 Imported by: 0

README

g4cache

Add cache on your GORM V2 queries' results is now a breeze

Go Reference GitHub license GitHub go.mod Go version GitHub tag (latest SemVer)

Introduction

  • 中文版说明请参考 README.zh_cn.md
  • Cache Layer for GORM GORM
  • only support Redis cache
  • Golang 1.16 +

Install

go get -u github.com/vodka0629/g4cache

Usage

Init
// define a redisClient
redisClient := redis.NewClient(&redis.Options{
    Addr:         "redis:6379",
    Password:     "",
})
// use redisAdapter to init the cache 
redisAdapter, _ := cache.RedisAdapter.New(redisClient)
plugin, err = G4Cache.New(&CacheOptions{
    Cache:     redisAdapter,
    Ttl:       time.Hour,
    AutoFlush: true,
})
// plugin to gorm
err = db.Use(plugin)
CURD

just follow the old path

Features
  • follow the GORM Plugin interface
  • support simple query / scan / plunk methods, and has-one / belongs-to / has-many / many-to-many relations
  • support cache all 1+n query results
  • depend on GORM db.Statement.Context
    • DONT use db.WithContext to change the context or input/output params to/from the plugin
  • all featured expire strategies
    • support custom TTL
    • support auto flush cache by GORM's create delete update methods
      • need to open AutoFlush option
    • support flush cache manually
      • use FlushTags to expire cache keys by Database's table_name
      • use FlushStruct to expire cache keys by GORM's struct
      • use FlushDB to flush the whole cache database danger!
  • provide all logs info for debug propose
    • depend on GORM's logger ( so you need to plugin to GORM first, and config the logger correctly )
    • set CacheOptions debug : true to enable the debug mode
    • set CacheOptions verbose : true to enable the verbose mode
    • colorful logs
  • adaptability
    • custom serializer/deserializer interface
      • need to implement serializer.Adapter interface
    • custom cache adapter interface
      • need to implement cache.Adapter interface
  • automatic test
TODO
  • full sized sample code ( at present, you can find the samples in g4cache_test.go )
  • setup github CI actions
    • provide code coverage report
  • provide more detailed documents
  • support GORM raw query

Documentation

Index

Constants

This section is empty.

Variables

View Source
var G4Cache = &Plugin{}
View Source
var Helper = &helper{}

Functions

This section is empty.

Types

type CacheOptions

type CacheOptions struct {
	Prefix     string                // prefix for cache layer key
	Ttl        time.Duration         // time to life, for cache data from gorm DB
	Serializer serializer.Serializer // need to support serialize & deserialize
	Cache      cache.Adapter         // cache layer
	AutoFlush  bool                  // auto flush cache by tag and behaviors(delete, update, save)
	Debug      bool                  // show debug [warning], by gorm.DB.Logger
	Verbose    bool                  // show verbose debug [info], by gorm.DB.Logger
}

type Plugin added in v1.0.4

type Plugin struct {
	Options *CacheOptions

	CountQuery int64
	CountHit   int64
	// contains filtered or unexported fields
}

func (*Plugin) AfterChange added in v1.0.4

func (c *Plugin) AfterChange(db *gorm.DB, event string)

func (*Plugin) AfterCreate added in v1.0.4

func (c *Plugin) AfterCreate(db *gorm.DB)

func (*Plugin) AfterDelete added in v1.0.4

func (c *Plugin) AfterDelete(db *gorm.DB)

func (*Plugin) AfterQuery added in v1.0.4

func (c *Plugin) AfterQuery(db *gorm.DB)

func (*Plugin) AfterUpdate added in v1.0.4

func (c *Plugin) AfterUpdate(db *gorm.DB)

func (*Plugin) CustomQuery added in v1.0.4

func (c *Plugin) CustomQuery(db *gorm.DB)

func (*Plugin) Debug added in v1.0.4

func (c *Plugin) Debug(v ...interface{})

func (*Plugin) FetchPluginFromDb added in v1.0.4

func (c *Plugin) FetchPluginFromDb(db *gorm.DB) (*Plugin, error)

func (*Plugin) FlushDB added in v1.0.4

func (c *Plugin) FlushDB(ctx context.Context) error

func (*Plugin) FlushStruct added in v1.0.4

func (c *Plugin) FlushStruct(ctx context.Context, v interface{}) error

func (*Plugin) FlushTags added in v1.0.4

func (c *Plugin) FlushTags(ctx context.Context, v ...string) error

func (*Plugin) GetTableNameByStruct added in v1.0.4

func (c *Plugin) GetTableNameByStruct(v interface{}) (string, error)

func (*Plugin) Hash added in v1.0.4

func (c *Plugin) Hash(db *gorm.DB) string

func (*Plugin) Initialize added in v1.0.4

func (c *Plugin) Initialize(db *gorm.DB) error

func (*Plugin) Name added in v1.0.4

func (c *Plugin) Name() string

func (*Plugin) New added in v1.0.4

func (c *Plugin) New(options *CacheOptions) (*Plugin, error)

func (*Plugin) ResetCount added in v1.0.4

func (c *Plugin) ResetCount()

func (*Plugin) Tags added in v1.0.4

func (c *Plugin) Tags(db *gorm.DB) []string

func (*Plugin) Verbose added in v1.0.4

func (c *Plugin) Verbose(v ...interface{})

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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