Module: EveBadger::Cache

Defined in:
lib/eve_badger/cache.rb

Overview

A wrapper around a Moneta object that provides automatic request caching for Evebadger::EveAPI while enabled.

Class Method Summary collapse

Class Method Details

.disableObject

disable request caching



22
23
24
# File 'lib/eve_badger/cache.rb', line 22

def self.disable
  @cache = nil
end

.enable(*args, **kwargs) ⇒ Object

Enable the cache with a specified Moneta adapter. See Moneta API documentation for possible configurations: www.rubydoc.info/gems/moneta/frames



14
15
16
17
18
19
# File 'lib/eve_badger/cache.rb', line 14

def self.enable(*args, **kwargs)
  unless @native_expires.any? { |name| args.include?(name) }
    kwargs.merge!({expires: true})
  end
  @cache = Moneta.new(*args, **kwargs)
end

.enabled?Boolean

test whether request caching is enabled

Returns:

  • (Boolean)


27
28
29
# File 'lib/eve_badger/cache.rb', line 27

def self.enabled?
  @cache ? true : false
end

.get(key) ⇒ Object

retrieve a value from the cache it if is enabled



46
47
48
49
50
51
52
# File 'lib/eve_badger/cache.rb', line 46

def self.get(key)
  if @cache
    @cache[key]
  else
    raise "Cannot get when cache is disabled."
  end
end

.store(key, value, options = {}) ⇒ Object

store a value in the cache if it is enabled



37
38
39
40
41
42
43
# File 'lib/eve_badger/cache.rb', line 37

def self.store(key, value, options={})
  if @cache
    @cache.store(key, value, options)
  else
    raise "Cannot store when cache is disabled."
  end
end

.typeObject

return the class type of the enabled cache, caches adapters which don’t natively support expiration will all appear as Moneta::Expires



32
33
34
# File 'lib/eve_badger/cache.rb', line 32

def self.type
  @cache.class
end