Class: Cyclid::UI::Memcache

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#clientObject (readonly)

Returns the value of attribute client.



14
15
16
# File 'app/cyclid_ui/memcache.rb', line 14

def client
  @client
end

#expiryObject (readonly)

Returns the value of attribute expiry.



14
15
16
# File 'app/cyclid_ui/memcache.rb', line 14

def expiry
  @expiry
end

#serverObject (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