Nov 2 2010

Speeding up your application with Cache_Lite

Cache_Lite is one of the fast, light and reliable cache system for PHP. It’s an extremely easy and small learning curve system to work with. This post will try to make you a light introduction to PHP Cache_Lite.

Installing PEAR Cache_Lite

In order to work with Cache_Lite you need to install the PEAR package.

pear install Cache_Lite-1.7.8

Starting with Cache_Lite

Is easy as instantiating the object to start using Cache_Lite.

<?php
  // Requiring the package
  require_once('Cache/Lite.php');
 
  $options = array(
    'cacheDir' => 'cache/', // This need to be writable
    'lifeTime' => 3600, // Value in seconds
    'automaticSerialization' => true // If you will serialize plain text strings, just define this as false, will give you a small performance increase
);
 
// Create a Cache_Lite object
$Cache_Lite = new Cache_Lite($options);

Using Cache_Lite

Now that we have the cache object instantiated, we can start using it.

<?php
  $cache_key = 'somekey';
  if( $data = $Cache_Lite->get($cache_key) ) {
    // Do something with cache data
   echo "CACHE DETECTED<br/>";
   var_dump($data);
  } else {
   echo "NOT DETECTED CACHE<br/>";
   $data = 'Some data to add into cache';
 
   $Cache_Lite->save($data,$cache_key); 
}

Just save the above code as test_cache.php and access the file, you can expect the following behavior. On the 1st request the string “NOT DETECTED CACHE” will be echo’ed and if you press refresh on the browser you should see “CACHE DETECTED” with the var_dump of the cache value on the browser.

For a more in deep documentation of Cache_Lite just go into the Cache_Lite package documentation over at PEAR.

Simpler than this you can’t get.

PHP Cache_Lite is a fast, simple and reliable cache system, usually used on non distributed applications, you can use a redundant and distributed cache solution, if you need a more complex approach to cache, like Memcached (a distributed memory cache system) or Redis ( an advanced key-value store ).

What is your experience with PHP and Cache systems ?

4 comments on “Speeding up your application with Cache_Lite

  1. Precisely what you reported manufactured lots of feel. However, take into consideration this specific, what happens if everyone extra a bit written content? After all, I dont need to show you how you can go your website, although imagine if anyone added in anything for you to probably receive parents awareness? Just like a online video media or even some sort of photograph as well as a pair of to obtain people today looking forward to just what youve reached say. I think, it could generate the web log turn on somewhat.

  2. Pingback: Programowanie w PHP » Blog Archive » Jose de Silva’s Blog: Speeding up your application with Cache_Lite

  3. Pingback: Programowanie w PHP » Blog Archive » Jose de Silva’s Blog: Speeding up your application with Cache_Lite

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>