0

I have this method below that gives me a count of products within a category. I want to cache the count in my redis server. I am able to do that, but I am not sure how to bust the cache and my concept around that is not clear. Any help or ideas would be appreciated.

public static function products(){
             $prods = $this>getProducts();
             $Count = count($prods);
        if($Count){
            // save the count to redis
          $redis->saveCount($count);

        }                       

}

When do I hit the sql database ($prods = $this>getProducts();) to get count, and when do I just get it from redis? Also, how would I know when to do it and when to bust the old records in redis?

Thanks

4
  • What do you mean by "bust the cache"? Is there some sort of gangsta cache roaming around out there? Or do you just want the page to not cache? Commented Aug 3, 2012 at 18:26
  • i think I was gonna go with gangsta option...but now thinking about it, lets say I want php logic to hit mysql...oops "hit" is that a mafia lang too:)? Commented Aug 3, 2012 at 18:53
  • I think what you're looking for is no-cache headers. Here's the relevant PHP documentation. Just do a ctrl+f for "caching" Commented Aug 3, 2012 at 18:56
  • Or take a look at memcache. Commented Aug 3, 2012 at 18:57

1 Answer 1

1

When you insert the count in the redis also insert the time it was inserted. Then, when the page loads, fetch the count and the time from the redis server, if the time is greater than X minutes or hours old, fetch the actual count from the product database and repeat. Is that what you're looking for?

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

2 Comments

something like that..but I want to cache the new data only if data in mysql changed...
That doesn't sound much like a cache to me. If I were implementing it, I would use a trigger on the MySql side to update the Redis. If you have command line access for PHP you could run the script above from the MySQL trigger using the method described in this question stackoverflow.com/questions/1467369/…. Then from your page you can just look at the Redis without touching the MySQL database at all.

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.