Redbean + Smarty update

This commit is contained in:
Robin Kloppe 2014-10-13 11:24:53 +02:00
parent c1b8226466
commit eb0835ff74
122 changed files with 12653 additions and 11637 deletions

View file

@ -2,44 +2,43 @@
/**
* Smarty Internal Plugin
*
* @package Smarty
* @package Smarty
* @subpackage Cacher
*/
/**
* Smarty Cache Handler Base for Key/Value Storage Implementations
*
* This class implements the functionality required to use simple key/value stores
* for hierarchical cache groups. key/value stores like memcache or APC do not support
* wildcards in keys, therefore a cache group cannot be cleared like "a|*" - which
* is no problem to filesystem and RDBMS implementations.
*
* This implementation is based on the concept of invalidation. While one specific cache
* can be identified and cleared, any range of caches cannot be identified. For this reason
* each level of the cache group hierarchy can have its own value in the store. These values
* are nothing but microtimes, telling us when a particular cache group was cleared for the
* last time. These keys are evaluated for every cache read to determine if the cache has
* been invalidated since it was created and should hence be treated as inexistent.
*
* Although deep hierarchies are possible, they are not recommended. Try to keep your
* cache groups as shallow as possible. Anything up 3-5 parents should be ok. So
* »a|b| is a good depth where »a|b|c|d|e|f|g|h|i|j| isn't. Try to join correlating
* cache groups: if your cache groups look somewhat like »a|b|$page|$items|$whatever«
* consider using »a|b|c|$page-$items-$whatever« instead.
*
* @package Smarty
* @package Smarty
* @subpackage Cacher
* @author Rodney Rehm
* @author Rodney Rehm
*/
abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
{
/**
* cache for contents
*
* @var array
*/
protected $contents = array();
/**
* cache for timestamps
*
* @var array
*/
protected $timestamps = array();
@ -49,14 +48,15 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
*
* @param Smarty_Template_Cached $cached cached object
* @param Smarty_Internal_Template $_template template object
*
* @return void
*/
public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template)
{
$cached->filepath = $_template->source->uid
. '#' . $this->sanitize($cached->source->name)
. '#' . $this->sanitize($cached->cache_id)
. '#' . $this->sanitize($cached->compile_id);
. '#' . $this->sanitize($cached->source->resource)
. '#' . $this->sanitize($cached->cache_id)
. '#' . $this->sanitize($cached->compile_id);
$this->populateTimestamp($cached);
}
@ -65,6 +65,7 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
* populate Cached Object with timestamp and exists from Resource
*
* @param Smarty_Template_Cached $cached cached object
*
* @return void
*/
public function populateTimestamp(Smarty_Template_Cached $cached)
@ -82,9 +83,10 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
*
* @param Smarty_Internal_Template $_template template object
* @param Smarty_Template_Cached $cached cached object
* @return booelan true or false if the cached content does not exist
*
* @return boolean true or false if the cached content does not exist
*/
public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached=null)
public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached = null)
{
if (!$cached) {
$cached = $_template->cached;
@ -97,6 +99,9 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
}
}
if (isset($content)) {
/** @var Smarty_Internal_Template $_smarty_tpl
* used in evaluated code
*/
$_smarty_tpl = $_template;
eval("?>" . $content);
@ -111,6 +116,7 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
*
* @param Smarty_Internal_Template $_template template object
* @param string $content content to cache
*
* @return boolean success
*/
public function writeCachedContent(Smarty_Internal_Template $_template, $content)
@ -122,27 +128,26 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
/**
* Empty cache
*
* {@internal the $exp_time argument is ignored altogether }}
*
* @param Smarty $smarty Smarty object
* @param integer $exp_time expiration time [being ignored]
*
* @return integer number of cache files deleted [always -1]
* @uses purge() to clear the whole store
* @uses invalidate() to mark everything outdated if purge() is inapplicable
*/
public function clearAll(Smarty $smarty, $exp_time=null)
public function clearAll(Smarty $smarty, $exp_time = null)
{
if (!$this->purge()) {
$this->invalidate(null);
}
return -1;
return - 1;
}
/**
* Empty cache for a specific template
*
* {@internal the $exp_time argument is ignored altogether}}
*
* @param Smarty $smarty Smarty object
@ -150,6 +155,7 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
* @param string $cache_id cache id
* @param string $compile_id compile id
* @param integer $exp_time expiration time [being ignored]
*
* @return integer number of cache files deleted [always -1]
* @uses buildCachedFilepath() to generate the CacheID
* @uses invalidate() to mark CacheIDs parent chain as outdated
@ -162,8 +168,9 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
$this->delete(array($cid));
$this->invalidate($cid, $resource_name, $cache_id, $compile_id, $uid);
return -1;
return - 1;
}
/**
* Get template's unique ID
*
@ -171,6 +178,7 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
* @param string $resource_name template name
* @param string $cache_id cache id
* @param string $compile_id compile id
*
* @return string filepath of cache file
*/
protected function getTemplateUid(Smarty $smarty, $resource_name, $cache_id, $compile_id)
@ -201,6 +209,7 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
* Sanitize CacheID components
*
* @param string $string CacheID component to sanitize
*
* @return string sanitized CacheID component
*/
protected function sanitize($string)
@ -224,6 +233,7 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
* @param string $content cached content
* @param integer &$timestamp cached timestamp (epoch)
* @param string $resource_uid resource's uid
*
* @return boolean success
*/
protected function fetch($cid, $resource_name = null, $cache_id = null, $compile_id = null, &$content = null, &$timestamp = null, $resource_uid = null)
@ -245,7 +255,6 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
/**
* Add current microtime to the beginning of $cache_content
*
* {@internal the header uses 8 Bytes, the first 4 Bytes are the seconds, the second 4 Bytes are the microseconds}}
*
* @param string &$content the content to be cached
@ -261,6 +270,7 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
* Extract the timestamp the $content was cached
*
* @param string &$content the cached content
*
* @return float the microtime the content was cached
*/
protected function getMetaTimestamp(&$content)
@ -280,6 +290,7 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
* @param string $cache_id cache id
* @param string $compile_id compile id
* @param string $resource_uid source's uid
*
* @return void
*/
protected function invalidate($cid = null, $resource_name = null, $cache_id = null, $compile_id = null, $resource_uid = null)
@ -289,22 +300,24 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
// invalidate everything
if (!$resource_name && !$cache_id && !$compile_id) {
$key = 'IVK#ALL';
}
// invalidate all caches by template
else if ($resource_name && !$cache_id && !$compile_id) {
$key = 'IVK#TEMPLATE#' . $resource_uid . '#' . $this->sanitize($resource_name);
}
// invalidate all caches by cache group
else if (!$resource_name && $cache_id && !$compile_id) {
$key = 'IVK#CACHE#' . $this->sanitize($cache_id);
}
// invalidate all caches by compile id
else if (!$resource_name && !$cache_id && $compile_id) {
$key = 'IVK#COMPILE#' . $this->sanitize($compile_id);
}
// invalidate by combination
} // invalidate all caches by template
else {
$key = 'IVK#CID#' . $cid;
if ($resource_name && !$cache_id && !$compile_id) {
$key = 'IVK#TEMPLATE#' . $resource_uid . '#' . $this->sanitize($resource_name);
} // invalidate all caches by cache group
else {
if (!$resource_name && $cache_id && !$compile_id) {
$key = 'IVK#CACHE#' . $this->sanitize($cache_id);
} // invalidate all caches by compile id
else {
if (!$resource_name && !$cache_id && $compile_id) {
$key = 'IVK#COMPILE#' . $this->sanitize($compile_id);
} // invalidate by combination
else {
$key = 'IVK#CID#' . $cid;
}
}
}
}
$this->write(array($key => $now));
}
@ -317,6 +330,7 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
* @param string $cache_id cache id
* @param string $compile_id compile id
* @param string $resource_uid source's filepath
*
* @return float the microtime the CacheID was invalidated
*/
protected function getLatestInvalidationTimestamp($cid, $resource_name = null, $cache_id = null, $compile_id = null, $resource_uid = null)
@ -342,7 +356,6 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
/**
* Translate a CacheID into the list of applicable InvalidationKeys.
*
* Splits "some|chain|into|an|array" into array( '#clearAll#', 'some', 'some|chain', 'some|chain|into', ... )
*
* @param string $cid CacheID to translate
@ -350,6 +363,7 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
* @param string $cache_id cache id
* @param string $compile_id compile id
* @param string $resource_uid source's filepath
*
* @return array list of InvalidationKeys
* @uses $invalidationKeyPrefix to prepend to each InvalidationKey
*/
@ -387,7 +401,7 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
$t[] = 'IVK#CACHE#' . $part;
$t[] = 'IVK#CID' . $_name . $part . $_compile;
// skip past delimiter position
$i++;
$i ++;
}
return $t;
@ -398,7 +412,8 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
*
* @param Smarty $smarty Smarty object
* @param Smarty_Template_Cached $cached cached object
* @return booelan true or false if cache is locked
*
* @return boolean true or false if cache is locked
*/
public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
{
@ -413,6 +428,8 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
*
* @param Smarty $smarty Smarty object
* @param Smarty_Template_Cached $cached cached object
*
* @return bool|void
*/
public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
{
@ -426,6 +443,8 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
*
* @param Smarty $smarty Smarty object
* @param Smarty_Template_Cached $cached cached object
*
* @return bool|void
*/
public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
{
@ -438,6 +457,7 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
* Read values for a set of keys from cache
*
* @param array $keys list of keys to fetch
*
* @return array list of values with the given keys used as indexes
*/
abstract protected function read(array $keys);
@ -445,16 +465,18 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
/**
* Save values for a set of keys to cache
*
* @param array $keys list of values to save
* @param int $expire expiration time
* @param array $keys list of values to save
* @param int $expire expiration time
*
* @return boolean true on success, false on failure
*/
abstract protected function write(array $keys, $expire=null);
abstract protected function write(array $keys, $expire = null);
/**
* Remove values from cache
*
* @param array $keys list of keys to delete
* @param array $keys list of keys to delete
*
* @return boolean true on success, false on failure
*/
abstract protected function delete(array $keys);
@ -468,5 +490,4 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
{
return false;
}
}