A Developer with a Pencil

Me and Redis Are Now Friends

During one of our latest project over @ ShinobiDevs we needed some way to represent some kind of a friendship status between the app’s user. The feature spec’d was more like Facebook’s request mechanism:

  • User can send friend requests to other users.
  • Other User needs to accept the friend request.
  • Current friends, pending requests and sent invitations are all visible tol the current user.

Pretty simple.

After a few days of hard work and lonely nights implementing this feature in the client’s app - we decided to release this functionality as a rails gem, so here is Friendis.

Friendis (a lame combination of Friends and Redis) is based on the Implement a social graph example from the Redis Cookbook and relies on the amazing abilities of Redis to manage the friends lists, requests and pending invitations.


Add this line to your application’s Gemfile:

gem 'friendis'

And then execute:

$ bundle

Or install it yourself as:

$ gem install friendis



create an initializer in config/initializers/friendis.rb:

Friendis.configure do |c|

The following options exist:

  • redis_connection: an existing Redis connection, defaults to a Redis.new instance.

Adding to a Model

All you need to do is to include the Friendable module:

include Friendis::Friendable

and to choose which attributes or methods will be cached in Redis for that user:

friend_this track: [:name, :picture]

Those fields will be changed in Redis after everytime you save the instance, note that your ORM needs to implement after_save and after_destroy since Friendis utilizes those callbacks to update and remove the cached data from Redis.

The id attribute will automatically be cached.


class User < ActiveRecord::Base
  include Friendis::Friendable

  friend_this track: [:name, :picture]

Friend Requests

@user1 = User.create(name: "Elad Meidar", picture: "http://picturez.com/elad.jpg")
@user2 = User.create(name: "Miki Bergin", picture: "http://picturez.com/miki.jpg")


Pending Friend Requests


pending_friend_requests will return the cached attributes for the pending friend requests, in this case

[{"name" => "Elad Meidar", "picture" => "http://picturez.com/elad.jpg", "id" => 1}]

Sent Friend Requests


sent_friend_requests will return the cached attributes for the sent friend requests, in this case

[{"name" => "Miki Bergin", "picture" => "http://picturez.com/miki.jpg", "id" => 2}]

Approving Friend Requests


Lisiting Friends


friends will return the cached attributes of the currently approved friends, in this case

[{"name" => "Miki Bergin", "picture" => "http://picturez.com/miki.jpg", "id" => 2}] 

Check Friendship

@user1.is_friends_with?(@user2) #=> true
@user1.is_friends_with?(@user3) #=> false



Contributing and Source Code

As always, Pull requests and forks are always welcome over @ Github