Module: ActiveSP::PersistentCachingConfig

Included in:
Connection
Defined in:
lib/activesp/persistent_caching.rb

Instance Method Summary collapse

Instance Method Details

#configure_persistent_cache(&blk) ⇒ Object

Configures the scope of the persistent cache. The default scope of the cache is the Connection object, i.e., each connection has its own cache. For example you can use this to make thread local caches. Note that the cache is not actually thread safe at this point so this may not be such a bad idea.

Caching in ActiveSP at the moment is very aggressive. What this means that everything you ever accessed will be cached. You can override the cache for a particular object by calling Base#reload on it. One advantage of this caching strategy is that every time you access an object in SharePoint, it is guaranteed to be the same object in Ruby as well, irrespective of how you obtained a reference to that object. This eliminates a whole slew of issues, but you need to be aware of this.

This method expects a block to which a new cache object is passed. The idea is that you store this cache object in a place that reflects the scope of your cache. If you already had a cache object stored for you current scope, you do not do anything with the cache object. The cache object that the block returns is the cache that will be used. Note that the block is called everytime ActiveSP needs the cache, so make it as efficient as possible. The example below illustrates how you can use the ||= operator for this to get a thread local cache.

You can use this block to return a cache of your own. A cache is only expected to have a lookup method to which the cache key is passed (do not assume it is an integer or a string because it is not) as parameter and is expected to return the value for that key. In case of a cache miss, the method should yield to retrieve the value, store it with the given key and return the value. You can use this to plug in a cache that has a limited size, or that uses weak references to clean up the cache. The latter suggestion is a lot safer than the former!

Examples:

How to configure caching strategy

c = ActiveSP::Connection.new(:login => l, :password => p, :root => r)
c.configure_persistent_cache { |cache| Thread.current[:sp_cache] ||= cache }


101
102
103
104
105
106
107
108
# File 'lib/activesp/persistent_caching.rb', line 101

def configure_persistent_cache(&blk)
  @last_persistent_cache_object = PersistentCache.new
  class << self ; self ; end.send(:define_method, :persistent_cache) do
    cache = blk.call(@last_persistent_cache_object)
    @last_persistent_cache_object = PersistentCache.new if cache == @last_persistent_cache_object
    cache
  end
end