caches icon indicating copy to clipboard operation
caches copied to clipboard

I wonder what logic is used to determine the cache supported by this component

Open gly-hub opened this issue 1 year ago • 2 comments

Your Question

I wonder what logic is used to determine the cache supported by this component? For example, I do a paging query, and the table is constantly inserted data, this component will not appear inconsistent data?

The document you expected this should be explained

Expected answer

gly-hub avatar Feb 23 '24 09:02 gly-hub

Hey @gly-hub, Yes, you are right! If the data is not inserted through gorm's default create/update/delete methods, it would show inconsistent data.

As per v4 of this plugin what you could do is to call your Cacher implementation's Invalidate method, in order to invalidate the cached data.

  • Interface ref: https://github.com/go-gorm/caches/blob/1b3e58f126151b57317c216804daf2018c0faed9/cacher.go#L16C2-L16C12
  • Redis Example Cacher Ref https://github.com/go-gorm/caches?tab=readme-ov-file#cacher-example-redis

If that's not possible in your use-case, you could set cache lifetime (let's say 5mins), this way you ensure that in the worst case you are going to have inconsistency for ~5min max.

TL;DR; Some databases (i.e. MySQL) have internal caching mechanism in place. Ref: https://dev.mysql.com/doc/refman/5.7/en/query-cache.html If none of these solved your problem you could look into some sort of database proxy (i.e. https://proxysql.com/ ) which can be used to achieve this goal.

ktsivkov avatar Feb 23 '24 13:02 ktsivkov

Thank you very much for solving my doubts. There is also a bit of confusion: does any executed sql store a state in memory? If the data is changed across services, can the data cache be cleaned efficiently?

gly-hub avatar Feb 26 '24 07:02 gly-hub