-
Notifications
You must be signed in to change notification settings - Fork 0
/
httparty_icebox_memcached.rb
49 lines (45 loc) · 1.06 KB
/
httparty_icebox_memcached.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#
# Assumes that MEMCACHE is instantiated, using the gem, use the following lines in environment.rb:
#
# require 'memcached'
# MEMCACHE = Memcached.new("localhost:11211")
#
# Usage:
#
# require 'httparty'
# require 'httparty_icebox'
# require 'httparty_icebox_memcached'
#
# class Foo
# include HTTParty
# include HTTParty::Icebox
# cache :store => 'memcached', :timeout => 3600*1 # cached 1 hour
# end
#
#
require 'httparty_icebox'
include HTTParty::Icebox::Store
class MemcachedStore < AbstractStore
include HTTParty::Icebox
def initialize(options={})
super;self
end
def set(key, value)
res = MEMCACHE.set(key, value, @timeout)
#puts "MemcachedStore.set, key: #{key}, result: #{res}"
Cache.logger.info("Cache: set (#{key})")
true
end
def get(key)
data = MEMCACHE.get(key) rescue nil
Cache.logger.info("Cache: #{data.nil? ? "miss" : "hit"} (#{key})")
data
end
def exists?(key)
data = MEMCACHE.get(key) rescue nil
!data.nil?
end
def stale?(key)
return true unless exists?(key)
end
end