I’ve been talking about using in-memory objects for the testing and development of my Sinatra app. When I say in-memory object I’m referring to all of the data behind my app as the objects: users, tasks, relationships, events… and in-memory simply means I’m storing it as a short-term object in RAM with no persistence beyond the session or thread I start up for testing.

In order to help me think through my implementation (which I’ll be working on today) here’s a quick run-through of the why and how behind it.

Why In-Memory Objects?

Two words: speed and flexibility. It is much quicker to have an object with an address in memory (the output that looks like “User:0x007fcda38cccd8”) than an object that is buried inside a database and needs some sort of interface (such as ActiveRecord or DataMapper) to hunt it down. It is only a matter of fractions of a second per object, but when you are TDD'ing your way through a thoroughly tested system all of those fractions add up to put a damper on your productivity and adds significant pauses to your flow.

Flexibility comes from the fact that if you’ve built a thorough and clean interface to your in-memory data stores then you can move that interface over to whatever database will best serves your needs. Plus you can defer the decision of what database to use until the moment you really need to make that decisions- providing more time for you and your customer to learn how the data will be used. And the kicker? You can selectively move different parts of the data to different stores, such as using PostgeSQL for some of the data and a NoSQL database for other parts.

How In-Memory Objects?

I’m sure there are several ways that you can implement a store of in-memory objects, but I’m going to stick with a simple hash. When I fire up my app I’m going to construct a general Datastore hash that has keys corresponding to :users, :tasks, etc. Then the value of each of those keys will be the collection of objects inside of it, and each one of those object’s keys will be its id. Here’s a quick example:

datastore =  {
:users => {
1 => {
:name => "Mike",
:email =>"mike@test.com"
},
2 => {
:name => "John",
:email => "john@test.com"
}
},
:tasks => {
1 => {
:description => "First task"
},
2 => {
:description => "Second task"
}
}
}

It looks simple enough, but I know that creating all of the actions I need (find_by_id, find_by_name, delete, save….) are going to take some time. I better get started.