views:

63

answers:

1

Hello, do you have any ideas of how to store comments in memcache?

Let's imagine that I'm having image with a lot of comments for it. All comments are separated in pages. User can define number of comments per page in his profile.

How should I store comments in memecache?

Should I store all comment's IDs in one memcache key or should I separate it into some keys (depending on page number)? How should I store texts of comments in memcache?

What is the best practice? P.S. I'm expecting short comments (like Youtube have) ~ 100-150 bytes per comment approximately.

Thank you.

+1  A: 

Since the comments are relatively short, this depends a lot on the typical number of comments per image and what kind of resources you have for memcache. A simple but flexible approach would be to store comments in groups based on the smallest possible comment/page option. Then, for users who request more comments per page, you would just fetch more comments from memcache.

So if the smallest number per page is 10, store the first 10 comments with a key like 'comments:image_id:10', and the second set in 'comments:image_id:20', etc. Then if another user needs 50 per page, just pull together comments from 10, 20, 30, etc.

Depending on your options, you might actually choose a number smaller than the smallest option, such that it is always a factor of the available options, thereby eliminating the need to prune some from the end.

For instance, if you store 10 at a time, but the user needs 15, you have to fetch 20 and drop 5. Not too big a deal, but you could have stored them in groups of 5 and then only fetched 15. It just depends on what your options are.

Rob Van Dam