Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't understand how redis works with rails

I'm new on redis and i find that learning redis basics is very easy, but when i try to understand how i can use it with rails, it become flow and i can't find any good tutoriel which explains steps from scratch , for example i find code like this :

class User < ActiveRecord::Base
   # follow a user
   def follow!(user)
     $redis.multi do
       $redis.sadd(self.redis_key(:following), user.id)
       $redis.sadd(user.redis_key(:followers), self.id)
     end
   end

  # unfollow a user
  def unfollow!(user)
    $redis.multi do
      $redis.srem(self.redis_key(:following), user.id)
      $redis.srem(user.redis_key(:followers), self.id)
    end
  end

but with this example, no other example show how to use follow method, what is the object i need pass to this method (is this object from a relational database ? or what ) etc ...

all examples i find in my search are incomplete, and this rend redis not easy at all when we decide to use it with rails !

I also find that the use of redis is at the model that inherits from ActiveRecord, I can not understand: if redis is used in the majority of cases with a relational database or alone, and what is the way most used, and how exactly ?

i know that my question is vast but what i search is how use redis and rails together, also if you have a good resource for me, i will very appreciate that. thank you

like image 466
medBouzid Avatar asked Aug 25 '13 15:08

medBouzid


1 Answers

no other example show how to use follow method, what is the object i need pass to this method (is this object from a relational database ? or what ) etc ...

the users are ActiveRecord objects -- your users of your system.

friend = User.find(params[:friend_id])
current_user.follow! friend

Usually, Redis is used as a secondary datastore. The regular user data, customer data, etc, is stored in Postgres or MySQL, and data that is redis-specific is stored in redis.

In RailsCasts 399 on autocomplete, Ryan shows how to use Redis to compliment the book store as a search engine only. He uses $redis.zrevrange and $redis.zincrby

like image 143
Jesse Wolgamott Avatar answered Oct 13 '22 13:10

Jesse Wolgamott