Class: WurflCloud::Cache::Memcached

Inherits:
Object
  • Object
show all
Defined in:
lib/wurfl_cloud/cache/memcached.rb

Instance Method Summary collapse

Constructor Details

#initialize(options, environment) ⇒ Memcached

Returns a new instance of Memcached.



17
18
19
20
21
22
23
# File 'lib/wurfl_cloud/cache/memcached.rb', line 17

def initialize(options, environment)
  opts = options.clone
  server = opts.delete(:server)
  @prefix = opts.delete(:prefix)
  opts[:expires_in] ||= 86400 
  @memcached = Dalli::Client.new(server, opts)
end

Instance Method Details

#[](key) ⇒ Object

Should return the value stored for the key, nil if the key is not in cache The Memcached cache reads from the mamcached server



54
55
56
# File 'lib/wurfl_cloud/cache/memcached.rb', line 54

def [](key)
  @memcached.get("#{key_prefix}#{Digest::MD5.hexdigest(key)}")
end

#[]=(key, value) ⇒ Object

Stores the value for the key in the mamcached server



59
60
61
# File 'lib/wurfl_cloud/cache/memcached.rb', line 59

def []=(key, value)
  @memcached.set("#{key_prefix}#{Digest::MD5.hexdigest(key)}", value)
end

#key_prefixObject



29
30
31
# File 'lib/wurfl_cloud/cache/memcached.rb', line 29

def key_prefix        
  "#{prefix}:#{mtime}:"
end

#mtimeObject



33
34
35
# File 'lib/wurfl_cloud/cache/memcached.rb', line 33

def mtime
  @memcached.get("#{prefix}mtime")
end

#mtime=(new_mtime) ⇒ Object



37
38
39
# File 'lib/wurfl_cloud/cache/memcached.rb', line 37

def mtime=(new_mtime)
  @memcached.set("#{prefix}mtime", new_mtime)
end

#prefixObject



25
26
27
# File 'lib/wurfl_cloud/cache/memcached.rb', line 25

def prefix
  "#{(@prefix.nil? || @prefix=="") ? "": "#{@prefix}:"}"
end

#validate(current_mtime) ⇒ Object

Validates the cache checks if the cache is still valid with respect to a new mtime received from the server if the new mtime is different from the one cached then it overwrites the current key prefix thus actually invalidationg the cache



45
46
47
48
49
50
# File 'lib/wurfl_cloud/cache/memcached.rb', line 45

def validate(current_mtime)
  old_mtime = self.mtime
  if !old_mtime || old_mtime!=current_mtime
    self.mtime = current_mtime
  end
end