Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix -cache=true insert no clean cache #1672

Merged
merged 2 commits into from Mar 23, 2022
Merged

fix -cache=true insert no clean cache #1672

merged 2 commits into from Mar 23, 2022

Conversation

Mikaelemmmm
Copy link
Member

在生成model代码指定-cache=true时,如果表中只有主键没有其他唯一索引时候,insert是不删除缓存的,但是findone会查缓存,这样会有问题在findone不存在的数据时候会缓存* ,那先findone在insert就会有问题

When the generated model code specifies - cache = true, if there is only a primary key in the table and there is no other unique index, the insert will not delete the cache, but findone will check the cache, which will cause problems. If there is no data in findone, it will cache *, then findone will have problems in the insert first

@kevwan kevwan merged commit d8054d8 into zeromicro:master Mar 23, 2022
fynxiu pushed a commit to fynxiu/go-zero that referenced this pull request Apr 1, 2022
* fix -cache=true insert no clean cache

* fix -cache=true insert no clean cache
@weihxa
Copy link

weihxa commented Dec 12, 2022

在insert前先执行了findone, 这个问题貌似没有彻底解决
例如我通过id等于1 来查询,redis中缓存是cache:database:table: id: 1: *

然后执行insert方法插入数据。虽然insert方法中有删除缓存的方法,但是就生成的缓存key为 cache:database:table: id: 0 并没有命中实际缓存的key所以redis中并没有删除那个缓存。这时候再次findone数据还是不对的。
具体看下图,原因可能是在insert中 生成删除key时使用的是data.id字段,但此时场景为插入新数据传入的数据中id字段为零值即0,导致生成的key错误

Execute findone before insert, this problem seems to have not been completely resolved
For example, if I query by id equal to 1, the cache in redis is cache:database:table: id:1:*

Then execute the insert method to insert data. Although there is a method to delete the cache in the insert method, the generated cache key is cache:database:table: id:0 and does not hit the actual cache key, so the cache is not deleted in redis. At this time, the findone data is still wrong again.
Specifically, look at the figure below. The reason may be that the data.id field is used when generating and deleting the key in insert, but at this time, the id field in the incoming data for inserting new data is zero, that is, 0, resulting in an error in the generated key.

image

我使用的版本:
Golang版本1.19
go-zero版本v1.4.2
goctl版本1.4.2

The version I use:
Golang version 1.19
go-zero version v1.4.2
goctl version 1.4.2

@Mikaelemmmm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants