liyuan1125 / gorm-cache Goto Github PK
View Code? Open in Web Editor NEWgorm-cache
gorm-cache
新版本的打算解析sql的语句,使用tableName
+where
+group
+order
+limit
等其他子句,生成一个key
例如
SELECT * FROM users WHERE username = 'gorm' order by id DESC limit 1;
以上sql生成的缓存 hashKey
大概为
users.username.gorm.order.id.desc.limit.1
然后使用go生成一个hash值,再缓存的redis内
由于gorm内获取不到where 等字句,所以想使用 vitess
如果不使用子句,例如order by group的话,可以在更新与删除时将缓存删除
例如users.username.gorm
这在更新的时候
UPDATE users SET avatar = '1.png' WHERE username = 'gorm';
或者删除数据的时候
DELETE FROM users WHERE username = 'gorm';
以上两个sql都可以生成users.username.gorm(tableName+where)
这时候可以删除缓存,但是这面临到一个问题,就是数据可能不对
例如
id | username | avatar |
---|---|---|
1 | gorm | 1.png |
2 | gorm | 2.png |
SELECT * FROM `users` WHERE username = 'gorm' ORDER BY id DESC limit 1;
SELECT * FROM `users` WHERE username = 'gorm' ORDER BY id ASC limit 1;
SELECT * FROM `users` WHERE username = 'gorm' ORDER BY id ASC;
以上三个语句查询到的数据有可能不是一样的,但是缓存的数据就会有问题,
所以目前还在考虑用哪个方案
希望 go-redis 可以支持 V9 版本
兄弟,我没有用你的库,但是我仔细看了一下源码,有很多参考学习意义。
我看go-zero的缓存处理,query raw的时候创建缓存,在update的时候更新缓存,remove的时候就清除缓存,query raws的时候不进行缓存。
库中有很多可以优化的地方。
` // 调用 Gorm的方法生产SQL
callbacks.BuildQuerySQL(tx)
// 是否有自定义key
if key, hasKey = FromKey(ctx); !hasKey {
key = p.prefix + generateKey(tx.Statement.SQL.String())
}`
未定义key的时候,使用tx.Statement.SQL.String()计算出key,但是callbacks.BuildQuerySQL(tx)处理后,sql语句的值还未放进去。比如,SELECT * FROM enum
WHERE type_id = ?,是这样的状态。这样就导致外部传的不同的type_id,但是key计算的却是同一个。太坑了。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.