Ongoing improvements in Caches.rb

Posted by yrashk

I’ve spent few hours improving Caches.rb recently. While it is not still released (i.e. no official gem, only svn trunk), I’d like to disclose recent changes and share with my plans about it.

So, what’s done?

  • First of all, it was greatly refactored. The most significant change is that storages are not responsible for caching algorithms anymore. Each storage has only few (like 5) simple API methods that needs to be defined. You can look into the sources (svn://verbdev.com/rubylibs/caches.rb/trunk) to see how they are designed now. Please be aware that API method names are most likely to be changed very soon. I don’t like current naming scheme.
  • One more caching module added—Caches::Storage::Global, which stores cache in a global variable cache.
  • Then, I got rid of CachesConfig. If you need to specify a storage for your instance, just do instance_cache_storage StorageModule. CachesStorage::Instance is used by default (which stores cache in an object’s instance)
  • Caching static methods was requested by caches.rb users several times, so here you are:
 
 class SomeClass
   def self.static_method
    ...
   end
  class_caches :static_method
 end
 
You can also specify storage for static methods: class_cache_storage StorageModule
  • Since I was adding some Rails-specific behavior recently I’ve got some feedback on Rails-specific usage of Caches.rb. Apparently, if you cache static methods in your rails app and you’re running in development mode, your class is going to be reloaded by Rails and hence you will lose your changes. That’s why Caches::Storage::Global has appeared. So, now you can use the following pattern:
 
 class MyModel < ActiveRecord::Base
   def self.static_method
    ...
   end
  class_cache_storage Caches::Storage::Global 
  class_caches :static_method
 end
 
  • I’ve added experimental method caches? which should be used to check class whether it caches this or that method. I haven’t decided whether it makes any sense, so may be I will drop it.

Seems that it’s all.

Now, few words on my plans. I plan to:

  • perform a code cleanup, refactor it to the extent possible
  • invent a better naming schema for storage API (though I’m still not very satisfied with it)
  • improve specs, perform more tests
  • release new version
  • think about adding more sophisticated storage modules, like support for memcached.

Update: I’ve performed some refactoring and added memcached support (Caches::Storage::MemCached). Everything is available in svn trunk for testing purposes. I plan to release new official version (0.4.0) soon.