Miscellaneous Code

  Home arrow Miscellaneous Code arrow Page 4 - The HMVC Design Pattern: Working with ...
MISCELLANEOUS CODE

The HMVC Design Pattern: Working with MySQL and Caching Data
By: Alejandros Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2010-03-03

    Table of Contents:
  • The HMVC Design Pattern: Working with MySQL and Caching Data
  • Review: the current source files for the HMVC-driven framework
  • The default storage mechanism for the framework
  • Coding a cache class

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    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.');

           }

           return unserialize($data);

       }

      

       // check if the cache file is valid or not

       public function valid($id)

       {

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

            $file = array_shift($file);

           return (bool)(time() - filemtime($file) <= $this->_expire);

       }

    }// End Cache class

    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.
    blog comments powered by Disqus

    MISCELLANEOUS CODE ARTICLES

    - Creating a Web Page Controller with the HMVC...
    - Coding Controllers and Views for the HMVC De...
    - A Sample Web Application with the HMVC Desig...
    - Adding a Class to Parse Views to an HMVC Des...
    - Building a Model Class for the HMVC Design P...
    - Filtering Input Data and Generating HTML For...
    - The HMVC Design Pattern: Working with MySQL ...
    - Dispatching Requests to MVC Triads with the ...
    - Implementing the Hierarchical Model-View-Con...
    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap