RAM-backed cache for FeedTools

FeedTools is a Ruby gem and Rails plug-in than parses both RSS and Atom feeds reliably. The library was intended to be used inside Rails, and comes with the entire kitchen sink, including a caching mechanism based on ActiveRecord and a database table.

This gem implements a RAM-backed cache, so FeedTools clients can get the benefits of caching without taking a dependency on ActiveRecord. The RAM-backed cache’s contents can be serialized using YAML.

Setup

rubygems install feedtools_ram_cache 

Usage

The gem does not interfere with the FeedTools API. To enable RAM-based caching, require the gem and configure FeedTools to use it:

# Configure FeedTools to use the RAM-backed cache.
require 'feed_tools_ram_cache'
FeedTools.configurations[:feed_cache] = FeedTools::RamFeedCache

In Rails, the code above can be placed in an initializer.

Serializing the Cache State

The cache state is a Ruby Hash available via the class attribute state. It can be stored in a YAML file, or loaded from a file. This can come in handy for tests.

# Save the cache state to state.yml
File.open('state.yml', 'w') { |f| YAML.dump FeedTools::RamFeedCache.state, f }
# Load the cache state from state.yml
FeedTools::RamFeedCache.state = YAML.load File.read('state.yml')

Clearing the Cache

The RAM-backed cache does not implement a size limitation. However, the cache can be cleared. This prevents it from growing indefinitely, and also provides a clean slate for testing.

FeedTools::RamFeedCache.clear

Offline Testing Mode

One-line summary: use the following in your unit tests.

FeedTools::RamFeedCache.offline_mode = true

The RAM-backed cache can be used for testing as follows. Test data is generated by clearing the cache, opening the feed to be tested, and saving the cache’s state (perhaps by serializing to a YAML file). Testing code sets the cache state to the saved state, and opens the feed again.

This strategy has the following issues: the code to be tested may clear the cache (my code does), and FeedTools will not use the cached data to see if it’s stale. While the cache is in offline mode, it ignores clearing requests, and it pretends its items are fresh.

Naming Convention

The naming scheme for this gem is confusing, but it is consistent with the naming of the FeedTools gem that it extends.

The gem is named feedtools_ram_cache, but the name of the library to be required in Ruby is feed_tools_ram_cache. This is consistent with FeedTools, whose gem name is +feedtools, providing the library feed_tools.

Requirements

The gem depends on the feedtools version used to test the code. If you need the RAM-based cache for an older version of feedtools, try to run the tests (via rake test) against your version of feedtools. If the tests pass, send me a pull request.

Contributions

I wrote the RAM-backed cache to fulfill my very specific needs. I will not be adding features, but I will do my best to fix bugs. If you want a feature, please don’t hesitate to fork the project and send me pull requests.