1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-13 17:44:00 +02:00

Moved file caching to Minify_Cache_File.

Removed Cache_Lite.
Deprecated UseServerCache() in favor of setCache().
Added Groups controller to ab tests.
This commit is contained in:
Steve Clay
2008-06-22 15:08:30 +00:00
parent a6d7115a6c
commit 4f615c12f3
15 changed files with 201 additions and 1058 deletions

98
lib/Minify/Cache/File.php Normal file
View File

@@ -0,0 +1,98 @@
<?php
/**
* Class Minify_Cache_File
* @package Minify
*/
class Minify_Cache_File {
public function __construct($path = '')
{
if (! $path) {
require_once 'Solar/Dir.php';
$path = rtrim(Solar_Dir::tmp(), DIRECTORY_SEPARATOR);
}
$this->_path = $path;
}
/**
* Write data to cache.
*
* @param string $id cache id (e.g. a filename)
*
* @param string $data
*
* @return bool success
*/
public function store($id, $data)
{
return self::_verifiedWrite($this->_path . '/' . $id, $data);
}
/**
* Get the size of a cache entry
*
* @param string $id cache id (e.g. a filename)
*
* @return int size in bytes
*/
public function getSize($id)
{
return filesize($this->_path . '/' . $id);
}
/**
* Does a valid cache entry exist?
*
* @param string $id cache id (e.g. a filename)
*
* @param int $srcMtime mtime of the original source file(s)
*
* @return bool exists
*/
public function isValid($id, $srcMtime)
{
$file = $this->_path . '/' . $id;
return (file_exists($file) && (filemtime($file) >= $srcMtime));
}
/**
* Send the cached content to output
*
* @param string $id cache id (e.g. a filename)
*/
public function display($id)
{
readfile($this->_path . '/' . $id);
}
/**
* Fetch the cached content
*
* @param string $id cache id (e.g. a filename)
*
* @return string
*/
public function fetch($id)
{
return file_get_contents($this->_path . '/' . $id);
}
private $_path = null;
/**
* Write data to file and verify its contents
*
* @param string $file path
*
* @param string $data
*
* @return bool success
*/
private static function _verifiedWrite($file, $data)
{
return (file_put_contents($file, $data, LOCK_EX)
&& (md5($data) === md5_file($file))
);
}
}