protected function __construct() { try { $this->session = CacheContext::factory(self::SESSION_PREFIX); } catch (Exception $ex) { $this->session = new SessionCacheEngine(); $this->session->configKey = self::SESSION_PREFIX; } }
/** * @param string $key The object KEY * @param int $ttl The time to live in seconds of the object. Depends on implementation. * @return object The Object */ public function get($key, $ttl = 0) { $log = LogHandler::getInstance(); if ($ttl === false) { $log->info("[Shmop Cache] Ignored {$key} because TTL=FALSE"); return null; } if (CacheContext::getInstance()->getReset()) { $log->info("[Shmop Cache] Failed to get {$key} because RESET=true"); return null; } if (CacheContext::getInstance()->getNoCache()) { $log->info("[Shmop Cache] Failed to get {$key} because NOCACHE=true"); return null; } $fileKey = $this->getKeyId($key); // Opened $shm_id = @shmop_open($fileKey, "a", 0, 0); if (!$shm_id) { $log->info("[Shmop Cache] '{$key}' not exists"); return null; } $fileAge = filemtime($this->getFTok($key)); // Check if ($ttl > 0 && intval(time() - $fileAge) > $ttl) { $log->info("[Shmop Cache] File too old. Ignoring '{$key}'"); // Close old descriptor shmop_close($shm_id); // delete old memory segment $shm_id = shmop_open($fileKey, "w", $this->getDefaultPermission(), $this->getMaxSize()); shmop_delete($shm_id); shmop_close($shm_id); return null; } $log->info("[Shmop Cache] Get '{$key}'"); $serialized = shmop_read($shm_id, 0, shmop_size($shm_id)); shmop_close($shm_id); return unserialize($serialized); }
/** * Get the thread result from the shared memory block and erase it * * @return mixed * @throws \Error * @throws object */ public function getResult() { if (is_null($this->threadKey)) { return null; } $key = $this->threadKey; $this->threadKey = null; $cache = CacheContext::factory('phpthread'); $result = $cache->get($key); $cache->release($key); if (is_object($result) && (is_subclass_of($result, '\\Error') || is_subclass_of($result, '\\Exception'))) { throw $result; } return $result; }
/** * * @param string $key * @param string $str * @return bool */ public function append($key, $str) { $this->lazyLoadMemCachedServers(); $log = LogHandler::getInstance(); if (!CacheContext::getInstance()->getNoCache()) { $log->info("[Memcached] Append '{$key}' in Memcached"); return $this->_memCached->append($key, $str); } else { $log->info("[Memcached] Not Set '{$key}' because NOCACHE=true"); } return true; }
/** * @param string $key The object Key * @param object $object The object to be cached * @param int $ttl The time to live in seconds of this objects * @return bool If the object is successfully posted */ public function append($key, $content, $ttl = 0) { $log = LogHandler::getInstance(); $fileKey = $this->fixKey($key); if (!CacheContext::getInstance()->getNoCache()) { $log->info("[Filesystem cache] Append '{$key}' in FileSystem"); try { file_put_contents($fileKey, serialize($content), true); } catch (Exception $ex) { echo "<br/><b>Warning:</b> I could not write to cache on file '" . basename($key) . "'. Switching to nocache=true mode. <br/>"; } } else { $log->info("[Filesystem cache] Not Set '{$key}' because NOCACHE=true"); } }
<?php require "vendor/autoload.php"; session_start(); // Get the 'default' object in the config/cacheconfig.php $pool = \ByJG\Cache\CacheContext::psrFactory(); $item = $pool->getItem('key'); if (!$item->isHit()) { $item->set('My Value'); $pool->save($item); } $item2 = $pool->getItem('key'); if ($item2->isHit()) { echo "-- Hit --\n"; } else { echo "-- Not Hit --\n"; } print_r($_SESSION); $pool->deleteItem($item->getKey()); print_r($_SESSION); for ($i = 0; $i < 4; $i++) { $item = $pool->getItem('key' . $i); $item->set('value - ' . $i); $pool->saveDeferred($item); } $pool->commit(); print_r($_SESSION);