views:

27

answers:

2

Long story short: I have some controller logic that requests a value from the cache X times, expecting to get a different value on subsequent requests if it has in fact changed on the cache server in between cache requests (this is all within the context of a single HTTP request).

However it seems that Rails MemCacheStore wraps itself with Strategy::LocalCache so no matter how many times I request the value it will always return the first value it pulled from the server regardless if that value has changed on the server in between requests.

I was hoping there was some undocumented :force option for the read() method, but no such luck.

So my next hope was to monkey-patch it somehow to get what I needed but I'm stumped there.

Any advice?

+1  A: 

I am not certain if this will work but worth a try:

cache.send(:bypass_local_cache) { cache.read("bar")}

Where bar is the key you are trying to access.

Note: bypass_local_cache is a private method.

KandadaBoggu
Unfortunately bypass_local_cache was introduced in activesupport-3.0.0 and as noted in the title I'm stuck on 2.3.x. However I might be able to back-port it so I'll look into that. Thanks.
Teflon Ted
+1  A: 

Got it working (thanks to @KandadaBoggu suggestion) by back-porting and monkey-patching the bypass_local_cache feature from activesupport 3.0

module ActiveSupport
  module Cache
    module Strategy
      module LocalCache
        protected
        def bypass_local_cache
          save_cache = Thread.current[thread_local_key]
          begin
            Thread.current[thread_local_key] = nil
            yield
          ensure
            Thread.current[thread_local_key] = save_cache
          end
        end
      end
    end
  end
end
Teflon Ted
You can avoid the nested statements by referring to module using the full name, i.e. `module ActiveSupport::Cache::Strategy::LocalCache`.
KandadaBoggu