123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513 |
- <?php
-
-
-
- abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
- {
-
-
- protected $contents = array();
-
-
-
- protected $timestamps = array();
-
-
-
- public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template)
- {
- $cached->filepath = $_template->source->uid . '#' . $this->sanitize($cached->source->resource) . '#' .
- $this->sanitize($cached->cache_id) . '#' . $this->sanitize($cached->compile_id);
-
- $this->populateTimestamp($cached);
- }
-
-
-
- public function populateTimestamp(Smarty_Template_Cached $cached)
- {
- if (!$this->fetch($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id, $content, $timestamp, $cached->source->uid)) {
- return;
- }
- $cached->content = $content;
- $cached->timestamp = (int) $timestamp;
- $cached->exists = $cached->timestamp;
- }
-
-
-
- public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached = null, $update = false)
- {
- if (!$cached) {
- $cached = $_template->cached;
- }
- $content = $cached->content ? $cached->content : null;
- $timestamp = $cached->timestamp ? $cached->timestamp : null;
- if ($content === null || !$timestamp) {
- if (!$this->fetch($_template->cached->filepath, $_template->source->name, $_template->cache_id, $_template->compile_id, $content, $timestamp, $_template->source->uid)) {
- return false;
- }
- }
- if (isset($content)) {
-
-
- $_smarty_tpl = $_template;
- eval("?>" . $content);
-
- return true;
- }
-
- return false;
- }
-
-
-
- public function writeCachedContent(Smarty_Internal_Template $_template, $content)
- {
- $this->addMetaTimestamp($content);
-
- return $this->write(array($_template->cached->filepath => $content), $_template->cache_lifetime);
- }
-
-
-
- public function readCachedContent(Smarty_Internal_Template $_template)
- {
- $content = $_template->cached->content ? $_template->cached->content : null;
- $timestamp = null;
- if ($content === null) {
- if (!$this->fetch($_template->cached->filepath, $_template->source->name, $_template->cache_id, $_template->compile_id, $content, $timestamp, $_template->source->uid)) {
- return false;
- }
- }
- if (isset($content)) {
- return $content;
- }
- return false;
- }
-
-
-
- public function clearAll(Smarty $smarty, $exp_time = null)
- {
- if (!$this->purge()) {
- $this->invalidate(null);
- }
-
- if (isset($smarty->_cache['template_objects'])) {
- foreach ($smarty->_cache['template_objects'] as $key => $tpl) {
- if (isset($tpl->cached)) {
- unset($smarty->_cache['template_objects'][$key]);
- }
- }
- }
- return - 1;
- }
-
-
-
- public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
- {
- $uid = $this->getTemplateUid($smarty, $resource_name);
- $cid = $uid . '#' . $this->sanitize($resource_name) . '#' . $this->sanitize($cache_id) . '#' .
- $this->sanitize($compile_id);
- $this->delete(array($cid));
- $this->invalidate($cid, $resource_name, $cache_id, $compile_id, $uid);
-
- if (isset($resource_name) && isset($smarty->_cache['template_objects'])) {
- if (isset($smarty->_cache['template_objects'])) {
- foreach ($smarty->_cache['template_objects'] as $key => $tpl) {
- if ($tpl->source->uid == $uid && isset($tpl->cached)) {
- unset($smarty->_cache['template_objects'][$key]);
- }
- }
- }
- }
- return - 1;
- }
-
-
-
- protected function getTemplateUid(Smarty $smarty, $resource_name)
- {
- if (isset($resource_name)) {
- $source = Smarty_Template_Source::load(null, $smarty, $resource_name);
- if ($source->exists) {
- return $source->uid;
- }
- }
- return '';
- }
-
-
-
- protected function sanitize($string)
- {
- $string = trim($string, '|');
- if (!$string) {
- return null;
- }
- return preg_replace('#[^\w\|]+#S', '_', $string);
- }
-
-
-
- protected function fetch($cid, $resource_name = null, $cache_id = null, $compile_id = null, &$content = null, &$timestamp = null, $resource_uid = null)
- {
- $t = $this->read(array($cid));
- $content = !empty($t[$cid]) ? $t[$cid] : null;
- $timestamp = null;
-
- if ($content && ($timestamp = $this->getMetaTimestamp($content))) {
- $invalidated = $this->getLatestInvalidationTimestamp($cid, $resource_name, $cache_id, $compile_id, $resource_uid);
- if ($invalidated > $timestamp) {
- $timestamp = null;
- $content = null;
- }
- }
-
- return !!$content;
- }
-
-
-
- protected function addMetaTimestamp(&$content)
- {
- $mt = explode(" ", microtime());
- $ts = pack("NN", $mt[1], (int) ($mt[0] * 100000000));
- $content = $ts . $content;
- }
-
-
-
- protected function getMetaTimestamp(&$content)
- {
- extract(unpack('N1s/N1m/a*content', $content));
- return $s + ($m / 100000000);
- }
-
-
-
- protected function invalidate($cid = null, $resource_name = null, $cache_id = null, $compile_id = null, $resource_uid = null)
- {
- $now = microtime(true);
- $key = null;
-
- if (!$resource_name && !$cache_id && !$compile_id) {
- $key = 'IVK#ALL';
- }
- else {
- if ($resource_name && !$cache_id && !$compile_id) {
- $key = 'IVK#TEMPLATE#' . $resource_uid . '#' . $this->sanitize($resource_name);
- }
- else {
- if (!$resource_name && $cache_id && !$compile_id) {
- $key = 'IVK#CACHE#' . $this->sanitize($cache_id);
- }
- else {
- if (!$resource_name && !$cache_id && $compile_id) {
- $key = 'IVK#COMPILE#' . $this->sanitize($compile_id);
- }
- else {
- $key = 'IVK#CID#' . $cid;
- }
- }
- }
- }
- $this->write(array($key => $now));
- }
-
-
-
- protected function getLatestInvalidationTimestamp($cid, $resource_name = null, $cache_id = null, $compile_id = null, $resource_uid = null)
- {
-
- if (false && !$cid) {
- return 0;
- }
-
- if (!($_cid = $this->listInvalidationKeys($cid, $resource_name, $cache_id, $compile_id, $resource_uid))) {
- return 0;
- }
-
-
- if (!($values = $this->read($_cid))) {
- return 0;
- }
-
- $values = array_map('floatval', $values);
-
- return max($values);
- }
-
-
-
- protected function listInvalidationKeys($cid, $resource_name = null, $cache_id = null, $compile_id = null, $resource_uid = null)
- {
- $t = array('IVK#ALL');
- $_name = $_compile = '#';
- if ($resource_name) {
- $_name .= $resource_uid . '#' . $this->sanitize($resource_name);
- $t[] = 'IVK#TEMPLATE' . $_name;
- }
- if ($compile_id) {
- $_compile .= $this->sanitize($compile_id);
- $t[] = 'IVK#COMPILE' . $_compile;
- }
- $_name .= '#';
- $cid = trim($cache_id, '|');
- if (!$cid) {
- return $t;
- }
- $i = 0;
- while (true) {
-
- $i = strpos($cid, '|', $i);
-
- if ($i === false) {
- $t[] = 'IVK#CACHE#' . $cid;
- $t[] = 'IVK#CID' . $_name . $cid . $_compile;
- $t[] = 'IVK#CID' . $_name . $_compile;
- break;
- }
- $part = substr($cid, 0, $i);
-
- $t[] = 'IVK#CACHE#' . $part;
- $t[] = 'IVK#CID' . $_name . $part . $_compile;
-
- $i ++;
- }
-
- return $t;
- }
-
-
-
- public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
- {
- $key = 'LOCK#' . $cached->filepath;
- $data = $this->read(array($key));
-
- return $data && time() - $data[$key] < $smarty->locking_timeout;
- }
-
-
-
- public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
- {
- $cached->is_locked = true;
- $key = 'LOCK#' . $cached->filepath;
- $this->write(array($key => time()), $smarty->locking_timeout);
- }
-
-
-
- public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
- {
- $cached->is_locked = false;
- $key = 'LOCK#' . $cached->filepath;
- $this->delete(array($key));
- }
-
-
-
- abstract protected function read(array $keys);
-
-
-
- abstract protected function write(array $keys, $expire = null);
-
-
-
- abstract protected function delete(array $keys);
-
-
-
- protected function purge()
- {
- return false;
- }
- }
|