DalliError: No server available – Using Rails, AWS EC2 and Elasticache

We deployed our Rails application today and found that one of our AWS EC2 servers wasn’t able to connect to our Elasticache instance using the Ruby dalli gem.

DalliError: No server available

Fortunately this isn’t a ruby/rails problem, it’s a permissions issue.

In order to allow an Amazon EC2 instance to access your cache cluster, you will need to grant the EC2 security group associated with the instance access to your cache security group.

Turns out the EC2 instance that wasn’t able to connect to Elasticache, was using a different security group, which we forgot to add to our elasticache instance. Once we did that, we were immediately able to get a connection.

Setting Default HTTP Cache Headers in Rails

We’re working on putting our current product behind Amazon Cloudfront. With Cloudfront, if you don’t set a Cache-Control header with an expiry time, Cloudfront will cache the content for 24 hours by default. Instead of littering all my controller actions with cache settings, I wanted to add a default value then be able to override on and action by action basis.

First, here’s how you actually set a cache-control header. This would go in a controller action.

expires_in 1.minute, :public => true

To add a default setting, add the expires_in call to a before_filter in your application controller.

class ApplicationController < ActionController::Base
  before_filter :set_default_cache_headers

  def set_default_cache_headers
    expires_in 1.minute, :public => true

Finally, override the cache header as needed in your controller actions.

class HomeController < ApplicationController
  def index
    # ... action logic
    expires_in 5.minute, :public => true

jQuery’s Advanced Attribute Selectors

TIL that jQuery has some pretty cool attribute querying capability.

I knew you could select element by attributes by simple adding them with brackets:


And that you can also match against content inside the attributes.


But this is where is gets cool. You’re not just limited to equal or not equal. You can also provide other operators for more advanced matching scenarios:

$('a[rel|="nofollow"]') // prefix search: "nofollow-"
$('a[rel*="nofollow"]') // contains search: "xxnofollowxx"
$('a[rel~="nofollow"]') // word search: "me nofollow bookmark"
$('a[rel$="nofollow"]') // starts with search: "nofollowxxx"
$('a[rel^="nofollow"]') // ends with search: "xxnofollow"

Check out the docs for more info: http://api.jquery.com/category/selectors/attribute-selectors/

Array-like objects in JavaScript

I learned something interesting in John Resig’s book Secrets of the JavaScript Ninja. How could you add Array like properties to your objects? At first thought, or at least my first thought, I would add an array to the object and then just use it internally as a collection, or maybe set the prototype to Array.prototype.

However, John points out a much simpler way. You can simply use  Array’s prototype methods on your object using call.

I decided to checkout the EcmaScript 5 standard (specifically Section 15.4.4 Properties of the Array Prototype Object) to find out more about these generic methods. Each property that could be used was said to be left “intentionally generic” and had a note similar to the following:

NOTE The concat function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method. Whether the concat function can be applied successfully to a host object is implementation-dependent.

Here’s the full list of Array properties that are said to be generic:

  • Array.prototype.toString
  • Array.prototype.toLocaleString
  • Array.prototype.concat
  • Array.prototype.join
  • Array.prototype.pop
  • Array.prototype.push
  • Array.prototype.reverse
  • Array.prototype.shift
  • Array.prototype.slice
  • Array.prototype.sort
  • Array.prototype.splice
  • Array.prototype.unshift
  • Array.prototype.indexOf
  • Array.prototype.lastIndexOf
  • Array.prototype.every
  • Array.prototype.some
  • Array.prototype.forEach
  • Array.prototype.map
  • Array.prototype.filter
  • Array.prototype.reduce
  • Array.prototype.reduceRight