Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5968853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:08:54+00:00 2026-05-22T20:08:54+00:00

Right now we are storing some query results on Memcache. After investigating a bit

  • 0

Right now we are storing some query results on Memcache. After investigating a bit more, I’ve seen that many people save each individual item in Memcache. The benefit of doing this is that they can get these items from Memcache on any other request.

Store an array

$key = 'page.items.20';
if( !( $results = $memcache->get($key) ) )
{
    $results = $con->execute('SELECT * FROM table LEFT JOIN .... LIMIT 0,20')->fetchAll();
    $memcache->save($results, $key, 3600);
}
...

PROS:

  • Easier

CONS:

  • If I change an individual item, I have to delete all caches (it can be a pain)
  • I can have duplicated results (the same item on different queries)

vs

Store each item

$key = 'page.items.20';
if( !( $results_ids = $memcache->get($key) ) )
{
    $results = $con->execute('SELECT * FROM table LEFT JOIN .... LIMIT 0,20')->fetchAll();

    $results_ids = array();
    foreach ( $results as $result )
    {
        $results_ids[] = $result['id'];
        // if doesn't exist, save individual item
        $memcache->add($result, 'item'.$result['id'], 3600);
    }

    // save results_ids 
    $memcache->save($results_ids, $key, 3600);
}
else
{
    $results = $memcache->multi_get($results_ids);
    // get elements which are not cached
    ...
}
... 

PROS:

  • I don’t have the same item stored twice on Memcache
  • Easier to invalidate results on several queries (just the item we change)

CONS:

  • More complicated business logic.

What do you think? Any other PROS or CONS on each way?

Some links

  • Post explaining the second method in Memcached list
  • Thread in Memcached Group
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T20:08:54+00:00Added an answer on May 22, 2026 at 8:08 pm

    Grab stats and try to calculate a hit ratio or possible improvement if you cache the full query vs doing individual item grabs in MC. Profiling this kind of code is also very helpful to actually see how your theory applies.

    It depends on what the query does. If you have a set of users and then want to grab the “top 10 music affinity” with some of those friends, it is worth to have both cachings:
    – each friend (in fact, each user of the site)
    – the top 10 query for each user (space is cheaper than CPU time)

    But in general it is worth to store in MC all individual entities that are going to be used frequently (either in the same code execution, or in subsequent requests or by other users). Then things like CPU or resource heavy queries and data processings either MC-them or delegate them to async. jobs instead of making them realtime (e.g. Top 10 site users doesn’t needs to be realtime, can be updated hourly or daily).
    And of course taking into account that if you store and MC individual entities, you have to remove all referential integrity from the DB to be able to reuse them either individually or in groups.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now, I keep all of my projects on my laptop. I'm thinking that
I'm working developing a system right now that deals with lots of conversions between
Currently I'm trying to create some coding short cuts for our website. Right now
Some Google search results have special "navigation" items right underneath their descriptions. Sometimes it
I have a page with php query results in a table. Now I want
I'm a newbie in android, working on list adapters right now. I know some
Right now I convert the string containing the decimal number to an integer (ignoring
Right now I have double numba = 5212.6312 String.Format({0:C}, Convert.ToInt32(numba) ) This will give
Right now, I'm currently serializing a class like this: class Session { String setting1;
Take the following string as an example: The quick brown fox Right now the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.