Class: Cyclid::UI::Memcache
- Inherits:
-
Object
- Object
- Cyclid::UI::Memcache
- Defined in:
- app/cyclid_ui/memcache.rb
Overview
Simple Memcache caching layer on top of the Memcached client. Keys are retrieved or set via. the cache method; if the key exists it is returned, if the key does not exist the given block is called and its output is stored in Memcached.
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#expiry ⇒ Object
readonly
Returns the value of attribute expiry.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
Instance Method Summary collapse
- #cache(key) ⇒ Object
- #expire(key) ⇒ Object
-
#initialize(args) ⇒ Memcache
constructor
A new instance of Memcache.
Constructor Details
#initialize(args) ⇒ Memcache
Returns a new instance of Memcache.
16 17 18 19 |
# File 'app/cyclid_ui/memcache.rb', line 16 def initialize(args) @server = args[:server] @expiry = args[:expiry] || 3600 end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
14 15 16 |
# File 'app/cyclid_ui/memcache.rb', line 14 def client @client end |
#expiry ⇒ Object (readonly)
Returns the value of attribute expiry.
14 15 16 |
# File 'app/cyclid_ui/memcache.rb', line 14 def expiry @expiry end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
14 15 16 |
# File 'app/cyclid_ui/memcache.rb', line 14 def server @server end |
Instance Method Details
#cache(key) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'app/cyclid_ui/memcache.rb', line 21 def cache(key) begin output = memcached.get(key) rescue Memcached::NotFound output = yield memcached.set(key, output, @expiry) end output end |
#expire(key) ⇒ Object
31 32 33 34 35 36 |
# File 'app/cyclid_ui/memcache.rb', line 31 def expire(key) memcached.delete key true rescue Memcached::NotFound false end |