Retrieving data from memcache

I am starting to learn the benefits of memcache and want to implement it in my project. I understood most of them, for example, how data can be obtained with a key, etc.

Now I understand that I can post a message with all its details in memcache and call the POST:123 key POST:123 , this is normal, and I can do this for each message.

But how to deal with the case when I query the posts table to get a list of all posts with their headers. Is it possible to do this with memcache, or should it always be requested from a table?

+4
source share
6 answers

Memcache is a cache key, which, as you described, is usually used when you know exactly what data you want to receive (i.e., it is not used to query and return an unknown list of results based on some filters).

Typically, the goal is not to replace all database queries with memcache calls, especially if optimization is not required.

Assuming the data will not match a single value, and you need quick access to all the data in your table, you might consider dumping them to keys based on some chunk value, such as the timestamp range.

However, it is probably best to either save the database query or load it directly into memory (if we are talking about a single server that writes all updates).

+3
source

You can have a key called "posts" with the value "1,2,3,4,10,12", and then update, retrieve it every time a new message is created, updated, deleted.

In any case, memcached is not a replacement for the database, it is a repository of keys / values ​​(fast and scalable on it). Thus, you need to decide what data to store in the database and what to upload to memcached.

Alternatively, you can use the cachemoney plugin, which will read / write through caching your AcriveRecord in memory (even memcached)

+1
source

Take a look at cache_money from Twitter. This stone adds caching at the ActiveRecord level. Search for calls by id will go through the cache. You can add indexing support to other fields of your table (i.e. title in your case)

 class Post < ActiveRecord::Base index :title end 

Now the search says that the filter by the header will go through the cache.

 Post.all(:conditions => {:title => "xxxx"}) 
+1
source

Basically what you need to do is first check the cache to see if it needs information. If there is nothing in the cache (or if the cached data is out of date), you should query the table and put the returned data in the cache, and also return it to the interface.

Does it help?

0
source

You cannot list memcached content - it is not intended to do this as you try. You need to save the full set of SQL query results instead of single records.

So yes: do a SELECT title FROM posts and save the result in memcached (e.g. as POSTS:ALL_UNORDERED ). Here is how it is designed. And yes, if you are requesting individual posts, such as SELECT * FROM posts WHERE id = XX , save this. Just keep in mind creating unique memcached keys for every request you make.

0
source

As far as I know, you cannot request multiple keys and values. But if you need to access the same message lists often, why don't you save it with a key, for example "posts" in your memcache.

0
source

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


All Articles