Home arrow PHP arrow Creating an Extensible Caching System in PHP

Creating an Extensible Caching System in PHP

In this part of the series, I demonstrate that the use of the “Plug-in” pattern in the construction of an extensible caching system is actually much simpler than many people might think. [Editor's note: this article was published out of sequence; it's actually the third in the series, and comes before yesterday's "HTML5 Client-Side Cache in PHP."]

TABLE OF CONTENTS:
  1. Creating an Extensible Caching System in PHP
  2. Building an APC wrapper
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
February 03, 2011

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

If you’re looking for an approach that lets you build highly-scalable applications without having to dive into complex programming concepts, take a peek at the “Plug-in” pattern. This simple, yet powerful programming methodology will permit you to develop truly “pluggable” programs, whose functionality can be easily extended by means of loosely-coupled modules.

What’s more, implementing the model imposed by the “Plug-in” pattern is so simple that it only requires an average background in the foundation and key concepts of object-oriented programming, such as Inheritance, Composition and interfaces. That's all that you’ll need to know to get the pattern up and running.

Trying to sell you on the benefits of “Plug-in” is a rather pointless task if I don’t show you some functional code samples that demonstrate how to use this paradigm in some concrete cases. In line with this idea, in a previous installment of this series I developed a basic PHP application. It was responsible for rendering different elements on the browser, such as divs, paragraphs and even some JavaScript alert boxes.

Beyond its simplicity, the real beauty of this example relied on the ability of the application to display all sorts of “renderable” objects on screen without having to amend a single section of its source code. Now do you see how easy it is to build extensible PHP programs with the “Plug-in” pattern? I bet you do.

It’s fair to point out, however, that the “Plug-in” approach can be used in situations that resemble more realistic environments. In keeping with this concept, in this third chapter of the series I’m going to start building a simple caching system, which will be able to swap easily between several “pluggable” cache back ends.

Does my proposal sound engaging enough for you? Then jump in and begin reading! 

Start building the sample cache system: defining a caching interface

Even though the idea may sound strange at first, it’s possible to take advantage of the functionality provided by the “Plug-in” pattern and create an caching system capable of swapping different cache back ends at runtime, pretty similar to what many frameworks will let you do nowadays (i.e. Zend Framework, Symfony, CodeIgniter and so forth).   

So, the first step I’m going to take toward the implementation of this “pluggable” caching system will consist of defining an abstract cache backend. This backend, not surprisingly, will be an interface called “Cacheable,” whose definition is shown below:

(Cache/Cacheable.php)

<?php

namespace Cache;

interface Cacheable
{
 public function set($key, $data);
 public function get($key);
 public function delete($key);
 public function clear();     
}

As you can see from the code fragment above, the “Cacheable” interface establishes a contract that must naturally be fulfilled by all of its eventual implementers. The contract is made up of the “set(),” “get(),” “delete()” and “clear()” methods, which define the behavior of a generic cache backend (actually, I could have added a few more, but to keep things clear and understandable, I’m happy to have only the existing ones).

So far, so good. Having already defined an abstract cache backend, the next logical step is to build a concrete one. In this case, this concrete cache will be a simple wrapper for the APC PHP extension, and in the forthcoming section I’ll be showing its corresponding source class.

So, if you want to see how this APC wrapper will be implemented, jump ahead and keep reading.



 
 
>>> 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: