Created
October 9, 2013 10:23
-
-
Save the8472/6899185 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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