title: PHP PEAR Cache_Lite
body: <p>HI.</p>
  
  <p>Since I m using shared hosting package, and im not able to use PECL Memcache, i would appreciate some tips about my own doubt between using my own little caching system or using PEAR Cache_Lite system.</p>
  
  <p>So here are my functions:</p>
  
  <pre><code>&lt;?php
  
  //this one create a simple .txt file named by unique query_key string generated width e.g $file_name=md5("SELECT * FROM table"); content of that file is serialized value of mysql return
  
  function write($query_key,$result)
  {
    global $config;
    $new_file_name=md5($query_key);
    $result_to_write=serialize($result);  
    if($handle=opendir($config['cache_dir'])) 
    {
      $f=fopen($config['cache_dir'].$new_file_name.'.txt','w+');
      fwrite($f,$result_to_write);        
      fclose($f);
      closedir($handle);
    }
  }
  
  // this one reads content of file (serialized mysql return of unique query_key) if timeout hes not expired, and if it is it return false
  
  function read($query_key,$timeout)
  {
    global $config;
    $file_name=md5($query_key);
    if($handle=opendir($config['cache_dir'])) 
    {
      if(file_exists($config['cache_dir'].$file_name.'.txt'))
      {
        $last_accessed=filemtime($config['cache_dir'].$file_name.'.txt');
        $now=time();
        if(($now-$last_accessed)&lt;$timeout)
        {
          $file=fopen($config['cache_dir'].$file_name.'.txt','rb');
          $f=fread($file,filesize($config['cache_dir'].$file_name.'.txt'));
          $array=unserialize($f);
          fclose($file);  
          closedir($handle);
        }else{ return FALSE; }
      }else{ return FALSE; }
    }
    if(!empty($array)) return $array;
    else return FALSE;
  }
  
  //and finally this one which is used when executing query, so it has timeout in seconds, if file (cached result) exists, and timeout has not expired, it returnt cached data , or it reads from database returns new result,and cache new result by writing new file
  
  function cache($query)
  {
    $timeout=600;
    $file_exists=read($query,$timeout);
    if($file_exists)
    {
      return $file_exists;
    }else{
      $result=get_rows($query);
      write($query,$result);
      return $result;
    }
  }
  ?&gt;
  </code></pre>
  
  <p>This my little "caching system" works very good, and as i can see, PEAR Cache_Lite works almost same way, and as i'm not familiar with pear cache system, i would like to know what is better, safer and faster solution to use between those two, and why??</p>
  
  <p>Thanx a Lot!!</p>