Module: Cacheable::ClassMethods
- Defined in:
- lib/steam/community/cacheable.rb
Overview
This module implements functionality to access the cache of a class that includes the Cacheable module as class methods
Instance Method Summary collapse
-
#cacheable_with_ids(*ids) ⇒ Object
Defines wich instance variables which should be used to index the cached objects.
-
#cached?(id) ⇒ Boolean
Returns whether an object with the given ID is already cached.
-
#clear_cache ⇒ Object
Clears the object cache for the class this method is called on.
-
#new(id, fetch = true, bypass_cache = false) ⇒ Object
This checks the cache for an existing object.
Instance Method Details
#cacheable_with_ids(*ids) ⇒ Object
A call to this method is needed if you want a class including this module to really use the cache.
Defines wich instance variables which should be used to index the cached objects
46 47 48 |
# File 'lib/steam/community/cacheable.rb', line 46 def cacheable_with_ids(*ids) class_variable_set(:@@cache_ids, ids) end |
#cached?(id) ⇒ Boolean
Returns whether an object with the given ID is already cached
55 56 57 58 |
# File 'lib/steam/community/cacheable.rb', line 55 def cached?(id) id.downcase! if id.is_a? String class_variable_get(:@@cache).key?(id) end |
#clear_cache ⇒ Object
Clears the object cache for the class this method is called on
61 62 63 |
# File 'lib/steam/community/cacheable.rb', line 61 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 ‘new` method of the cacheable object class.
75 76 77 78 79 80 81 82 83 |
# File 'lib/steam/community/cacheable.rb', line 75 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 |