PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Kacper Rowinski   Cache Factory   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: examples of usage
Class: Cache Factory
Put and get data from different cache containers
Author: By
Last change:
Date: 12 years ago
Size: 2,276 bytes
 

Contents

Class file image Download
<?php
/*
 * This is a simple example how to user cacheFacatory class
 *
 * Main goal was to create a simple and easy switch class for popular caches
 * I have used
 * 'memcached' => http://www.php.net/manual/en/memcached.construct.php
 * 'redis' => https://github.com/nicolasff/phpredis
 * 'xcache' => http://xcache.lighttpd.net/wiki/XcacheApi
 * 'static' => using static varibles for one run testing (useful if you want to install cache and keep your app running :))
 */

// best to use autoloader, for test we include all >=]
include('cacheFactory.php');
include(
'cacheFactory/interfaceCache.php');
include(
'cacheFactory/cacheStatic.php');
include(
'cacheFactory/cacheMemcached.php');
include(
'cacheFactory/cacheXcache.php');
include(
'cacheFactory/cacheRedis.php');

// configuration for memcached -> we need IP and PORT (default)
//define('CACHE_FACTORY_IP_SERVER', '127.0.0.1');
//define('CACHE_FACTORY_IP_PORT', 11211);

// configuration for redis (default)
define('CACHE_FACTORY_IP_SERVER', '127.0.0.1');
define('CACHE_FACTORY_IP_PORT', 6379);

// if we use serveral application we need unique prefix for every key to avoid duplication
define('CACHE_FACTORY_UNIQUE_ID', 'unique_key');

// choose you handler we got 'memcached', 'redis', 'xcache', 'static'
// we can run only one cache (imho, no idea why you woudl use multiple caches)
define('CACHE_FACTORY_HANDLER', 'redis');

// get instance of class (we genereate ony one connection run!)
$cacheFactory = cacheFactory::getInstance();

// some tests like add data array

// create new key and data to cache for 3700s
$test_results = $cacheFactory->insert('array_test', range(1, 100), 3700);
var_dump($test_results);

// get data from cache
$test_results = $cacheFactory->get('array_test');
var_dump($test_results);

// remove cache by key
$test_results = $cacheFactory->delete('array_test');
var_dump($test_results);

// example of cache usage like for db object..
$key_name = 'obj_test';
if (
false == $our_data_arary = $cacheFactory->get($key_name))
{

   
$our_data_arary = new stdClass();
   
$our_data_arary->tralalala = '111';
   
$our_data_arary->status = 'enabled';

   
$cacheFactory->insert($key_name, $our_data_arary, 100);
}
var_dump($our_data_arary);