Blame view
sources/3rdparty/aws-sdk/lib/cachecore/cachefile.class.php
4.84 KB
|
03e52840d
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
<?php
/**
* Container for all file-based cache methods. Inherits additional methods from <CacheCore>. Adheres
* to the ICacheCore interface.
*
* @version 2012.04.17
* @copyright 2006-2012 Ryan Parman
* @copyright 2006-2010 Foleeo, Inc.
* @copyright 2012 Amazon.com, Inc. or its affiliates.
* @copyright 2008-2010 Contributors
* @license http://opensource.org/licenses/bsd-license.php Simplified BSD License
* @link http://github.com/skyzyx/cachecore CacheCore
* @link http://getcloudfusion.com CloudFusion
*/
class CacheFile extends CacheCore implements ICacheCore
{
/*%******************************************************************************************%*/
// CONSTRUCTOR
/**
* Constructs a new instance of this class.
*
* @param string $name (Required) A name to uniquely identify the cache object.
* @param string $location (Optional) The location to store the cache object in. This may vary by cache method. The default value is NULL.
* @param integer $expires (Optional) The number of seconds until a cache object is considered stale. The default value is 0.
* @param boolean $gzip (Optional) Whether data should be gzipped before being stored. The default value is true.
* @return object Reference to the cache object.
*/
public function __construct($name, $location = null, $expires = 0, $gzip = true)
{
parent::__construct($name, $location, $expires, $gzip);
$this->id = $this->location . '/' . $this->name . '.cache';
}
/**
* Creates a new cache.
*
* @param mixed $data (Required) The data to cache.
* @return boolean Whether the operation was successful.
*/
public function create($data)
{
if (file_exists($this->id))
{
return false;
}
elseif (realpath($this->location) && file_exists($this->location) && is_writeable($this->location))
{
$data = serialize($data);
$data = $this->gzip ? gzcompress($data) : $data;
return (bool) file_put_contents($this->id, $data);
}
elseif (realpath($this->location) && file_exists($this->location))
{
throw new CacheFile_Exception('The file system location "' . $this->location . '" is not writable. Check the file system permissions for this directory.');
}
else
{
throw new CacheFile_Exception('The file system location "' . $this->location . '" does not exist. Create the directory, or double-check any relative paths that may have been set.');
}
return false;
}
/**
* Reads a cache.
*
* @return mixed Either the content of the cache object, or boolean `false`.
*/
public function read()
{
if (file_exists($this->id) && is_readable($this->id))
{
$data = file_get_contents($this->id);
$data = $this->gzip ? gzuncompress($data) : $data;
$data = unserialize($data);
if ($data === false)
{
/*
This should only happen when someone changes the gzip settings and there is
existing data or someone has been mucking about in the cache folder manually.
Delete the bad entry since the file cache doesn't clean up after itself and
then return false so fresh data will be retrieved.
*/
$this->delete();
return false;
}
return $data;
}
return false;
}
/**
* Updates an existing cache.
*
* @param mixed $data (Required) The data to cache.
* @return boolean Whether the operation was successful.
*/
public function update($data)
{
if (file_exists($this->id) && is_writeable($this->id))
{
$data = serialize($data);
$data = $this->gzip ? gzcompress($data) : $data;
return (bool) file_put_contents($this->id, $data);
}
else
{
throw new CacheFile_Exception('The file system location is not writeable. Check your file system permissions and ensure that the cache directory exists.');
}
return false;
}
/**
* Deletes a cache.
*
* @return boolean Whether the operation was successful.
*/
public function delete()
{
if (file_exists($this->id))
{
return unlink($this->id);
}
return false;
}
/**
* Checks whether the cache object is expired or not.
*
* @return boolean Whether the cache is expired or not.
*/
public function is_expired()
{
if ($this->timestamp() + $this->expires < time())
{
return true;
}
return false;
}
/**
* Retrieves the timestamp of the cache.
*
* @return mixed Either the Unix time stamp of the cache creation, or boolean `false`.
*/
public function timestamp()
{
clearstatcache();
if (file_exists($this->id))
{
$this->timestamp = filemtime($this->id);
return $this->timestamp;
}
return false;
}
/**
* Resets the freshness of the cache.
*
* @return boolean Whether the operation was successful.
*/
public function reset()
{
if (file_exists($this->id))
{
return touch($this->id);
}
return false;
}
}
/*%******************************************************************************************%*/
// EXCEPTIONS
class CacheFile_Exception extends CacheCore_Exception {}
|