Blog

Create a cache key from method argument values

In my project ProductWidgets I'm using caching heavily and I happen to be in love with Redis and use it for everything I can. I also really like the concept of key-based cache expiration that 37signals recently wrote about.

Now, in a couple of situations I had to add a lot of variables to the cache key to make sure it caches the right thing. Have a look at this example:

def search(category, query, locale, page, per_page)
  cache_key = [
    'searches',
    category,
    query,
    locale,
    page,
    per_page
  ].join(':')

  Rails.cache.fetch cache_key do
    # Heavy lifting
  end
end

It occurred to me that in situations like this, where I basically want to cache the whole method based on the arguments, there must be some automated way of doing this where you don't have to add each argument to the cache key. Imagine some other developer comes along and adds an argument to the method and forgets to add it to the cache key! I do not want to be involved in that bug hunt...

Read on

How to make everything background-processable through Sidekiq

I recently switched from Resque to Sidekiq for background processing in a project I'm working on. Sidekiq has a very slick feel to it and is basically a supercharged version of Resque, using threads instead of processed for its workers.

One thing that has irritated me when using Resque was that one was supposed to create separate worker classes for each job that should be processed in the background. While that makes sense for "real" jobs, e.g. syncing the Mailchimp database with your own or exporting some data into a XML file and mailing it out, often I just wanted to make an existing (class or instance) method processable in the background, e.g. user.perform_async :set_timezone_from_ip, request.remote_ip or Account.perform_async :disable_overdue.

Sidekiq offers the delay and delay_for methods to make class and instance method calls asynchronous, as detailed in their Wiki. It also says the following on that Wiki page, though:

I strongly recommend avoid delaying methods on instances. This stores object state in Redis and which can get out of date, causing stale data problems."

Read on

The road ahead for ShowSpace

I've been working on ShowSpace for more than 1.5 years now. It started off as an idea for a small web application but I quickly realized it had a lot of potential and started working on it more and more and calling it a "startup". It certainly wasn't always easy because I chose to do all development and marketing myself, but I learned a lot along the way!

Over its lifespan, ShowSpace has undergone lots of minor and a few major changes: for example, it was first focused on storefronts instead of widgets. A storefront was basically a store without a checkout, where all the products were affiliate products that linked to the merchants where you could actually buy them (some storefronts are still online). I also experimented with different pricing models, initially focusing on monthly payments before switching to "pay-as-you-go" product packages.

All in all it was an incredible learning experience and I have grown as a developer as well as an entrepreneur. I can certainly much more appreciate the amount of work that goes into launching a product, finding the first few users, and adapting the product over and over as you grow.

Read on

How to install and secure MySQL 5.5 from source with Sprinkle

I recently fell in love with Sprinkle as a way to provision servers for Ruby on Rails applications. I had heard about Chef and Puppet a long time ago and I knew that at some point I would have to start using one of them if I didn't want to do the same repetitive tasks over and over again.

The thing is that I actually enjoy working with a new and fresh server! It's that new car smell and you know you get to set up the latest and greatest everything! Still, after doing it multiple times in the last months, I realized the time had come and I set aside half a day to get down and dirty with Chef.

I picked Chef because I had seen that Ryan Bates of Railscast fame had done an episode on it and I normally trust him and have never been disappointed in his sense of picking the best tool for a job.

Read on

How to upload your Gmvault backups to S3

Gmvault is a great tool by Guillaume Aubert that makes it easy to create a backup of your Gmail emails on your local disk. It is cross-platform (Windows, Mac OS and Linux), supports incremental backups and handles errors that occur during the backup gracefully.

I have been looking for a tool to take automatic backups of my Gmail account for a while and Gmvault fits the bill. What I also wanted was a way to store the backed up files safely on Amazon S3 so I could recover them even if my local disk failed. This functionality is not included in Gmvault but it was quite straight-forward to code.

Read on

How to compile custom Sass stylesheets dynamically during runtime

Update January 15 2016: As someone pointed out in a comment on the gist, this method doesn't seem to work anymore with recent versions of Sprockets.

Update May 29 2013: The name of the stylesheet files was changed slightly (hyphen replaced by underscore) after feedback in the comments.

In any Rails 3.1 (or newer) app where the user can change the style (e.g. layout, colors, dimensions) of certain items you come to the point where you wish you could use all of the sweetness of the Rails asset pipeline to generate custom stylesheets dynamically during runtime.

After a bit of googling I realized what it comes down to is imitating the process Rails performs when precompiling all assets on deploy, only that we want to want to compile Sass code that was generated dynamically, not read from a static file in app/assets/javascripts. After having a look at the assets:precompile Rake task which does the heavy lifting it's clear that Sprockets::StaticCompiler is the main suspect in this case.

Read on