Module: Sinatra::Sinatralli::Helpers

Defined in:
lib/sinatralli.rb

Instance Method Summary collapse

Instance Method Details

#cache(key, opts = {}, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/sinatralli.rb', line 7

def cache(key, opts={}, &block)
  return call_fragment(block) unless cache_enabled
  key = "_sinatralli_frag_#{request.host + request.path_info.gsub(/^\//, '')}_#{key.to_s}"
  if cache = cache_get(key)
    @_out_buf << cache
  else
    cache_set(key, call_fragment(block), opts[:expires])
  end
end

#cache_delete(key) ⇒ Object



34
35
36
# File 'lib/sinatralli.rb', line 34

def cache_delete(key)
  dalli.delete(key) if cache_enabled
end

#cache_flushObject



38
39
40
# File 'lib/sinatralli.rb', line 38

def cache_flush
  dalli.flush if cache_enabled
end

#cache_get(key) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/sinatralli.rb', line 25

def cache_get(key)
  return nil unless cache_enabled
  cache = dalli.get(key)
  if cache
    puts "GET: #{key}" if settings.cache_logging
    return Marshal.load(cache)
  end
end

#cache_set(key, value, expires = nil) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/sinatralli.rb', line 17

def cache_set(key, value, expires=nil)
  return value unless cache_enabled
  expires ||= settings.cache_expires
  dalli.set(key, Marshal.dump(value), expires)
  puts "SET: #{key}" if settings.cache_logging
  value
end