Module: Backends::Helpers::CachingHelper

Defined in:
lib/backends/helpers/caching_helper.rb

Overview

Helps with cache handling with Dalli.

Class Method Summary collapse

Class Method Details

.drop(dalli_instance, key) ⇒ Boolean

Drops `data` under `key` in the given caching instance.

Parameters:

  • dalli_instance (::Dalli::Client)

    caching instance

  • key (String)

    key value

Returns:

  • (Boolean)

    success or failure


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/backends/helpers/caching_helper.rb', line 43

def self.drop(dalli_instance, key)
  return if dalli_instance.blank? || key.blank?

  begin
    dalli_instance.delete(key)
    true
  rescue
    # ignore
    false
  end
end

.load(dalli_instance, key) ⇒ Object, NilClass

Loads `data` under `key` from the given caching instance.

Parameters:

  • dalli_instance (::Dalli::Client)

    caching instance

  • key (String)

    key value

Returns:

  • (Object, NilClass)

    data or nothing


28
29
30
31
32
33
34
35
36
# File 'lib/backends/helpers/caching_helper.rb', line 28

def self.load(dalli_instance, key)
  return if dalli_instance.blank? || key.blank?

  begin
    dalli_instance.get(key)
  rescue
    nil
  end
end

.save(dalli_instance, key, data) ⇒ Boolean

Saves `data` under `key` in the given caching instance.

Parameters:

  • dalli_instance (::Dalli::Client)

    caching instance

  • key (String)

    key value

  • data (Object)

    data to store

Returns:

  • (Boolean)

    success or failure


11
12
13
14
15
16
17
18
19
20
21
# File 'lib/backends/helpers/caching_helper.rb', line 11

def self.save(dalli_instance, key, data)
  return if dalli_instance.blank? || key.blank?

  begin
    dalli_instance.set(key, data)
    true
  rescue
    # ignore
    false
  end
end