开发者

MySQL: Query Cacheing (How do I use memcache?)

开发者 https://www.devze.com 2022-12-25 05:55 出处:网络
I have an query like: SELECT id as OfferId FROM offers WHERE concat(partycode, connectioncode) = ? AND CURDATE() BETWEEN offer_start_date

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 ("12开发者_如何学编程1211, 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 ?


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

0

精彩评论

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