Recipe 13.3. Persisting Objects with Madeleine
Problem
You want to store objects in RAM and persist them between independent executions of the program. This will let your program recall its state indefinitely and access it very quickly.
Solution
Use the
Madeleine library available as the
madeleine gem. It transparently persists any Ruby object that can be serialized with Marshal. Unlike a conventional database
persistence layer, Madeleine keeps all of its objects in RAM at all times.
To use Madeleine, you have to decide which objects in your system need to be serialized, and which ones you might have saved to a database traditionally. Here's a simple Madeleine-backed program for conducting yes/no polls, in which agreement adds one to a total and disagreement subtracts one:
#!/usr/bin/ruby -w
# poll.rb
require 'rubygems'
require 'madeleine'
class Poll
attr_accessor :name
attr_reader :total
def initialize(name)
@name = name
@total = 0
end
def agree
@total += 1
end
def disagree
@total -= 1
end
end
So far there's been no Madeleine code, just a normal class with instance variables
and accessors. But how will we store the state of the poll between invocations of the polling program? Since instances of the Poll class can be serialized with Marshall, we can wrap a Poll object in a MadeleineSnapshot, and keep it in a file:
poll = SnapshotMadeleine.new('poll_data') do
Poll.new('Is Ruby great?')
end
The system accessor retrieves the object wrapped by MadeleineSnapshot:
if ARGV[0] == 'agree'
poll.system.agree
elsif ARGV[0] == 'disagree'
poll.system.disagree
end
puts "Name: #{poll.system.name}"
puts "Total: #{poll.system.total}"
You can save the current state of the object with take_snapshot:
poll.take_snapshot
Here are a few sample runs of the poll.rb program:
$ ruby poll.rb agree
Name: Is Ruby great?
Total: 1
$ ruby poll.rb agree
Name: Is Ruby great?
Total: 2
$ ruby poll.rb disagree
Name: Is Ruby great?
Total: 1
Discussion
Recall this piece of code:
poll = SnapshotMadeleine.new('poll_data') do
Poll.new('Is Ruby great?')
end
The first time that code is run, Madeleine creates a directory called poll_data. Then it runs the code block. The result of the code block is the object whose state will be tracked in the poll_data directory.
On subsequent runs, the poll_data directory already exists,
and Madeleine loads the current state of the Poll object from the latest snapshot in the directory. It doesn't run the code block.
Here are the contents of poll_data after we run the program three times:
$ ls poll_data
000000000000000000001.snapshot
000000000000000000002.snapshot
000000000000000000003.snapshot
Every time we call poll.take_snapshot, Madeleine serializes the Poll object to a snapshot file in poll_data. If the data ever gets corrupted, you can remove the corrupted snapshot files and revert to a previous version of the data.
A clever trick for programs like our poll application is to use Kernel#at_exit to automatically save the state of an object when the program ends. This way, even if your program is killed by a Unix signal, or throws an exception, your data will be saved.
at_exit { poll.take_snapshot }
In applications where a process runs indefinitely, you can save snapshots at regular intervals by spawning a separate thread:
def save_recurring_snapshots(madeleine_object, time_interval)
loop do
madeleine_object.take_snapshot
sleep time_interval
end
end
Thread.new { save_recurring_snapshots(poll, 24*60*60) }
See Also
 |