Class: Lawnchair::StorageEngine::Abstract

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

Direct Known Subclasses

InProcess, Redis

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.data_storeObject (readonly)

Returns the value of attribute data_store.



5
6
7
# File 'lib/storage_engine/abstract.rb', line 5

def data_store
  @data_store
end

Class Method Details

.computed_key(key) ⇒ Object



33
34
35
36
37
# File 'lib/storage_engine/abstract.rb', line 33

def computed_key(key)
  raise "Missing key" if key.nil? || key.empty?
  prefix = "Lawnchair"
  "#{prefix}:#{key}"
end

.db_connection?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/storage_engine/abstract.rb', line 39

def db_connection?
  true
end

.fetch(key, options = {}, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/storage_engine/abstract.rb', line 11

def fetch(key, options={}, &block)
  start_time = Time.now
  if value = get(key, options)
    log("HIT", key, Time.now-start_time)
    return value
  else
    value = block.call
    set(key, value, options)
    log("MISS", key, Time.now-start_time)
    return value
  end
end

.get(key, options = {}) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/storage_engine/abstract.rb', line 24

def get(key, options={})
  if options[:raw]
    data_store[computed_key(key)]
  else
    value = data_store[computed_key(key)]
    value.nil? ? nil : Marshal.load(value)
  end
end

.log(message, key, elapsed) ⇒ Object



43
44
45
46
# File 'lib/storage_engine/abstract.rb', line 43

def log(message, key, elapsed)
  Lawnchair.redis.hincrby(message, computed_key(key), 1)
  ActionController::Base.logger.info("Lawnchair Cache: #{message} (%0.6f secs): #{key}" % elapsed) if defined? ::ActionController::Base
end