Module: Cacheable::ClassMethods

Defined in:
lib/steam/community/cacheable.rb

Instance Method Summary collapse

Instance Method Details

#cacheable_with_ids(*ids) ⇒ Object

Defines wich instance variables should be used to index the cached objects A call to this method is needed, if you want a class including this module to really use the cache.



27
28
29
# File 'lib/steam/community/cacheable.rb', line 27

def cacheable_with_ids(*ids)
  class_variable_set(:@@cache_ids, ids)
end

#cached?(id) ⇒ Boolean

Returns whether the requested object id is already cached

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/steam/community/cacheable.rb', line 32

def cached?(id)
  id.downcase! if id.is_a? String
  class_variable_get(:@@cache).key?(id)
end

#clear_cacheObject

Clears the object cache



38
39
40
# File 'lib/steam/community/cacheable.rb', line 38

def clear_cache
  class_variable_set :@@cache, {}
end

#new(id, fetch = true, bypass_cache = false) ⇒ Object

This checks the cache for an existing object. If it exists it is returned. Otherwise a new object is created. Overrides the default constructor.



45
46
47
48
49
50
51
52
53
# File 'lib/steam/community/cacheable.rb', line 45

def new(id, fetch = true, bypass_cache = false)
  if cached?(id) && !bypass_cache
    object = class_variable_get(:@@cache)[id]
    object.fetch if fetch && !object.fetched?
    object
  else
    super(id, fetch)
  end
end