Module: Cacheable

Included in:
Portal2Inventory, SteamGroup, SteamId, TF2BetaInventory, TF2Inventory
Defined in:
lib/steam/community/cacheable.rb

Overview

This module implements caching functionality to be used in any object class having one or more unique object identifier (i.e. ID) and using a ‘fetch` method to fetch data, e.g. using a HTTP download.

Author:

  • Sebastian Staudt

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fetch_timeTime (readonly)

Returns the time the object’s data has been fetched the last time

Returns:

  • (Time)

    The time the object has been updated the last time



90
91
92
# File 'lib/steam/community/cacheable.rb', line 90

def fetch_time
  @fetch_time
end

Class Method Details

.included(base) ⇒ Object

When this module is included in another class it is initialized to make use of caching

The original ‘new` method of the including class will be aliased with `create`, relaying all instantiations to the `new` method defined in ClassMethods. Additionally the class variable to save the attributes to cache (i.e. cache IDs) and the cache class variable itself are initialized.

Parameters:

  • base (Class)

    The class to extend with caching functionality

See Also:



23
24
25
26
27
28
29
30
31
# File 'lib/steam/community/cacheable.rb', line 23

def self.included(base)
  base.extend ClassMethods
  base.send :class_variable_set, :@@cache, {}
  base.send :class_variable_set, :@@cache_ids, []

  class << base
    alias_method :create, :new
  end
end

Instance Method Details

#cacheObject

Saves this object in the cache

This will use the ID attributes selected for caching



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/steam/community/cacheable.rb', line 106

def cache
  cache     = self.class.send :class_variable_get, :@@cache
  cache_ids = self.class.send :class_variable_get, :@@cache_ids

  cache_ids.each do |cache_id|
    cache_id_value = instance_variable_get('@' + cache_id.to_s)
    unless cache_id_value.nil? or cache.key?(cache_id_value)
      cache[cache_id_value] = self
    end
  end

  true
end

#fetchObject

Note:

This method should be overridden in cacheable object classes and should implement the logic to retrieve the object’s data. The overriding method should always call ‘super` to have the fetch time up-to-date.

Sets the time this object has been fetched the last time



126
127
128
# File 'lib/steam/community/cacheable.rb', line 126

def fetch
  @fetch_time = Time.now
end

#fetched?Boolean

Returns whether the data for this object has already been fetched

Returns:

  • (Boolean)

    ‘true` if this object’s data is available



133
134
135
# File 'lib/steam/community/cacheable.rb', line 133

def fetched?
  !@fetch_time.nil?
end

#initialize(fetch_now = true) ⇒ Object

Note:

The real constructor of cacheable classes is Cacheable::ClassMethods#new.

Creates a new object and fetches the associated data of the object if desired

Parameters:

  • fetch_now (Boolean) (defaults to: true)

    If ‘true`, the object’s #fetch method is called to retrieve its data



98
99
100
101
# File 'lib/steam/community/cacheable.rb', line 98

def initialize(fetch_now = true) #:notnew:
  fetch if fetch_now
  cache
end