Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I clear rails action cache? (Rails.cache.clear isn't working)

How can I clear rails action cache without restarting the server?

I have a script that updates the Postgres database with new data. I'd like to clear all cached pages after the script completes. but I can't seem to get this to take effect without completely restarting the Heroku server

I have tried without success:

  • Rails.cache.clear => ["/app/tmp/cache/bootsnap-compile-cache", "/app/tmp/cache/bootsnap-load-path-cache"]
  • rails tmp:cache:clear
  • rails assets:clean

config.cache_store is not set explicitly for production, (I've seen mixed information as to what cache is being used by default)

I have ensured that there is no caching in the browser (by disabling caching in dev tools and bypassing the service worker)

I'm using Heroku with ruby 2.5.3, rails 5.2.1.1, and actionpack-action_caching 1.2.0

How do I know it's not being cached at the database layer or with Fragment Caching?

I added logging to the action which only logs the first time a page is accessed after a restart

Update: I removed action caching and instead better optimized the SQL quarries by using eager loading.

Update 2 (writing in retrospect)

I didn't properly understand how Heroku's dyno's work.

Heroku spines up new VM's (dynos) when using heroku run bash, causing any cache clearing not to affect the webserver running on the existing dyno. To ssh into running dynos you'll want to use heroku ps:exec

like image 652
Arye Eidelman Avatar asked Nov 06 '22 22:11

Arye Eidelman


1 Answers

As you can see here, default cache store in production is mem_cache_store. Therefore, you should be able to clear it in different ways, without restarting the sever. Maciej Mensfeld wrote a blog about these ways. I personally use flush_all command in the cache server if Rails.cache.clear does not make effect.

like image 61
Hazhir Derakhshi Avatar answered Nov 12 '22 14:11

Hazhir Derakhshi