zoopcommerce / juggernaut
A caching module for PHP that includes adapters for File System, APC and Memcached. It also includes a number of helper classes for DB caching and full page cache
Installs: 1 443
Dependents: 1
Suggesters: 0
Security: 0
Stars: 6
Watchers: 3
Forks: 0
Open Issues: 2
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-12-16 15:25:59 UTC
README
#Juggernaut
Introduction
Juggernaut is a super fast PHP cache. It has a number of storage adapters and a few simple helpers to get you up and caching quickly.
One of the best aspects of this module is that it provides flood protection both on the initial cache creation - by queuing subsequent requests - but also on re-caching - by serving old cache for subsequent requests until the new cache has been re-created.
These two features allow Juggernaut to be at least 100% faster than the Zend Framework 2 cache adapters in normal use, and up to 270% faster on highly concurrent applications.
Installation
Install the module using Composer into your application's vendor directory. Add the following line to your
composer.json
.
{ "require": { "zoopcommerce/juggernaut": "dev-master" } }
Usage
You can use Juggernaut by directly instantiating a storage adapter and calling set/get.
Key-Value-Pair Caching Using Storage Adapters
File System
// you should always store the cache below the web root for security!! $cacheDirectory = __DIR__ . '../cache'; $cache = new Zoop\Juggernaut\Adapter\FileSystem($cacheDirectory); $key = 'yourUniqueKey'; $data = $cache->getItem($key, $success); // check if cache hit/miss if ($success === false) { // cache missed so now we have to execute // some query that takes a long time for($i=0;$i<1000000;$i++) { $data = rand(0, 10000); } //save it to cache $cache->setItem($key, $data); echo $data; } else { // cache hit! echo $data; }
MongoDB
$mongo = new MongoClient('mongodb://username:password@localhost:27017'); $collection = $mongo->selectCollection('Cache'); $cache = new Zoop\Juggernaut\Adapter\MongoDB($collection); $key = 'yourUniqueKey'; $data = $cache->getItem($key, $success); // check if cache hit/miss if ($success === false) { // cache missed so now we have to execute // some query that takes a long time for($i=0;$i<1000000;$i++) { $data = rand(0, 10000); } //save it to cache $cache->setItem($key, $data); echo $data; } else { // cache hit! echo $data; }
Memcached
//coming soon
MySQL
//coming soon
Helpers
There are a few helpers that will expidite the usage of Juggernaut.
Full Page
As the name suggests, the "Full Page" helper will store the rendered page directly to cache. This results in blindingly fast page loads.
To use this script just place the following at the top of your pages.
$pageTtl = 600; //10 mins $cacheDirectory = __DIR__ . '../cache'; $adapter = new Zoop\Juggernaut\Adapter\FileSystem($cacheDirectory); $pageCache = new Zoop\Juggernaut\Helper\FullPage($adapter, $pageTtl); $pageCache->start();
You can use any of the provided adapters to store the full page cache. eg.
$pageTtl = 600; //10 mins $database='MyMongoDb'; $username='mymongouser'; $password='mymongopass'; $adapter = new Zoop\Juggernaut\Adapter\MongoDB($database, $username, $password); $pageCache = new Zoop\Juggernaut\Helper\FullPage($adapter, $pageTtl); $pageCache->start();
There's no need to manually save the rendered page to cache as the script will automatically flush the page output to the cache adapter once the script exits.
Database
MySQLi
You can use the mysqli helper to automatically cache your sql queries.
$cacheDirectory = __DIR__ . '../cache'; $adapter = new Zoop\Juggernaut\Adapter\FileSystem($cacheDirectory); $db = new Zoop\Juggernaut\Helper\Database\Mysqli($cache); $db->connect($host, $username, $passwd, $database); $q="SELECT COUNT(`pageviews`) as 'pageviews' FROM `analytics` GROUP BY `date`"; $r = $db->query($q, 600); //second arg is ttl if($r!==false) { $pageviews = $db->fetchRow($q)['pageviews']; }
As you can see you don't have to worry if the cache exists or not as the helper does all the heavy lifting.
Coming soon
- Unit tests
- Working examples
- MySQL adapter
- Memcached adapter
- MongoDB helper