Asim Zaidi
Asim Zaidi

Reputation: 28314

caching for noob with mysql, php and redis

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

Upvotes: 0

Views: 652

Answers (1)

Samuel
Samuel

Reputation: 17171

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?

Upvotes: 1

Related Questions