gorm-cache
gorm-cache copied to clipboard
gorm v2的即插即用、无需修改代码的旁路缓存。An easy-to-use look-aside cache solution for gorm v2 users.
1. 全局配置添加`KeyPrefix`用于配置缓存Key前缀 2. 适配Key前缀配置更改 3. 删除`util/definition.go`内的`GormCachePrefix`全局变量 4. 如果没有填写Key前缀配置,则使用默认的`gormcache`前缀,兼容旧版本
因为所有的数据混杂在同个Redis实例里面,所以希望可以指定Redis的Key前缀
Count操作会异常,原因是gorm的Count的返回类型是int64,会提示反射类型是uint, 直接异常 panic: reflect: call of reflect.Value.Field on uint Value goroutine 29 [running]: reflect.Value.Field({0xdd5ce0, 0xc0004b4600, 0x187}, 0x0) C:/Program Files/Go/src/reflect/value.go:1278 +0x269 gorm.io/gorm/schema.(*Field).setupValuerAndSetter.func2({0xf95ba8, 0x157aca0}, {0xdd5ce0, 0xc0004b4600, 0x187}) C:/Users/Administrator/go/pkg/mod/gorm.io/[email protected]/schema/field.go:454 +0x117 github.com/Pacific73/gorm-cache/cache.getObjectsAfterLoad(0xc000212690) C:/Users/Administrator/go/pkg/mod/github.com/!pacific73/[email protected]/cache/helpers.go:249 +0x3d6...
查询不存在记录时,会一直无法命中缓存。建议加一个选项开启后。不存在数据时,也缓存一下。
**I believe that you can read Chinese without barriers, if you have any difficulty in understanding the following, please let me know** 通常golang的习惯,json tag和gorm tag都会写在同一个struct里 我希望给用户交付的Json需要忽略某些字段的时候就会在struct里设置json:"-"的tag来忽略这个字段,但是同时这个字段又是要参与逻辑的,比如password,salt之类的 同时这个struct也是gorm要返回的,这时由于cache的序列化是基于encoding/json的,就会同时忽略json:"-"的字段,也就是逻辑判断字段会少内容 解决方案:我目前的做法是引入[jsoniter](https://github.com/json-iterator/go)来替换掉encoding/json,因为jsoniter允许指定[tagkey](https://github.com/json-iterator/go/blob/71ac16282d122fdd1e3a6d3e7f79b79b4cc3b50e/config.go#L23),并且jsoniter号称性能也比encoding/json要快,询问一下这个变更是否适合合并入主线
add WithOptiot go build -tags=jsoniter
for issue #6 小体量应用经常会不特意构建view model来特意处理和前端得交互,将json tag和gorm tag写在同一个struct里 这时cache和json混用tag就会造成不向前端暴露的字段在程序里也无法获取,所以单独指定gorm-cache的tag key 目前没有发现使用gcache作为tag的常用库,但是有一个缓存库名为[gcache](https://github.com/bluele/gcache)
Support for using same redis space when running multiple servers. When running multiple app servers for the same app, all the servers are connected same DB. So it make sense...
在Redis集群模式下会得到出现Lua脚本加载错误 ``` ERR bad lua script for redis cluster, all the keys that the script uses should be passed using the KEYS array, and KEYS should not be in expression...