The HMVC Design Pattern: Working with MySQL and Caching Data - Coding a cache class
(Page 4 of 4 )
In the previous segment, you learned how to give to this HMVC-based framework the ability to interact with MySQL via a simple abstraction class. Considering that connecting to a database and running queries will always introduce an overhead into any application being executed, it'd be desirable to implement a caching mechanism that permits you to skip (at least partially) these processes when possible.
For this particular project, that caching mechanism will be made up of a single class that will cache data only by using the file system. The definition of this class is shown below, so look at it please:
(Cache.php)
<?php
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;
}
// private constructor
private 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.');
Frankly speaking, the logic that stands behind the "Cache" class is very easy to grasp. It uses only a few straightforward methods for saving data to a specified cache directory and for getting this data back. There's also an extra method that simply determines whether or not a given cache is valid, based on a time expire strategy.
Finally, I decided to make the cache class a Singleton, but keep in mind that this process is entirely optional. Naturally, it should be discarded if you need to deal with multiple instances in accordance with the requirements of your own applications.
And with this last code sample, we've come to the end of this third tutorial of the series. At this point, this sample framework is not only capable of implementing a basic HMVC schema, but is capable of working with MySQL and caching data too, which makes it much more functional.
However, there are still a few requirements that must be met before I show you how to develop a PHP application composed of individual MVC modules. Thus, for the moment take a deep breath, be patient and read the final thoughts.
Final thoughts
In this third episode of the series, I turned the framework that implements the HMVC design pattern into a more functional stack of classes by adding two more to it: a basic MySQL driver and a simple cache class.
In the course of the next tutorial, I'm going to continue expanding the framework's current functionality by coding two more classes, which will come in handy for filtering user-supplied data and helping in the generation of HTML forms.
Now that you know what to expect from the upcoming part, you don't have any excuses to miss it!
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.