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)


93
94
95
# File 'lib/feed_tools_ram_cache.rb', line 93

def self.connected?
  @by_id != nil
end

.find_by_href(url) ⇒ Object

Required by FeedTools.



115
116
117
# File 'lib/feed_tools_ram_cache.rb', line 115

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

.find_by_id(id) ⇒ Object

Required by FeedTools.



110
111
112
# File 'lib/feed_tools_ram_cache.rb', line 110

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

.initialize_cacheObject

Called by FeedTools to initialize the cache.



85
86
87
88
89
90
# File 'lib/feed_tools_ram_cache.rb', line 85

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.



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

def self.length
  @by_id.length
end

.set_up_correctly?Boolean

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

Returns:

  • (Boolean)


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

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)


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

def self.table_exists?
  true
end

.write_item(item) ⇒ Object

Writes an item into the cache



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

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


122
123
124
125
# File 'lib/feed_tools_ram_cache.rb', line 122

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

#hashObject



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

def hash
  @fields.hash
end

#new_record?Boolean

Called by FeedTools.

Returns:

  • (Boolean)


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

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

#saveObject

Called by FeedTools to save the cache item.



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

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