Skip to content

iandev/php-vcache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 

Repository files navigation

php-vcache

Generic light weight PHP page caching tool for generating and serving static HTML documents (with gzip compression). Perfect for high traffic blogging websites or for websites that don't have to serve constantly updating or time critical content.

Usage:

Place this in your index.php or wherever your application root is. This was tested with wordpress by entering this code in the index.php file and substituting the commented section below (/your PHP application starting point here/) with the original wordpress index.php code.

include "cache.php";

//for gzip support, change this to GzipFileRepository
use \ViewCache\FileRepository as Repository;
use \ViewCache\BufferedCache as Cache;
use \ViewCache\FileExpirationCachePolicy as CachePolicy1;
use \ViewCache\HttpMethodCachePolicy as CachePolicy2;
use \ViewCache\UriRegexCachePolicy as CachePolicy3;
use \ViewCache\FileLogger as Logger;
use \ViewCache\LogType as LogType;

$key = $_SERVER["REQUEST_URI"];
$expiration_seconds = 30;
$cache_dir = "cachedir";

$logger = Logger::instance("vcache-error.log");
$repository = new Repository($cache_dir);
$cache = new Cache(
  array(
    //cache pages for specific amount of seconds
    new CachePolicy1($repository, $expiration_seconds), 
    //only cache pages using certain http method calls
    new CachePolicy2(array("GET"=>array("allowed"=>true))),
    //only cache pages that match the request uri
    new CachePolicy3(array("/p=1/", "/p=2/"))
  ), 
  $repository,
  $logger
);

$page_contents = $cache->get($key);
if($page_contents == null || empty($page_contents)) {
  $cache->bufferStart();
  
  /*
    your PHP application starting point here
  */
  
  $page_contents = $cache->bufferGetEndSave($key);
}

echo $page_contents;

The MIT License (MIT)

Copyright (c) 2013 Ian Herbert

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages