Home arrow PHP arrow Page 3 - Developing a Caching Class for an MVC-based Framework in PHP 5

Building a cache class - PHP

In this seventh part of the series, I add a cache class to the classes that comprise the MVC-based framework we're building. The new class uses the file system as the underlying backend for caching data, but it’s also possible to create one that caches data on shared memory, a SQLite database and so forth. Indeed, numerous possibilities exist.

TABLE OF CONTENTS:
  1. Developing a Caching Class for an MVC-based Framework in PHP 5
  2. Review: the previous HTML helper class
  3. Building a cache class
  4. Determining cache validity
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
June 22, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Any decent PHP framework must have some kind of caching mechanism to reduce the overhead caused when accessing a database server or serving static pages, and the one that I’m building here is no exception. For this particular project, the caching system will be a regular class that will use the file system for caching data.

Having explained that, here’s the partial definition of the cache class:

class Cache

{

                private $cachedir = 'cache/';

                private $expire = 60;

                private static $instance = NULL;

               

                // get Singleton instance of Cache class

                public static function getInstance($cachedir = '')

                {

                                if (self::$instance === NULL)

                                {

                                                self::$instance = new self($cachedir);

                                }

                                return self::$instance;

                }

               

                // constructor

                public function __construct($cachedir = '')

                {

                                if ($cachedir !== '')

                                {

                                                if (!is_dir($cachedir) or !is_writable($cachedir))

                                                {

                                                                throw new Exception('Cache directory must be a valid writeable directory.');     

                                                }

                                                $this->cachedir = $cachedir;

                                }

                }

               

                // write data to cache file given an ID

                public function set($id, $data)

                {

                                $file = $this->cachedir . $id;

                                if (file_exists($file))

                                {

                                                unlink($file);

                                }

                                // write data to cache

                                if (!file_put_contents($file, serialize($data)))

                                {

                                                throw new Exception('Error writing data to cache file.');

                                }

                }

               

                // read data from cache file given an ID

                public function get($id)

                {

                                $file = glob($this->cachedir . $id);

                                $file = array_shift($file);

                                if (!$data = file_get_contents($file))

                                {

                                                throw new Exception('Error reading data from cache file.');

                                }

                                return unserialize($data);

                }

}

As you can see above, the “Cache” class defines a couple of methods that can be used for writing data to the file-based cache and for reading from it. This two-way process is performed by the “set()” and “get()” methods respectively, which will throw a default exception if an error occurs when accessing the pertinent cache file.

Also, you should notice that the caching class implements a “getInstance()” method, which also returns a Singleton instance of the class. In this way it can be accessed through a single point across the whole framework.

At this point, while the previous cache class does a fair job of caching data, it lacks a mandatory feature: it doesn’t implement a method that permits it to determine the validity of a specified cache file.

To address that issue, in the section to come I’m going to code that method, thus completing the cache class.

To learn how this additional method will be defined, read the next few lines.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: