Can't figure out how redis works with rails.

I am new to redis , and I found that learning the basics of redis is very simple, but when I try to understand how I can use it with rails, it becomes a stream, and I cannot find any good teacher who explains the steps from scratch, for example , I find the code as follows:

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 in this example, no other example shows how to use the follow method, what is an object , I need to go to this method (is this object from a relational database? or what), etc ...

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

I also found that using redis in a model that inherits from ActiveRecord, I cannot understand: if redis is used in most cases with a relational database or alone, and what is most often used, and how exactly?

I know that my question is extensive, but I am looking for how to use redis and rails together, also, if you have a good resource for me, I will greatly appreciate it. thanks

+6
source share
1 answer

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

users are ActiveRecord objects — your users on your system.

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

Typically, Redis is used as a secondary data store. Normal user data, customer data, etc. They are stored in Postgres or MySQL, and data specific to redis is stored in redis.

In RailsCasts 399 at autocomplete, Ryan shows how to use Redis to compliment a bookstore only as a search engine. It uses $redis.zrevrange and $redis.zincrby

+8
source

Source: https://habr.com/ru/post/952459/


All Articles