Rails cache will block the clients' request while they are making -
prevent requests blocked caching , auto regenerate fresh caches
we can make rails cache
, , set expires time that
rails.cache.fetch(cache_key, expires_in: 1.minute) `fetch_data_from_mongodb_with_complex_query` end
somehow, when new request comes in, expiration happens, , request block. question is, how can avoid kind of situation? basically, want give previous cache client's request while rails making cache.
as shown in the expected behaviour diagram, second request cache 1
not cache 2
, although rails making cache 2
. therefore, user won't have spend time on making new cache. so, how can automatically regenerate caches without users' request trigger it?
expected behaviour
cache snippet
cache_key = "#{__callee__}" rails.cache.fetch(cache_key, expires_in: 1.hour) all.order_by(updated_at: -1).limit(max_rtn_count) end
update
how cached keys in command ?
because cached query can generate composition of start_date
, end_date
, depature_at
, arrive_at
.
it's not possible invalidate cached keys manually.
how cache keys, refresh in rake task
using expiration
tricky once cached object expires, won't able fetch expired value.
the best practice decouple cache-refreshing process end user traffic. need rake task populates/refreshes cache , run task cron. if reason, job doesn't run, cache expire , users incur additional time fetch data.
however, if dataset large refresh/load of @ once, you'd have use different cache-expiration policy (you update expiration time after every cache-hit).
alternatively, disable cache expiration , use different indicator (such time) determine if object in cache date or stale. if stale, use asynchronous activejob
worker queue job update cache. stale data returned user , cache updated in background.
Comments
Post a Comment