Friendly  NoSQL with MySQL in Ruby

Store schema-less data in MySQL.
Evolve your data model without feeling the pain of migrations.
Build indexes offline
Building an index can take hours with a lot of data. Stay agile without taking your app offline.
Write-through and read-through caching baked right in.
Fast websites make more money. The 99.8% cache hit rate we see in production is a one-liner with Friendly.

Created by: James Golick and Jonathan Palardy for FetLife #NSFW.

Inspired by How FriendFeed uses MySQL to store schema-less data.

Get Friendly in 5 minutes

Install friendly

Install the gem.

sudo gem install friendly

Setup gem dependency in Rails

If you're on rails, add this in environment.rb:

config.gem "friendly"

and create a config/friendly.yml:

  	development:
  	  :adapter:  "mysql"
  	  :host:     "localhost"
  	  :user:     "root"
  	  :password: "swordfish"
  	  :database: "friendly_development"
        

not using Rails:

  	Friendly.configure :adapter  => "mysql",
  				       :host     => "localhost",
  				       :user     => "root",
  				       :password => "swordfish",
  				       :database => "playing_with_friendly"
        

Create a Model

Fire up your vim:

  	class BlogPost
  	  include Friendly::Document
	  
  	  attribute :author, String
  	  attribute :title,  String
  	  attribute :body,   String
  	end
        

Create the Table

From irb or script/console:

Friendly.create_tables!

Index

Just add the indexes you want to your model.

  	class BlogPost
  	  include Friendly::Document
	  
  	  attribute :author, String
  	  attribute :title,  String
  	  attribute :body,   String
	  
  	  indexes :author
  	  indexes :created_at
  	end      
        

Run create_tables again and the index tables will be created for you.

Friendly.create_tables!

Create Objects

With familiar ActiveRecord syntax:

  	BlogPost.create :author => "James Golick",
  				    :title  => "Friendly has familiar syntax.",
  				    :body   => "So, there's very little learning curve."
       

Query

All posts by James Golick:

BlogPost.all(:author => "James Golick")

Most recent posts:

BlogPost.all(:order! => :created_at.desc)

Cache

Install the memcached gem:

sudo gem install memcached

Configure Friendly to cache:

Friendly.cache = Friendly::Memcached.new(Memcached.new)

Configure your model to cache:

  	class BlogPost
  	  include Friendly::Document
	  
  	  attribute :author, String
  	  attribute :title,  String
  	  attribute :body,   String
	  
  	  indexes :author
  	  indexes :created_at
	  
  	  caches_by :id
  	end
        

And now your app is that much friendlier!