0

I have an query like:

SELECT id as OfferId FROM offers
WHERE concat(partycode, connectioncode) = ? 
AND CURDATE() BETWEEN offer_start_date 
AND offer_end_date AND id IN ("121211, 123341,151512,5145626 ");

Now I want to cache the results of this query using memcache and so my question is

  1. How can I cache an query using memcache.
  2. I am currently using CURDATE() which cannot be used if we want to implement caching and so how can I get current date functionality without using CURDATE() function ?
4
  • Is this question clear or does it need some more modifications ? Commented Apr 2, 2010 at 18:11
  • 1
    Is this question basically: How do I use memcache? Commented Apr 2, 2010 at 18:13
  • For Memcache look here: stackoverflow.com/questions/2512571/… and stackoverflow.com/questions/2444011/memcache-vs-mysql-in-memory Commented Apr 2, 2010 at 18:34
  • I'm highly sceptical about many of the memcache questions here...people seem to think that memcache is a necessary addition for any [web] application, without any feel for what kinds of data can usefully be put in it. memcache is not a magic fix to make queries faster; with suitable indexes and cache in the database itself, simple queries like this should be fine without it. memcache adds complexity and potentially consistency problems: don't add it to a project until you really need to. Commented Apr 3, 2010 at 0:00

1 Answer 1

1

Something like this should work:

function getOffers($ids) {
    $key = implode(',', $ids);

    $cache = new Memcache();
    $cache->connect('localhost');
    $content = $cache->get($key);
    if ($content === false) {
        // content is not cached, so we have to run the query
        $content = $yourDb->query('your query here');
        $cache->add($key, $content);
    }

    $cache->close();

    return $content;
}

getOffers(array('121211','123341','151512','5145626'));

You can take this a step further by sorting $ids so that the same "set" of IDs (but in a different order) will still take advantage of an available cache for that set.

This isn't date-sensitive, but you can make it date sensitive by adding the date string to $key

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.