Skip to content

Instantly share code, notes, and snippets.

@maccman
Created May 30, 2013 17:51
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maccman/5679726 to your computer and use it in GitHub Desktop.
Save maccman/5679726 to your computer and use it in GitHub Desktop.
require 'dalli'
require 'memcachier'
module Sprockets
module Cache
# A simple Memcache cache store.
#
# environment.cache = Sprockets::Cache::MemcacheStore.new
#
class MemcacheStore
def initialize(key_prefix = 'sprockets')
@memcache = Dalli::Client.new
@key_prefix = key_prefix
Dalli.logger = Logger.new('/dev/null')
end
# Lookup value in cache
def [](key)
data = @memcache.get path_for(key)
Marshal.load data if data
end
# Save value to cache
def []=(key, value)
@memcache.set path_for(key), Marshal.dump(value)
value
end
private
def path_for(key)
"#{@key_prefix}:#{key}"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment