Memcached with Rails 3

Rails,Ruby — Dillon @ 6:13 pm

I never had a reason to play with memcached and it was on my list of things to learn. Below I will demonstrate an example app very quickly and simply. I’m not doing anything more complicated than simple primitive storing. If you are going to store any objects in memcached, dalli gem will take care of this for you (ymmv).

First let’s create a rails app and an rvm gemset to play in.

rvm use 1.9.2@memcache --create

If you don’t have rails in your global gemset, you can install rails now. I used the 3.1 RC here just to test out 3.1 and memcached at the same time.

gem install rails --pre
gem install dalli

Dalli is written by Mike Perham (awesome guy) and simply wires up Rails.cache to be our memcached server. This is convenient and more standard than creating your own global variable or other configuration.

Next, let’s install memcached if we haven’t already:

brew install memcached
memcached -v

You can also start memcached as a service under Mac using the homebrew instructions. I like to leave things in the foreground when I’m first setting them up or grok’ing.

Ok, now we can create our dummy app.

rails new memcache_test

Our dummy rails app is going to have a slow controller action in it. In this case, it could be a row count of a huge database. In your case it could be a slow network call or a result of an expensive SQL operation.

First, edit your Gemfile:

gem 'dalli'

Edit config/environments/development.rb

# Memcached
  config.perform_caching = true
  config.action_controller.perform_caching = true
  config.cache_store = :dalli_store, 'localhost:11211'

Rails c should work at this point and this line:

Rails.cache.read('foo')
=> nil

Now generate a controller:

rails g controller posts
class PostsController < ApplicationController
  def index
    @posts_count = Rails.cache.read 'posts_count'
    if @posts_count
    else
      # expensive operation
      sleep 3
      @posts_count = 321
      Rails.cache.write('posts_count', @posts_count, :expires_in => 3)
    end
  end
end

Create a route in routes.rb:

resources :posts

Create a simple view in app/views/posts/index.json.erb:

{ posts: <%= @posts_count %> }

Now when you hit the page it should be slow the first time but fast the second time. After 3 seconds, it’s slow again.

$ time wget -O - localhost:3000/posts.json
HTTP request sent, awaiting response... 200 OK
Length: 14 [application/json]
real	0m3.049s
user	0m0.001s
sys	0m0.003s
$ time wget -O - localhost:3000/posts.json
HTTP request sent, awaiting response... 200 OK
Length: 14 [application/json]
real	0m0.049s
user	0m0.001s
sys	0m0.003s

So you can see that memcached returned the cached object and avoided the sleep call. In the real world, this would equate to lower page rendering time or better application performance.

Pivot Table in Ruby

Ruby — Dillon @ 10:42 pm

Sometimes you need to transpose data from columns to rows or vice-versa. In the SQL world, this is called a pivot table. Usually this happens when you’re coming from a key-value store and want to turn it into something more structured but it can also happen in poorly designed or legacy databases. The idea is pretty simple, pivot the data from rows into columns. For example, here’s a listing of CDs:

-------------------------------------
DJ Shadow       |  electronica
DJ Shadow       |  1996
DJ Shadow       |  Endtroducing
The Avalanches  |  Since I Left You
The Avalanches  |  2000
The Avalanches  |  electronica
-------------------------------------

There are really only two CDs here but the data is presented to us as artist:attribute pairs. We want the data to look like this:

Artist         | Genre       | Album            | Year
------------------------------------------------------
DJ Shadow      | electronica | Entroducing      | 1996
The Avalanches | electronica | Since I Left You | 2000

(more…)

Simple Ruby-Processing Collision Detection

Processing,Ruby — Dillon @ 9:30 pm


In an old post I talked about doing collision detection using a 2d bitmap (or grid). That example was done using Processing (Java). It was a simple example but was very relevant to all the problems I had when building Tatris which did not use a bitmap for player position, instead it used real 2d space of x,y.

I didn’t talk about it in the previous post but I had added an Observer to the Java code that would highlight blocks in green when a collision was detected. Unfortunately, the code is a bit unclean although it works. I have a few global variables (common in Processing sketches) and some unused variables. So I never posted it.

Fast-forward to today and I have been playing around with ruby-processing (rp5) and porting some of my sketches. The code is not much smaller (300ish lines of Java, 265 lines of Ruby) but I didn’t optimize either code for line numbers. Neither code is really easier to read. This might be because I straight-up ported the code with only minor changes. Even if I were to do this from scratch, I’m not sure I could make it more ruby-like. Where I use arrays for x,y, I’m sure I could do something cooler like ruby Hashes, but a lot of the code is bound to JRuby and the Processing API so it ends up reading about the same.
(more…)

Dealing with Git Merges

Development — Dillon @ 10:26 pm


Git changed the way I code singularly because of how branching works. Hopefully you have at least tried branching out in Git. Even if you have, you might still be doing the old SVN ways of never merging. If you do merge, chances are you’ve had a conflict. I have had only really bad experience with conflicts when something is majorly wrong with my repo. For the most part, Git does everything automatically and is really smart about it.

But sometimes, Git doesn’t know what’s going on because too many changes have happened. No fault of Git but sometimes it has cost me some time when I’m either doing too many things at once or did something wrong. As far as actually defining what I do and in what order (workflow), it’s not really relevant because I usually work on one feature branch at one time. In a team environment (which I haven’t really done), I’d say this looks pretty good.

Regardless, we’re going to play around with conflicts and show a sample workflow with branching. First, go into some directory you don’t care about and create a project:

cd /tmp
mkdir test_conflict
cd test_conflict

Create some “code”.

echo -e "This code computes PI and is released." > code.txt
 
git init
Initialized empty Git repository in /tmp/test_conflict/.git/
git add code.txt
git commit -a -m "initial commit"
[master (root-commit) e7007ea] initial commit
 1 files changed, 4 insertions(+), 0 deletions(-)

(more…)

What do you use an Enumerator for in Ruby?

Ruby — Dillon @ 11:55 am

Sometimes when I’m fumbling around in irb on an API I don’t know, or with Active Record queries, I get an Enumerator object back when I don’t want to. So if I save this Enumerator as a variable, what the heck do I do with it? And why do I get an Enumerator anyway?

So what we’ll do is play around with a hash of elements. Elements have a name, color and what happens when you touch them attributes. We’ll iterate through them and print out the values as normal but we’ll also store the Enumerator by itself and iterate through it later.

# enumerator test
# ie: why the ---- do i get an enumerable back when I'm cluelessly hacking?
 
elements = [
  { :name => "earth",   :color => "brown",  :touch => "I'm dirty." },
  { :name => "wind",    :color => "white",  :touch => "I'm flying." },
  { :name => "fire",    :color => "red",    :touch => "I'm on fire!" },
  { :name => "water",   :color => "blue",   :touch => "I'm wet." },
  { :name => "ice",     :color => "blue",   :touch => "Brr!"}
]
 
# Here is the wtf moment.  What are we supposed to do with an Enumerator?!
puts "What do you use a Ruby enumerator for?"
puts elements.find
 
# Fix it you dummy, you didn't pass in a block.
puts "\nYou don't get an enumerator if you pass a block in:"
puts elements.find{|e| e[:name] == "wind"}
 
# ok so ruby devs aren't dumb, wtf is enumerable
enum = elements.each
 
color_enum = enum.collect { |e| e[:touch] }
puts "\nDelayed enum search: "
puts color_enum.collect { |c| c }.join(", ")
 
puts "\nStandard collect with no enumerator: "
puts elements.collect{|e| e[:color]}.join(",")
# => ["brown", "white", "red", "blue", "blue"]
 
map = elements.collect {|e| {e[:name].to_sym => e[:color]} }
# [{:earth=>"brown"}, {:wind=>"white"}, {:fire=>"red"}, {:water=>"blue"}, {:ice=>"blue"}]
# No!  This is an array of hashes.  Not what we want.
 
h = {}
elements.each {|e| k=e[:name]; v=e[:color]; h[k]=v}
# {"earth"=>"brown", "wind"=>"white", "fire"=>"red", "water"=>"blue", "ice"=>"blue"}
# YES!  One hash, one deep.  Perfect.
 
puts "\nNice hash associating element to color: "
puts h

(more…)

How to write a Ruby and Rails 3 REST API

Rails,Ruby — Dillon @ 11:18 pm

Background

I’ve always wondered how I’d go about publishing a real REST API on the web to do something. In this example, we’ll create an employee manager app-thing. It’s not particularly interesting but it shows what “API” means. In another bit, we’ll create an “API” meaning a library to interact with this web service.

The rails app

First, create a new rails app.
rails new rest_api

