class SocialAvatarProxy::Configuration::Memcache
Public Class Methods
new()
click to toggle source
# File lib/social_avatar_proxy/configuration/memcache.rb, line 49 def initialize key do |options| pre_hash = "#{options[:service]}-#{options[:identifier]}" "#{@namespace || "sap/"}#{Digest::MD5.hexdigest(pre_hash)}" end end
Public Instance Methods
read(options = {})
click to toggle source
# File lib/social_avatar_proxy/configuration/memcache.rb, line 9 def read(options = {}) # generate the key file_key = generate_key(options) # if the key exists, serve the data if data = connection.get(file_key) content_type = connection.get("#{file_key}-content-type") || "image/jpeg" mtime = connection.get("#{file_key}-mtime") || Time.now # serve the avatar file AvatarFile.new(file_key).tap do |file| begin file.write(data) file.content_type(content_type) file.mtime(mtime) file.rewind rescue file.close file.unlink raise end end end end
write(file, options = {})
click to toggle source
# File lib/social_avatar_proxy/configuration/memcache.rb, line 32 def write(file, options = {}) # generate the file location file_key = generate_key(options) # write the file connection.set(file_key, file.read) # if the file has a content type if file.respond_to?(:content_type) && file.content_type # write the content type connection.set("#{file_key}-content-type", file.content_type) end # if the file has a modified time if file.respond_to?(:mtime) && file.mtime # write the content type connection.set("#{file_key}-mtime", file.mtime.to_i) end end
Private Instance Methods
connection(value = nil)
click to toggle source
return a connection to Memcache
defaults to a Dalli::Client object
# File lib/social_avatar_proxy/configuration/memcache.rb, line 82 def connection(value = nil) if value unless value.respond_to?(:get) && value.respond_to?(:set) raise ArgumentError, %Q( #{value.inspect} must respond to #set and #get when using as a Memcache connection ) end @connection = value end @connection ||= Dalli::Client.new(@host || "localhost:11211") end
generate_key(*args)
click to toggle source
generate a key given the arguments
# File lib/social_avatar_proxy/configuration/memcache.rb, line 76 def generate_key(*args) @key.yield(*args) end
host(value)
click to toggle source
set which Memcached server to use default: “localhost:11211”
# File lib/social_avatar_proxy/configuration/memcache.rb, line 59 def host(value) @host = value end
key(&block)
click to toggle source
set the key generation block should accept a hash of options
# File lib/social_avatar_proxy/configuration/memcache.rb, line 71 def key(&block) @key = block end
namespace(value)
click to toggle source
set the key namespace default: “sap/”
# File lib/social_avatar_proxy/configuration/memcache.rb, line 65 def namespace(value) @namespace = value end