Moved away from storing individual string values to storing whole objects/documents in redis hashes. Hashes make more sense when you need to store a group of data values like a blog post.
But I’m not making a blog. My task it to create an updated version of my personal journal and information storage system. Daily I use it to log my thoughts, to stage blog posts (like this one), keep track of links to check later (which often doesn’t happen), keep todo lists for projects, and other things. But the old version relied too heavily on subfolder-type organization. The new version will be tag based, which will hopefully make it easier for me to keep things from getting buried in my hierarchy. Since the new design is tag-based, the data structures involved are simpler, opening the door for me to give redis a try.
Picking up where my previous blog post about node+redis left off …
I’ve now switched to fictorial’s node-redis-client. It supports MULTI, which allows you to do something similar transactions, like in traditional relational databases. Rolling back on failure is a nice side-effect, but it wasn’t as big a necessity as being able to trigger a callback when a group of asynchronous events had finished. MULTI takes care of this, so now I can save each field associated with my item (title, body, tags, and add creation date to a sorted set) and push a status update when they’ve all completed. I could have used a simple counter system that counts down from the number of operations being performed and triggers a callback when it reaches 0, but that doesn’t seem parallel-safe.
One thing to note about fictorial’s redis client, is that HGETALL returns an object to you, where normally it would return an array. I didn’t expect this, but eventually stumbled upon it after browsing the client source code. However, the feature seems incomplete because it doesn’t turn the object values into strings, which yields undesirable results when you run the object through JSON stringify.
Gradually I’m building up functions and abstractions to help with redis queries. The first I wrote is hashObjects(), which pulls the specified ids from a redis hash and returns an array of objects. This function is used with the redis call that gets the 10 most recent item ids from my “date created” sorted set.
I’m rather enjoying this new type of data modeling where you have to choose the best data-structure for the job.