info about link
: Javascript : PHP Index : MySQL :
database array caching class
: Source : : Explanation : : Example : : Todo : : Feedback :

Since it would be difficult do demonstrate a caching mechanism online, i have created a zip-file that contains a demonstration of the code.
Just unpack it at any given location, open the index.html file and then click on the 'setup'-link. This will guide you through the proces of configuring the settings in order to make the demonstration work. Once you've done that you can click on the 'test'-link for the demonstration.

However, to give you an idea what your code would look like once you start working with it, here are some examples:

Setting up the code

The caching mechanism needs to know what data to select from the database and how to store it.
So, of course you'll have to define a query somewhere, which will be responsible for retrieving the data you want to cache. This can be done by extending the abstract class DbArray_Query_Abstract . In your custom class you'll need only to implement 1 method, which is getQuery(), the return value of which should be the query. Let's have a look what that looks like:
<?php
require_once "Abstract.php";
class 
DbArray_Query_categories extends DbArray_Query_Abstract
{
    public function 
getQuery()
    {
        return 
"SELECT CategoryID, CategoryName FROM Categories ORDER BY CategoryID";
    }
}
?>

In this example we're selecting all the data from a database table Categories. This is all you need to do to create a cached array!

There are other public methods in the Abstract class though, which act as defaults, but which you can override to customize your data further.

getFirstOption() will allow you to add a value to the beginning of the array. This can be particularly usefull in select-boxes where you want to display something like 'please select'.

    
public function getFirstOption()
    {
        return 
"-- please select --";
    }
 
// or
    
public function getFirstOption()
    {
        return array(
=> "-- please select --");
    }

As you can see you can return either a String or an Array. When you return a String, an empty key is used.

With getIfNoResults() you can define what should be returned when the query didn't yield any results.

    
public function getIfNoResults()
    {
        return array(
=> "no results found");
    }

The return value has to be an array.

By default, the mechanism that creates the array will create sub-arrays when more than two columns are present in the rows that the database returns. When there's a maximum of 1 sub-array, we call it 1-deep; this will happen when there's 3 columns in the database results. With four columns the mechanism will go 2-deep, with five 3-deep, etc.
Of course, sometimes this is not what you want. For this purpose the method getDepth() can help you, which will restrict 'the depth'.

    
public function getDepth()
    {
        return 
1;
    }


Working with code

Now that we have set up some code that allows us to cache the data from the Categories table, let's see how we can interact with it.
There are only three functions that you are going to need:
get
<?php
$categoryArr 
DbArray::get("categories");
?>

This will assign the data to an array called $categoryArr.
It's also possible to use it directly though, in a foreach loop for example:
<?php
echo "<select>";
foreach (
DbArray::get("categories") AS $catId=> $catName)
{
    echo 
"<option value=\"".$catId."\">".$catName."</option>";
}
echo 
"</select>";
?>



It's even possible to directly access a sub variable: <?php
/* 1 */ DbArray::get("categories""1");
// or even (in case this would be a deeply nested array structure
/* 2 */ 
DbArray::get("categories""1""7""5""2");
?>

/* 1 */ will return the value of $categories[1]
/* 2 */ will return the value of $categories[1][7][5][2]
If they exists of course .. otherwise a an error will occur which is similar to the native php-error you get when trying to access a value that doesn't exist.
exists
<?php
if (isset($_GET['cid']))
{
    if (
DbArray::exists("categories"$_GET['cid']))
    {
        echo 
DbArray::get("categories"$_GET['cid']);
    }
    else
    {
        echo 
"this category doesn't exist<br>;"
    
}
}
?>


To test more deeply nested values, you can do the same trick as is demonstrated above wit get():
<?php
DbArray
::exists("categories""1""7""5""2");
?>

This will only return True if the value $categories[1][7][5][2] exists

cache
This one is pretty straightforward. By now you have already defined the query that is responsible for selecting the right data and you've come up with a key that you use for retrieving it through the DbArray class. Refreshing the cache is simply done by passing this key to the cache() method:

<?php
DbArray
::cache("categories");
?>


That's it!
So, whenever you do an INSERT, UPDATE or DELETE query that concerns any of your cached data, simply perform this operation to refresh the cached data.
(of course, the most elegant solution would be to use a database class in which you define the cache-keys in your table-classes so the insert(), update() en delete() methods can do this automatically for you)

=[Disclaimer]=     © 2005-2012 Excudo.net