Archive

Archive for December, 2009

Clean example of using APC caching

December 3rd, 2009 No comments

A nice clean example of using APC.

class bar{
    private $ttl = 300;  // number of seconds time to live.

    public function foo($val) {

        $key = sprintf('%s-%s-%s', __CLASS__, __FUNCTION__, $val);
        $result = apc_fetch($key);

        if (!$result) {
            // do logic here;
            $result = 'something';

            apc_store($result, $key, $this->ttl);
        }

        return $result;
    }
}

The inclusion of the class, and function name, is to help organize your cache. Thus giving you the ability to clear only part of your cache.

Hope this helps…

Categories: PHP Tags: