Class: FeedTools::RamFeedCache

Inherits:
Object
  • Object
show all
Defined in:
lib/feed_tools_ram_cache.rb

Overview

RAM-backed cache for FeedTools.

This is a drop-in replacement for FeedTools::DatabaseFeedCache that does not require a database.

Constant Summary collapse

FIELDS =

Fields required by FeedTools.

The FeedTools documentation is outdated. The correct fields can be inferred from the migration found at:

http://feedtools.rubyforge.org/svn/trunk/db/migration.rb
[:id, :href, :title, :link, :feed_data, :feed_data_type,
:http_headers, :last_retrieved, :time_to_live, :serialized]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_values = {}) ⇒ RamFeedCache

Creates a new cache item.



20
21
22
# File 'lib/feed_tools_ram_cache.rb', line 20

def initialize(initial_values = {})
  @fields = initial_values.dup
end

Instance Attribute Details

#fieldsObject (readonly)

The fields in this cache item.



25
26
27
# File 'lib/feed_tools_ram_cache.rb', line 25

def fields
  @fields
end

Class Method Details

.clearObject

Removes all the cache items.



39
40
41
# File 'lib/feed_tools_ram_cache.rb', line 39

def self.clear
  # NOTE: Actual implementation supplied in offline_mode=.
end

.connected?Boolean

Called by FeedTools to determine if the cache is online.

Returns:

  • (Boolean)


132
133
134
# File 'lib/feed_tools_ram_cache.rb', line 132

def self.connected?
  @by_id != nil
end

.find_by_href(url) ⇒ Object

Required by FeedTools.



154
155
156
# File 'lib/feed_tools_ram_cache.rb', line 154

def self.find_by_href(url)
  @by_href[url]
end

.find_by_id(id) ⇒ Object

Required by FeedTools.



149
150
151
# File 'lib/feed_tools_ram_cache.rb', line 149

def self.find_by_id(id)
  @by_id[id]
end

.initialize_cacheObject

Called by FeedTools to initialize the cache.



124
125
126
127
128
129
# File 'lib/feed_tools_ram_cache.rb', line 124

def self.initialize_cache
  # NOTE: the FeedTools documentation says this will be called once. In fact,
  #       the method is called over and over again.
  @by_id ||= {}
  @by_href ||= {}
end

.lengthObject

The number of entries in the cache.



44
45
46
# File 'lib/feed_tools_ram_cache.rb', line 44

def self.length
  @by_id.length
end

.offline_mode=(offline) ⇒ Object

Enters or exits offline testing mode.

In offline testing mode, cache clearing requests are ignored, and items pretend that they are fresh. Warning: this means items won’t pretend like dumb ActiveRecord objects, and may fail unit tests that treat them as such.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/feed_tools_ram_cache.rb', line 88

def self.offline_mode=(offline)
  if offline  # Pretend items are live to stay offline.
    undef :last_retrieved, :time_to_live
    def last_retrieved
      Time.now
    end
    def time_to_live
      86400  # A day since last_retrieved was read, should avoid re-fetching.
    end      
    
    class <<self
      undef :clear
      def clear
        initialize_cache
      end
    end
  else  # Going online is OK, don't lie about the cache state.
    undef :last_retrieved, :time_to_live
    def last_retrieved
      @fields[:last_retrieved]
    end
    def time_to_live
      @fields[:time_to_live]
    end      
    class <<self
      undef :clear
      def clear
        @by_id, @by_href = nil, nil
        initialize_cache
      end
    end
  end
end

.set_up_correctly?Boolean

FeedTools documentation doesn’t specify this method, but the implementation calls it.

Returns:

  • (Boolean)


138
139
140
# File 'lib/feed_tools_ram_cache.rb', line 138

def self.set_up_correctly?
  connected?
end

.stateObject

The cache state, in a format that can be serialized.



28
29
30
# File 'lib/feed_tools_ram_cache.rb', line 28

def self.state
  @by_id.values.map { |value| value.fields }
end

.state=(new_state) ⇒ Object

Loads previously saved state into the cache.



33
34
35
36
# File 'lib/feed_tools_ram_cache.rb', line 33

def self.state=(new_state)
  clear
  new_state.each { |fields| write_item RamFeedCache.new(fields) }
end

.table_exists?Boolean

FeedTools documentation doesn’t specify this method, but the implementation calls it.

Returns:

  • (Boolean)


144
145
146
# File 'lib/feed_tools_ram_cache.rb', line 144

def self.table_exists?
  true
end

.write_item(item) ⇒ Object

Writes an item into the cache



64
65
66
67
68
69
70
# File 'lib/feed_tools_ram_cache.rb', line 64

def self.write_item(item)
  # NOTE: FeedTools seems to rely on ActiveRecord's auto-incrementing IDs.
  item.id = @by_id.length + 1 unless item.id

  @by_id[item.id] = item
  @by_href[item.href] = item
end

Instance Method Details

#==(other) ⇒ Object

Good idea: override equality comparison so it works for equal cache entries.

Must also override hash, since we're overriding ==.


161
162
163
164
# File 'lib/feed_tools_ram_cache.rb', line 161

def ==(other)
  return false unless other.kind_of?(RamFeedCache)
  @fields == other.fields
end

#hashObject



166
167
168
# File 'lib/feed_tools_ram_cache.rb', line 166

def hash
  @fields.hash
end

#new_record?Boolean

Called by FeedTools.

Returns:

  • (Boolean)


79
80
81
# File 'lib/feed_tools_ram_cache.rb', line 79

def new_record?
  @fields[:id].nil? ? true : false
end

#saveObject

Called by FeedTools to save the cache item.



73
74
75
76
# File 'lib/feed_tools_ram_cache.rb', line 73

def save
  self.class.write_item(self)
  true
end