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

hakre
  • 193,403
  • 52
  • 435
  • 836
Asim Zaidi
  • 27,016
  • 49
  • 132
  • 221
  • 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? – Matt Aug 03 '12 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:)? – Asim Zaidi Aug 03 '12 at 18:53
  • I think what you're looking for is no-cache headers. Here's the relevant [PHP documentation](http://php.net/manual/en/function.header.php). Just do a ctrl+f for "caching" – Matt Aug 03 '12 at 18:56
  • Or take a look at [memcache](http://php.net/manual/en/book.memcache.php). – Matt Aug 03 '12 at 18:57

1 Answers1

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?

Samuel
  • 16,923
  • 6
  • 62
  • 75
  • something like that..but I want to cache the new data only if data in mysql changed... – Asim Zaidi Aug 03 '12 at 18:54
  • 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 http://stackoverflow.com/questions/1467369/invoking-a-php-script-from-a-mysql-trigger. Then from your page you can just look at the Redis without touching the MySQL database at all. – Samuel Aug 03 '12 at 19:21