Skip to content

Instantly share code, notes, and snippets.

@the8472
Created October 9, 2013 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save the8472/6899185 to your computer and use it in GitHub Desktop.
Save the8472/6899185 to your computer and use it in GitHub Desktop.
require "dalli"
module ActiveSupport::Cache
# unlike the "official" dalli store this subclasses the abstract Store class which handles nil values properly
# also implement the local cache strategy to avoid network hits
class SimpleDalliStore < Store
def initialize(*addresses)
opts = addresses.extract_options!
opts[:compress] ||= opts[:compression]
servers = if addresses.empty?
nil # use the default from Dalli::Client
else
addresses
end
@dalli = Dalli::Client.new(servers, opts)
super(opts)
extend Strategy::LocalCache
end
def clear(options = nil)
@dalli.flush_all
rescue Dalli::DalliError => e
Rails.logger.error("DalliError: #{e.message}")
false
end
def reset
@dalli.reset
end
protected
def read_entry(key, options) # :nodoc:
entry = @dalli.get(key, options)
rescue Dalli::DalliError => e
Rails.logger.error("DalliError: #{e.message}")
nil
end
# Write an entry to the cache.
def write_entry(key, value, options) # :nodoc:
expires_in = options[:expires_in]
@dalli.set key, value, expires_in, options
rescue Dalli::DalliError => e
Rails.logger.error("DalliError: #{e.message}")
false
end
# Delete an entry from the cache.
def delete_entry(key, options) # :nodoc:
@dalli.delete(key)
rescue Dalli::DalliError => e
Rails.logger.error("DalliError: #{e.message}")
false
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment