Blogroll

Friday, November 4, 2011

Use cache class

in previous post, i introduced the technical to cache a objec to file
And in this post, i continue introduce to way to use it.
The fist : thinking the id for cache, this id must be unique, example I have class person and do the list method  in this class at page 2 in default module.

The best id is ="default_person_list_2"
so we have $cache_key ="default_person_list_2"

class person{
 public function list() {
        $cache_key ='default_person_list_2';
       include 'caches.php';
        $cache =new cache();
        if ($cache->isValid($cache_key))
        {
            $result = $cache->getCache($cache_key);
        }
        else
        {
             $person_db = new person_db();
            
              $result = $person_db ->getAll();
              $result =$result;
              $cache->save($cache_key, __CACHE_TIME, $result);
        }
        
     // do something continue.
    }
}
we include caches class and create new object caches.
the first: we check valid long life . if it is still valid then we get result from cache.
Otherwise, we get result from db throught $person_db (or some class else) and cache the result to file.
And if have second request, we will not get result from db again.

Olá! Se você ainda não assinou, assine nosso RSS feed e receba nossas atualizações por email, ou siga nos no Twitter.
Nome: Email:

0 comments:

Post a Comment