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
  protect_from_forgery
  
  before_filter :set_default_cache_headers

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

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

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

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:

$('a[rel]')

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

$('a[rel="nofollow"]')
$('a[rel!="nofollow"]')

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

 

 

The Art of Electronics – International Edition

I’ver recently begun diving into Electronics and Circuitry and wanted to pick up a book to become more acquainted with the concepts.

The internet seems to be an agreement that “The Art of Electronics (2nd Edition)” by Paul Horowitz and Winfield Hill is the authoritative book on the subject, but a brand new hardcover copy from Amazon will set you back close to $100 USD. I found a lot of overseas booksellers on EBAY who claimed to have the same book for around $30. After reading the descriptions, it turned out they were actually selling the Internation Edition. What the heck, ordered.

I ordered the book in the middle of the day on Friday, and to my surprise had it on my doorstep Monday! I’ve included pictures to show the book build and print quality. I don’t have access to a US Edition of the book, but apparently it’s all the same content, just printed on less expensive material, and in paperback. Also, it’s freaking red. Overall, it’s fine, and exactly what I wanted.

The Art of Electronics 2nd Edition (International) cover

The Art of Electronics 2nd Edition (International)