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

Document how to expire cache manually #445

Open
designedev opened this issue Feb 29, 2020 · 5 comments
Open

Document how to expire cache manually #445

designedev opened this issue Feb 29, 2020 · 5 comments

Comments

@designedev
Copy link

sometimes i want to expire(remove) cache manually. for example,

in rails console:
val = ValueModel.fetch(1)
(and log shows like that.. SQL: select * from value_model where id = 1 limit 1)

and this record is updated at other environment(ex: raw sql from DBMS)

then, use statement again in rails console.
val2 = ValueModel.fetch(1)
and then rails console shows, cache hit, and nothing changed.

in this situation, can i expire this cache manually?

@dylanahsmith
Copy link
Contributor

When a record is saved, expire_cache is the method called on it to expire the cache keys for it. That includes:

  • the primary index key for the record
  • the primary index for parent records that embed the record
  • cached attributes, including for secondary indices

If you are trying to load the record (e.g. because the raw SQL update is for performance reasons) then there is an expire_primary_key_cache_index(id) class method on the model that can be used to expire just the primary index for that record. If the record is embedded in parent records, the same method could be used on the parent model to expire records that embed it. There isn't currently a class method exposed to expire cached attributes, but we would be open to adding that if it were needed.

@designedev
Copy link
Author

@dylanahsmith thank you.
but sadly i use version 0.5.1 because of ruby version issue.
and i can`t use expire_primary_key_cache_index.

so i used some workaround because method expire_cache if private 👯‍♂

if variable "target" is invalid cache,

cache_key = target.primary_cache_index_key
IdentityCache.cache.delete(cache_key)
#cache expired. in this case, i need to expire primary index only.

@khiav223577
Copy link

I find there is rails_cache_key in the class, so we could expire the cache without loading the model!

IdentityCache.cache.delete(MyModel.rails_cache_key(my_id))

@dylanahsmith
Copy link
Contributor

If you are on the latest version of the code, then MyModel.expire_primary_key_cache_index(my_id) can be used.

@dylanahsmith dylanahsmith changed the title is there way to expire identifycache manually? Document how to expire cache manually Apr 15, 2021
@dylanahsmith
Copy link
Contributor

Oh right, I meant to leave this open to remember to document this. Updated the title accordingly

@dylanahsmith dylanahsmith reopened this Apr 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants