cache_shim

package module
v0.0.0-...-aee1bd4 Latest Latest
Warning

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

Go to latest
Published: May 25, 2023 License: MIT Imports: 3 Imported by: 0

README

cache_shim 缓存适配器

一个数据库与内存之间的缓存适配器,运用了控制反转的思想。引入之后无需重复为每种实体实现相同的缓存策略

框架背景

大多数情况下我们在代码中添加Redis作为缓存中间件之后都会多出很多代码来。每给一个实体添加缓存,都需要加一大堆代码。久而久之,代码库变得非常冗杂而且难看。

举个栗子,一个带缓存的实体的查询逻辑的伪代码:

func (e *TestS) Select() {
	e, ok := SelectFromRedis()
	if !ok {
		et, ok := SelectFromDB()
		if !ok {
			return
		}
		e = et

		go func() {
			SaveToCache(e)
		}()

		return
	}
	return
}

假如一个服务中有多个需要维护到缓存中的实体,再加上删除,修改等其他逻辑,就会出现非常多的相似代码,导致维护十分麻烦,而且复用性极低。即使写成工具类,当有新服务加入时,也需要将代码拷来拷去,显得十分不专业。

因此,写了这个缓存适配器框架,使用控制反转的思想,代码中的实体只需实现指定接口,删除、修改、查询时只需要调用该库的对应方法即可,无需关心具体实现,框架会自动完成缓存相关逻辑。

使用示例

  1. 缓存客户端实现CacheClintImpl接口
type CacheClintImpl interface {
	Del(key string) (int64, error)
	SetString(key, value string) error
	GetString(k string) (string, error)
}

实现参考redis_storage.go

  1. 初始化缓存客户端并将缓存客户端示例初始化到框架中
redis_ex.Init()
cache_shim.InitCacheClient(&redis_ex.RDB)
  1. 需要做缓存的实体实现CacheTypeImpl接口
type CacheTypeImpl interface {
	CacheKey() string

	Delete() error
	Select() error
	Update() error
}

实现参考user_ex.go

  1. 向数据库中插入数据之后,使用框架提供的查询方法查询

因为控制反转,这个时候不需要直接调用实体的Select()方法,直接调用cache-shim包中的Select()函数即可,框架将自动维护缓存中的数据。

_ = t1.Insert()
t, err := cache_shim.Select[*db_ex.UserEx](t1)
fmt.Printf("t.type: %T\tt: %v\terr: %v",t,t,err)

代码参考main.go

修改、删除同理

TODO

  • 支持传入自定义序列化、反序列化函数
  • CacheClint生成的实体变量改为由用户存储

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete

func Delete(cacheShim *CacheShim, entity CacheType) error

Delete Delete

func Insert

func Insert(_ *CacheShim, entity CacheType) error

Insert Insert

func Select

func Select[T CacheType](cacheShim *CacheShim, entity CacheType) (res T, err error)

Select by primary key

func Update

func Update(cacheShim *CacheShim, entity CacheType) error

Update 延时双删策略

Types

type CacheClint

type CacheClint interface {
	Del(key string) (int64, error)
	SetString(key, value string, expire int) error
	GetString(k string) (string, error)
}

CacheClint 缓存客户端接口定义

type CacheShim

type CacheShim struct {
	CacheClient CacheClint
	// contains filtered or unexported fields
}

func InitCacheClient

func InitCacheClient(cacheClient CacheClint) *CacheShim

type CacheType

type CacheType interface {
	CacheKey() string
	Expiration() int

	Insert() error
	Delete() error
	Select() error
	Update() error
}

CacheType 需要缓存的实体接口定义

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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