Class: SocialAvatarProxy::Configuration::Memcache

Inherits:
Cache
  • Object
show all
Defined in:
lib/social_avatar_proxy/configuration/memcache.rb

Instance Method Summary collapse

Methods inherited from Cache

#configure, #disabled?, #enabled?, #fetch

Constructor Details

#initializeMemcache

Returns a new instance of Memcache.



49
50
51
52
53
54
# 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

Instance Method Details

#read(options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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 = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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