I'm not sure if this is by design or not but I was expecting that when caching is configured for a cell, that details of cache hits, misses and deletions would be written to the log file (at least in development).
Is this something that is broken, intentionally missing or just as yet, not implemented and would be accepted as a patch?
We once had this in earlier versions but removed it as nobody seemed to take advantage of this feature. I'd be happy to see a patch, however, maybe we should introduce some cell/debugging.rb module responsible for mixing in development-specific behaviour into cells (e.g. caching logging, template finding output, etc). If you send a patch for logging I can take care of the rest. Thanks!
Assuming we only need to support Rails 3 we can use ActiveSupport::Notifications (RailsCast).
I had a quick play with it and simply modifying #expire_cache_key and #render_state in Cell::Caching::ClassMethods to include instrumentation seems to do the job:
def expire_cache_key(key, *args)
ActiveSupport::Notifications.instrument("expire_fragment.action_controller", :key => key) do
def render_state(state, *args)
return super(state, *args) unless self.class.cache?(state)
key = self.class.state_cache_key(state, call_state_versioner(state, *args))
options = self.class.cache_options[state]
ActiveSupport::Notifications.instrument("read_fragment.action_controller", :key => key) do
self.class.cache_store.fetch(key, options) do
ActiveSupport::Notifications.instrument("write_fragment.action_controller", :key => key) do
Note that the instrumentation in #render_state does result in both a read and write being logged the when the cache is empty, so the logic may want to be changed to conditionally do a cache read or write instead of a fetch.
I wasn't sure how to go about testing this however as I couldn't see any caching tests.
Yeah, notifications seem to be a good choice, although I'd like to push stuff like this in a Cell::Debugging module. Cash tests are here: https://github.com/apotonick/cells/blob/master/test/rails/caching_test.rb
Is the Cell::Debugging module feature going forward? We'd be also interested in having such a logging feature. We just started playing caching our cells and its difficult to understand what is happening without some logging.
introduce Cell::Caching::Notifications, fixes #64.
Cool, only 3 years later this got implemented. Thanks @fractious I basically copied your code to Caching::Notifications.
Do i need to wire the notifications to the log output myself? As the cache output is pretty usefull for a dev.
... yes i finally made it to try cells
@schorsch Yo! Good to see you here!
Here is a subscribe example for read_fragment.action_controller and write_fragment.action_controller
ActiveSupport::Notifications.subscribe('read_fragment.action_controller') do |name, start, finish, id, payload|
name # => String, name of the event (such as 'read_fragment.action_controller' from above)
start # => Time, when the instrumented block started execution
finish # => Time, when the instrumented block ended execution
id # => String, unique ID for this notification
payload # => Hash, the payload
@schorsch contact me if you need more support