开发者

retrieving data from memcache

开发者 https://www.devze.com 2022-12-23 18:45 出处:网络
I am starting to learn the benefits of memcache, and would like to implement it on my project. I have understood most of it, su开发者_高级运维ch as how data can be retrieved by a key and so on.

I am starting to learn the benefits of memcache, and would like to implement it on my project. I have understood most of it, su开发者_高级运维ch as how data can be retrieved by a key and so on.

Now I get it that I can put a post with all of its details in memcache and call the key POST:123, that is OK, and I can do it for each post.

But how to deal with the case when I query the table posts to get the list of all posts with their titles. Can this be done with memcache, or should this always be queried from the table?


Memcache is a key-value cache, so as you've described, it is typically used when you know exactly what data you want to retrieve (ie., it is not used for querying and returning an unknown list of results based on some filters).

Generally, the goal is not to replace all database queries with memcache calls, especially if the optimization isn't necessary.

Assuming the data won't fit in a single value, and you did have a need for quick-access to all the data in your table, you could consider dumping it into keys based on some chunk-value, like a timestamp range.

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


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

In any case, memcached is not a database replacement, it's a key/value storage (fast and scalable at it). So you have to decide what data to store in DB and what offload to memcached.

Alternatively you could use "cachemoney" plugin which will do read/write through caching of your AcriveRecord in memory (even in memcached)


Look at the cache_money gem from Twitter. This gem adds caching at the ActiveRecord level. The find calls by id will go through the cache. You can add support for indexing by other fields in your table (i.e. title in your case)

class Post < ActiveRecord::Base
  index :title
end

Now the find calls that filter by title will go through cache.

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


Basically what you should do it check the cache first to see if it has the information you need. If there is nothing in the cache (or if the cached data is stale), then you should query the table and place the returned data in the cache, as well as return it to the front-end.

Does that help?


You cannot enumerate memcached contents - it's not designed to do that the way you try. You need to store the complete result set of SQL queries instead of single records.

So, yes: Do a SELECT title FROM posts and store the result in memcached (eg as POSTS:ALL_UNORDERED). This is how it is designed. And yes, if querying single records like SELECT * FROM posts WHERE id = XX then store that. Just keep in mind to create unique memcached keys for each query you perform.


As far as I know you can't query for multiple keys and values. But if you need to access the same list of posts often, why don't you store this in with a key like "posts" in your memcache.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号