Module: Sinatra::Salli::Helpers

Defined in:
lib/salli/helpers.rb

Instance Method Summary collapse

Instance Method Details

#cache(nade, expires = nil, &maybe) ⇒ Object

Fragment cache

Raises:

  • (ArgumentError)


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

def cache(nade, expires=nil, &maybe)
  raise ArgumentError, 'No block was used' unless block_given?
  return call_me(maybe) unless all_systems_go?
  # Push to output buffer
  if value = cache_get(nade)
    buff? ? @_out_buf << value : value
  else
    cache_set(nade, call_me(maybe), expires)
  end
end

#cache_delete(key) ⇒ Object

Delete by key



45
46
47
# File 'lib/salli/helpers.rb', line 45

def cache_delete(key)
  client.delete(keygen(key.to_s)) if all_systems_go?
end

#cache_flushObject

Wipe out



52
53
54
# File 'lib/salli/helpers.rb', line 52

def cache_flush
  client.flush if all_systems_go?
end

#cache_get(key) ⇒ Object

Fetch



33
34
35
36
37
38
39
40
# File 'lib/salli/helpers.rb', line 33

def cache_get(key)
  return nil unless all_systems_go?
  key = keygen(key.to_s)
  if value = client.get(key)
    Salli.log('get', key) if settings.cache_logging
    Marshal.load(value)
  end
end

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

Key / value



21
22
23
24
25
26
27
28
# File 'lib/salli/helpers.rb', line 21

def cache_set(key, value, expires=nil)
  return value unless all_systems_go?
  key = keygen(key.to_s)
  expires ||= settings.cache_expires
  client.set(key, Marshal.dump(value), expires)
  Salli.log('set', key) if settings.cache_logging
  value
end