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
42
# File 'lib/feed_tools_ram_cache.rb', line 39

def self.clear
  @by_id, @by_href = nil, nil
  initialize_cache    
end

.connected?Boolean

Called by FeedTools to determine if the cache is online.

Returns:

  • (Boolean)


83
84
85
# File 'lib/feed_tools_ram_cache.rb', line 83

def self.connected?
  @by_id != nil
end

.find_by_href(url) ⇒ Object

Required by FeedTools.



105
106
107
# File 'lib/feed_tools_ram_cache.rb', line 105

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

.find_by_id(id) ⇒ Object

Required by FeedTools.



100
101
102
# File 'lib/feed_tools_ram_cache.rb', line 100

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

.initialize_cacheObject

Called by FeedTools to initialize the cache.



75
76
77
78
79
80
# File 'lib/feed_tools_ram_cache.rb', line 75

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

.set_up_correctly?Boolean

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

Returns:

  • (Boolean)


89
90
91
# File 'lib/feed_tools_ram_cache.rb', line 89

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)


95
96
97
# File 'lib/feed_tools_ram_cache.rb', line 95

def self.table_exists?
  true
end

.write_item(item) ⇒ Object

Writes an item into the cache



60
61
62
63
64
65
66
# File 'lib/feed_tools_ram_cache.rb', line 60

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 ==.


112
113
114
115
# File 'lib/feed_tools_ram_cache.rb', line 112

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

#hashObject



117
118
119
# File 'lib/feed_tools_ram_cache.rb', line 117

def hash
  @fields.hash
end

#saveObject

Called by FeedTools to save the cache item.



69
70
71
72
# File 'lib/feed_tools_ram_cache.rb', line 69

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