Class: Elephas::Providers::RubyOnRails

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/elephas/providers/ruby_on_rails.rb

Overview

This is a Ruby on Rails providers, which uses Rails.cache.

Instance Method Summary (collapse)

Methods included from Base

#now

Instance Method Details

- (Boolean) delete(key)

Deletes a value from the cache.

Parameters:

  • key (String)

    The key to delete.

Returns:

  • (Boolean)

    true if the key was in the cache, false otherwise.



39
40
41
42
43
44
# File 'lib/elephas/providers/ruby_on_rails.rb', line 39

def delete(key)
  key = key.ensure_string
  rv = Rails.cache.exist?(key)
  Rails.cache.delete(key)
  rv
end

- (Boolean) exists?(key)

Checks if a key exists in the cache.

Parameters:

  • key (String)

    The key to lookup.

Returns:

  • (Boolean)

    true if the key is in the cache, false otherwise.



50
51
52
53
# File 'lib/elephas/providers/ruby_on_rails.rb', line 50

def exists?(key)
  key = key.ensure_string
  Rails.cache.exist?(key) && Rails.cache.read(key).valid?
end

- (Entry|NilClass) read(key)

Reads a value from the cache.

Parameters:

  • key (String)

    The key to lookup.

Returns:

  • (Entry|NilClass)

    The read value or nil.



17
18
19
# File 'lib/elephas/providers/ruby_on_rails.rb', line 17

def read(key)
  self.exists?(key) ? Rails.cache.read(key) : nil
end

- (Object) write(key, value, options = {})

Writes a value to the cache.

Parameters:

  • key (String)

    The key to associate the value with.

  • value (Object)

    The value to write. Setting a value to nil **doesn't mean deleting the value.

  • options (Hash) (defaults to: {})

    A list of options for writing.

Returns:

  • (Object)

    The value itself.

See Also:



28
29
30
31
32
33
# File 'lib/elephas/providers/ruby_on_rails.rb', line 28

def write(key, value, options = {})
  entry = ::Elephas::Entry.ensure(value, key, options)
  entry.refresh
  Rails.cache.write(key, entry, expires_in: entry.ttl)
  entry
end