Our #fetch_jwks method only returns the JSON response if the request was successful, and nil otherwise. Meaning we can cater for any failed response by leaving the current value in the cache, so we always have the latest successful response we've received.
The cache will be cleared even if the response is not successful and fetch_jwks returns nil.
Rails.cache.fetch("JWKS_CACHE_KEY", force:true, skip_nil:true) do
Time.now.to_s
end
=> "2021-08-31 11:22:32 +0200"
Rails.cache.fetch("JWKS_CACHE_KEY", force:true, skip_nil:true) donilend
=> nil
Rails.cache.fetch("JWKS_CACHE_KEY")
=> nil
Robert Pankowecki Nice catch. It looks like you're correct. An oversight while writing this.
The issue here is using force: true and skip_nil: true at the same time.
force is always going to force a cache write. So actually using them together would require splitting up the logic.
Robert Pankowecki
Using
force: true, skip_nil: truedoes not work the way you described it in:
The cache will be cleared even if the response is not successful and fetch_jwks returns nil.
Rails.cache.fetch("JWKS_CACHE_KEY", force: true, skip_nil: true) do Time.now.to_s end => "2021-08-31 11:22:32 +0200" Rails.cache.fetch("JWKS_CACHE_KEY", force: true, skip_nil: true) do nil end => nil Rails.cache.fetch("JWKS_CACHE_KEY") => nil