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.



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

def delete(key)
  fkey = key.ensure_string
  rv = Rails.cache.exist?(fkey)
  Rails.cache.delete(fkey)
  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.



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

def exists?(key)
  fkey = key.ensure_string
  Rails.cache.exist?(fkey) && Rails.cache.read(fkey).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
34
# File 'lib/elephas/providers/ruby_on_rails.rb', line 28

def write(key, value, options = {})
  fvalue = ::Elephas::Entry.ensure(value, key, options)
  fvalue.refresh

  Rails.cache.write(key, value, :expires_in => fvalue.ttl)
  value
end