Edit config/database.yml:

  # MySQL
  #   gem install mysql2
  development:
    adapter: mysql2
    host: localhost
    database: rest_api
    username: rest_api
    password: rest_api
    pool: 5
    timeout: 5000

(more…)

Singleton and Observer Pattern in Ruby 1.9

Ruby — Dillon @ 10:29 pm


This post isn’t specific to Ruby 1.9, I just want to differentiate this a little bit from other examples out there.

First cut of singleton

A while ago, I wrote a singleton class as a proof of concept. It turns out that this was the hard way. Since then, I’ve just used the Singleton Mixin instead of writing it by hand. For this post, I’ll talk about both. First off, what we’re going to create is a God class which, other than starting a quite dangerous religious debate, will illustrate a singleton object and how to work within and outside it.

The first example is very contrived. We’ll create a God class and define a metaclass with class < < self which will keep us from creating a God instance with God.new and instead will force use to refer to only one God. So essentially, any properties within that block will be constrained to one single instance. Anything outside it, is a normal instance.

Like I said, this is pretty old code and so it doesn't flow all that well. First, we create the God class as a singleton because we'll just declare that all religions are about the same big guy (as an example). It has instance variables like name, religion and soul because God doesn't have one name. Everyone calls him something different. This is also true for religion. Soul as an instance variable of God actually doesn't make much sense. But it does allow us to track what "humans" there are. In the next example, this will be clearer.
(more...)

Perl vs Ruby

Development,Ruby — Dillon @ 12:18 pm


This isn’t like Perl vs Ruby: FIGHT! This is more like walking vs running: COMPARE!. They will both get you there, just in different times and sweat amounts. :)

I stopped doing Perl in about 2002 but not because I hated Perl. I stopped doing Perl because started learning Java. Java was where the jobs were. Since all that, I know I’ve become a better developer so a lot of this is situational to me and not a reflection on the language. Without a control group, you can’t say that the language has made me any different. I’ve learned things (not all things) from each language, environment and community as I’ve bounced around. No doubt, I’m Ruby-biased so please post comments, corrections and better ways of doing things in Perl so that I’m the most accurate I can be. Recently it’s popped back up at work and I needed a refresher but

Ok, enough about that. I’m here to compare how you do things in each language. Ruby is all about blocks, syntax sugar and a very different community than Perl. Both share many idealistic values (such as there’s more than one way to do something) and Ruby was inspired a lot by Perl. So this isn’t a battle but more of a Rosetta stone.
(more…)

Watchr filesystem events not firing

Mac,Ruby — Dillon @ 10:30 pm

I wanted to use a ruby environment inside a Ubuntu VM but still be able to edit text files in Textmate. So I shared out my project folder and fired up a watchr script I made. Unfortunately, it didn’t work at all.

I tried a few different libs, including rev, rb-inotify, rb-fsevents (which turned out to be mac only) but nothing was firing when I’d save a file. I thought maybe rev or watchr was broken in 1.9 but that was not the case. The problem is the vmware shared folders. When you do a write, modify or whatever, it doesn’t fire the same hooks as a local event does:

Modify File -> Textmate Save -> project/foo.txt (does not fire in watchr)
Modify File -> Vi Save in VM -> project/foo.txt (watchr fires)

And it wasn’t just watchr (as I said), every ruby library was seeing the same thing. So I gave up on the shared folders through VMware and just installed netatalk.

sudo aptitude install netatalk

I can edit files in Textmate this way and watchr works as expected. So why is this important? Because a tight REPL is important. And watchr / wtchr makes the tight loop happen. This is also probably going to happen if you use autotest in a project with lib/* test/* too.

Pretty specific but I hope it helps someone.

Arduino Cat Faucet with Mongodb and Rails

Arduino,Noteworthy,Rails,Ruby — Dillon @ 11:01 pm

I built a robot arm for my cat during a month-long geekcation. :) Here are some shots of the web interface. The graph shows the percentage of the day that she drinks.

Final hardware rig

Background

My cat likes to drink fresh and cold water directly from the faucet. We get up and turn on the faucet only to leave it running after she’s jumped down. It’s not really a big problem for us but I saw a fun problem that I could work on. As much as this seems like a weird and freakish oddity, it’s a potential start of a smarthome sensor network that may provide some utility. I also saw an opportunity to learn various things such as MongoDB, mechanical construction with Microrax, Rails3 and more development on Arduino with an Xbee module.

(more…)

« Previous PageNext Page »
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
(c) 2012 SQUARISM | powered by WordPress with Barecity