Skip to content

Instantly share code, notes, and snippets.

@nkallen
Created November 25, 2008 00:31
Show Gist options
  • Save nkallen/28715 to your computer and use it in GitHub Desktop.
Save nkallen/28715 to your computer and use it in GitHub Desktop.
module ActsAsCached
class LocalCache
delegate :respond_to?, :to => :@remote_cache
def initialize(remote_cache)
@remote_cache = remote_cache
end
def cache_locally
@remote_cache = LocalCacheBuffer.new(original_cache = @remote_cache)
yield
ensure
@remote_cache = original_cache
end
def method_missing(method, *args, &block)
@remote_cache.send(method, *args, &block)
end
end
class LocalCacheBuffer
delegate :respond_to?, :to => :@remote_cache
def initialize(remote_cache)
@local_cache = {}
@remote_cache = remote_cache
end
def get(key, *options)
if @local_cache.has_key?(key)
@local_cache[key]
else
@local_cache[key] = @remote_cache.get(key, *options)
end
end
def set(key, value, *options)
@remote_cache.set(key, value, *options)
@local_cache[key] = value
end
def add(key, value, *options)
result = @remote_cache.add(key, value, *options)
if result == "STORED\r\n"
@local_cache[key] = value
end
result
end
def delete(key, *options)
@remote_cache.delete(key, *options)
@local_cache.delete(key)
end
def method_missing(method, *args, &block)
@remote_cache.send(method, *args, &block)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment