PHP Classes

Tiny PHP Cache Class: Store and retrieve data in several cache container

Recommend this page to a friend!
  Info   View files Example   View files View files (9)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 115 This week: 1All time: 9,555 This week: 560Up
Version License PHP version Categories
php-tiny-cacher 1.0Custom (specified...5PHP 5, Cache
Description 

Author

This class can store and retrieve data in several cache containers.

It can access different storage containers to store a given data string either in files, MemCached, Redis or MySQL database.

The class can also retrieve the same data from the storage container if it did not pass a given expiry time.

Picture of Enrico Sola
Name: Enrico Sola <contact>
Classes: 5 packages by
Country: Italy Italy
Age: 30
All time rank: 3209121 in Italy Italy
Week rank: 411 Up18 in Italy Italy Up

Example

<?php
require dirname(__FILE__) . '/php-tiny-cacher.php';

use
PHPTinyCacher\PHPTinyCacher;

$namespace = 'demo';
$ttl = 120;
$increment = 4;

$total = microtime(true);
$strategies = PHPTinyCacher::getSupportedStrategies();
echo
'Supported strategies: ' . implode(', ', $strategies) . PHP_EOL;
$cache = new PHPTinyCacher();
$cache->setNamespace($namespace);
$cache->setVerbose(true);
$cache->setDefaultTTL($ttl);
foreach (
$strategies as $key => $value ){
    echo
'Starting test using "' . $value . '" as strategy...' . PHP_EOL;
    switch (
$value ){
        case
'redis':{
           
$cache->connectToRedis('127.0.0.1', 6379);
        }break;
        case
'memcached':{
           
$cache->connectToMemcached(array(
                array(
'127.0.0.1', 11211)
            ));
        }break;
        case
'sqlite3':{
           
$cache->connectToSQLite('cache.db');
        }break;
        case
'file':{
           
$cache->setStorageDirectory('cache');
        }break;
    }
   
$cache->setStrategy($value);
   
$start = microtime(true);
    echo
'Pushing some elements into the cache...' . PHP_EOL;
   
$cache->pushMulti(array(
       
'cache-entry' => 'Some data that should be cached for next uses ?',
       
'foo' => 'bar',
       
'serialised' => array(1, 2, 3, 5, 'a', true),
       
'numeric' => 10
   
), true);
    echo
'Elements pushed into the cache, checking if one element exists...' . PHP_EOL;
    echo
'Does the element exist? ' . ( $cache->has('cache-entry') === true ? 'Yes.' : 'No.' ) . PHP_EOL;
    echo
'Retrieving the same element...' . PHP_EOL;
    echo
'Value: ' . $cache->pull('cache-entry', true) . PHP_EOL;
    echo
'Incrementing the numeric entry...' . PHP_EOL;
   
$cache->increment('numeric', $increment);
    echo
'Incremented value now is: ' . $cache->pull('numeric') . PHP_EOL;
    echo
'Removing the element...' . PHP_EOL;
   
$cache->remove('cache-entry');
    echo
'The element has been removed, dropping all elements from the cache...' . PHP_EOL;
   
$cache->invalidate(true);
    echo
'Cache content cleared.' . PHP_EOL;
    echo
'Test completed in ' . ( microtime(true) - $start ) . ' seconds.' . PHP_EOL . PHP_EOL;
}
echo
'All tests completed in ' . ( microtime(true) - $total ) . ' seconds.' . PHP_EOL . PHP_EOL;


Details

php-tiny-cacher


  Files folder image Files  
File Role Description
Accessible without login Plain text file changelog.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file database.sql Data Auxiliary data
Accessible without login Plain text file demo.php Example Example script
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file LICENSE.md Lic. License text
Plain text file php-tiny-cacher.php Class Class source
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file todo.txt Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
 100%
Total:115
This week:1
All time:9,555
This week:560Up