Using redis instead of database in heroku application

I have a simple application deployed on heroku. Mostly users come and enter search queries. I would like to add a small section on the side of the page that shows "recent searches." Although for this I do not want to add a database to my application. I read about redis, but can it be used for this? can i just click about the last 10 searches on redis hashes and get them?

+3
source share
1 answer

Yes, this is a good option for redis. Click the search terms in the Redis list and get the last ten when you want to display them. From time to time, you can remove all but the last ten items from the list to avoid too much value.

Add items to a list like this (assumed to be REDISa Redis join):

REDIS.rpush('searches', 'Xyz')

and get the last ten, like this:

REDIS.lrange('searches', -10, -1)

(as a side effect, Redis will first get the last element of the list, which is probably what you want). Redis lists the work as Ruby arrays; negative indices mean counting from the end.

To close the list of up to ten items, you can use this command:

REDIS.ltrim('searches', -10, -1)

, - , , , . lrange , 10 , .

+6

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


All Articles