Class: Elephas::Backends::RubyOnRails

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

Overview

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

Instance Method Summary (collapse)

Methods inherited 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.



37
38
39
40
41
# File 'lib/elephas/backends/ruby_on_rails.rb', line 37

def delete(key)
  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.



47
48
49
# File 'lib/elephas/backends/ruby_on_rails.rb', line 47

def exists?(key)
  Rails.cache.exist?(key) && Rails.cache.read(key).valid?(self)
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.



15
16
17
# File 'lib/elephas/backends/ruby_on_rails.rb', line 15

def read(key)
  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:

  • Cache.setup_options


26
27
28
29
30
31
# File 'lib/elephas/backends/ruby_on_rails.rb', line 26

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