FeedTools database cache Feb 16

The ruby gem FeedTools has a built-in caching mechanism. Unfortunately, it doesn't quite work out of the box, and the documentation doesn't explain it very well. Despite what it says, you have to tell it to explicitly use the cache in your code:

#!/usr/bin/env ruby

require 'rubygems'
require 'feed_tools'

FeedTools.configurations[:feed_cache] = "DatabaseFeedCache"

puts "Getting feed 1st time..."
f = FeedTools::Feed.open('http://www.slashdot.org/index.rss')
puts "1. live? #{f.live?}"
puts "Getting feed 2nd time..."
f = FeedTools::Feed.open('http://www.slashdot.org/index.rss')
puts "2. live? #{f.live?}"

Assuming FeedTools can find a database.yml file and the database has the cached_feeds table described in the FeedTools documentation, the above script will cache the feed after the first time and f.live? will return false the second time you run it.

I also added an index on the href column in cached_feeds. All the lookups are done on that column and the migration that comes with FeedTools doesn't have any indices. Here's my migration for cached_feeds:

class AddFeedToolsTables < ActiveRecord::Migration
  def self.up
    create_table :cached_feeds do |t|
      t.column :href, :string
      t.column :title, :string
      t.column :link, :string
      t.column :feed_data, :text
      t.column :feed_data_type, :string
      t.column :http_headers, :text
      t.column :last_retrieved, :datetime
      t.column :time_to_live, :integer
      t.column :serialized, :text
    end
    add_index :cached_feeds, :href
  end

  def self.down
    drop_table :cached_feeds
  end
end

2 comments

2 comments

Bob Aman Feb 18 2008

Yeah documentation kinda sucks. Yes, by design, the caching mechanism is disabled by default. This allows you to futz around in irb with FeedTools without changing any configuration settings.

Patrick Crosby Feb 19 2008

Bob: thanks for explaining why the cache isn't on by default. There are a few blog posts out there describing how to use the database cache that say it works as long as the database is set up properly. I assume they are referring to an older version of FeedTools. Hopefully this post will help with any confusion. Anyway, FeedTools is great! Thanks for writing and maintaining it.

Add a comment

Name (required)
Email (won't be displayed)
URL (include http://)
Comment (required)