PHP Classes

File: example-cache.php

Recommend this page to a friend!
  Classes of Kirill Shvakov   Simple Cache   example-cache.php   Download  
File: example-cache.php
Role: Example script
Content type: text/plain
Description: Examples
Class: Simple Cache
Access cached data in different containers
Author: By
Last change: v 0.3
Date: 12 years ago
Size: 4,067 bytes
 

Contents

Class file image Download
<?php
$startTime
= mktime(true);
require_once
'lib/SimpleCache.php';
$testCache = array(
   
'time' => time(),
   
'data' => 'Data. SimpleCache'
);
/*
 Strategy
try {
   SimpleCache :: setup(SimpleCache :: MEMCACHED, $mConfig);
} catch (Exception $E) {
   SimpleCache :: setup(SimpleCache :: FILE, $fConfig);
}
$cacheEngine = SimpleCache :: getEngine();
*/
/*
try {
   
    SimpleCache :: setup(SimpleCache :: FILE, $fConfig);
    SimpleCache :: setup(SimpleCache :: MEMCACHED, $mConfig);
    SimpleCache :: setup(SimpleCache :: XCACHE, $xConfig);
   
} catch (Exception $E) {
    echo '<span style="color:red">' . $E->getMessage() . '</span><br/>' . "\n";
}*/
$hash = 's<dds$%^frswfjkfbDS';

try {
   
SimpleCache :: setup(SimpleCache :: FILE, array('cacheDir' => dirname(__FILE__) . '/cache/', 'hash' => $hash));
   
$cacheEngine = SimpleCache :: getEngine(SimpleCache :: FILE);
   
$cache = $cacheEngine->get('testKey');
    if (
$cache) {
       
$fileCacheData = $cache;
    } else {
       
$fileCacheData = array_merge($testCache, array('engine' => 'File'));
       
$cacheEngine->set('testKey', $fileCacheData, array(), 10);
    }
   
//$cacheEngine->delete('testKey');
   
echo 'File cache <br/>';
   
print_r($fileCacheData);
    echo
'<hr/>';
   
} catch (
Exception $E) {
    echo
'<hr/><span style="color:red">' . $E->getMessage() . '</span><hr/>';
}


try {
   
SimpleCache :: setup(SimpleCache :: MEMCACHED, array(
           
'hash' => $hash,
           
'compressed' => true,
           
'servers' => array(array(
                   
'host' => 'localhost',
                   
'port' => 11211,
                   
'weight' => 1,
                   
'persistent' => true
               
)
            )
        )
    );
   
$cacheEngine = SimpleCache :: getEngine(SimpleCache :: MEMCACHED);
   
$cache = $cacheEngine->get('testKey');
    if (
$cache) {
       
$memcachedCacheData = $cache;
    } else {
       
$memcachedCacheData = array_merge($testCache, array('engine' => 'Memcached'));
       
$cacheEngine->set('testKey', $memcachedCacheData, array(), 10);
    }
   
//$cacheEngine->delete('testKey');
   
echo 'Memcached cache <br/>';
   
print_r($memcachedCacheData);
   
} catch (
Exception $E) {
    echo
'<hr/><span style="color:red">' . $E->getMessage() . '</span><hr/>';
}
try {
   
SimpleCache :: setup(SimpleCache :: LIBMEMCACHED, array(
       
'hash' => $hash,
       
'compressed' => true,
       
'servers' => array(array(
                   
'host' => 'localhost',
                   
'port' => 11211,
                   
'weight' => 1
               
)
            )
        )
    );
   
$cacheEngine = SimpleCache :: getEngine(SimpleCache :: LIBMEMCACHED);
   
$cache = $cacheEngine->get('testKey');
    if (
$cache) {
       
$memcachedCacheData = $cache;
    } else {
       
$memcachedCacheData = array_merge($testCache, array('engine' => 'Libmemcached'));
       
$cacheEngine->set('testKey', $memcachedCacheData, array(), 10);
    }
   
//$cacheEngine->delete('testKey');
   
echo 'Libmemcached cache <br/>';
   
print_r($memcachedCacheData);
   
} catch (
Exception $E) {
    echo
'<hr/><span style="color:red">' . $E->getMessage() . '</span><hr/>';
}
try {
   
SimpleCache :: setup(SimpleCache :: XCACHE, array('hash' => $hash));
   
$cacheEngine = SimpleCache :: getEngine(SimpleCache :: XCACHE);
   
$cache = $cacheEngine->get('testKey');
    if (
$cache) {
       
$xcacheCacheData = $cache;
    } else {
       
$xcacheCacheData = array_merge($testCache, array('engine' => 'Memcached'));
       
$cacheEngine->set('testKey', $xcacheCacheData, array(), 10);
    }
   
//$cacheEngine->delete('testKey');
   
echo 'Xcache cache <br/>';
   
print_r($xcacheCacheData);
   
} catch (
Exception $E) {
    echo
'<hr/><span style="color:red">' . $E->getMessage() . '</span><hr/>';
}

/*
 $allCacheEnginies = SimpleCache :: getEnginies();
 var_dump($allCacheEnginies->FILE->get('testKey'));
*/

echo $startTime - mktime(true);