PHP Classes

File: CacheExtendedTest.php

Recommend this page to a friend!
  Classes of Andrei Alexandru   Simple Cache System   CacheExtendedTest.php   Download  
File: CacheExtendedTest.php
Role: Example script
Content type: text/plain
Description: How to use the MySQLStorage we created.
Class: Simple Cache System
Store and retrieve cached data in MySQL and files
Author: By
Last change:
Date: 7 years ago
Size: 1,172 bytes
 

Contents

Class file image Download
<?php

use Atto\Cache\Cache;

require
'Atto/Cache/Cache.php';
require
'Atto/Cache/Item.php';
require
'Atto/Cache/Storage.php';
require
'MySQLStorage.php';


// Lets create this storage now, a custom one
$storage = new MySQLStorage('localhost', 'root', '', 'database', 'cache');

// We use the MySQL Storage engine now
$cache = new Cache($storage, 60); // By default all items have 60 seconds of time to live

// Create some random data
$data = ['foo', 'bar', 'baz'];

// Store new items
$cache->store('some.id.key', $data); // $data will live for 60 seconds (default - Cache constructor)
$cache->store('another.key', $data, 60 * 60 * 24 * 7); // Now $data will live for 7 days
$cache->store('forever', $data, 0); // Using a $ttl less than 1 will indicate that this item never expires

// Retrieve items from the cache
$data = $cache->get('some.id.key');

print_r($cache->get('some.id.key'));
print_r($cache->get('forever'));

if (
$data === null) {
   
// $data does not exist in the cache storage
   
$data = ['foo', 'bar', 'baz'];
}

// Invalidate data
$cache->invalidate('another.key'); // This will remove an item from the cache storage