Class: Accelerator

Inherits:
Object
  • Object
show all
Defined in:
lib/accelerator.rb

Instance Method Summary collapse

Constructor Details

#initialize(host = "localhost:11211") ⇒ Accelerator

Returns a new instance of Accelerator.



8
9
10
# File 'lib/accelerator.rb', line 8

def initialize(host="localhost:11211")
  @memc = Memcached.new(host)
end

Instance Method Details

#delete(uri) ⇒ Object



12
13
14
# File 'lib/accelerator.rb', line 12

def delete(uri)
  @memc.delete(key(uri))
end

#expire(uri, ttl = nil) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/accelerator.rb', line 16

def expire(uri, ttl=nil)
  if data = get_and_set_time(uri)
    data[:ttl]   = ttl if ttl
    data[:time] -= data[:ttl]
    @memc.set(key(uri), data.to_json, 604800, false)
  end
end

#get(uri) ⇒ Object



24
25
26
27
28
# File 'lib/accelerator.rb', line 24

def get(uri)
  if data = get_and_parse(uri)
    [ data.delete(:body), data ]
  end
end

#set(uri, body, ttl = nil) ⇒ Object



30
31
32
33
34
35
# File 'lib/accelerator.rb', line 30

def set(uri, body, ttl=nil)
  data   = get_and_set_time(uri)
  data ||= { :time => Time.now.to_i, :ttl => ttl || 10 }
  data[:body] = body
  @memc.set(key(uri), data.to_json, 604800, false)
end