本文整理汇总了PHP中Zend\Cache\Storage\StorageInterface::getItem方法的典型用法代码示例。如果您正苦于以下问题:PHP StorageInterface::getItem方法的具体用法?PHP StorageInterface::getItem怎么用?PHP StorageInterface::getItem使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Zend\Cache\Storage\StorageInterface
的用法示例。
在下文中一共展示了StorageInterface::getItem方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: load
/**
* {@inheritdoc}
*/
public function load()
{
$contents = $this->storage->getItem($this->key);
if ($contents !== null) {
$this->setFromStorage($contents);
}
}
示例2: getStatus
/**
* {@inheritdoc}
*
* @see \Cerberus\CerberusInterface::getStatus()
*/
public function getStatus($serviceName = null)
{
$this->setNamespace($serviceName);
$success = false;
$failures = (int) $this->storage->getItem('failures', $success);
if (!$success) {
$failures = 0;
$this->storage->setItem('failures', $failures);
}
// Still has failures left
if ($failures < $this->maxFailures) {
return CerberusInterface::CLOSED;
}
$success = false;
$lastAttempt = $this->storage->getItem('last_attempt', $success);
// This is the first attempt after a failure, open the circuit
if (!$success) {
$lastAttempt = time();
$this->storage->setItem('last_attempt', $lastAttempt);
return CerberusInterface::OPEN;
}
// Reached maxFailues but has passed the timeout limit, so we can try again
// We update the lastAttempt so only one call passes through
if (time() - $lastAttempt >= $this->timeout) {
$lastAttempt = time();
$this->storage->setItem('last_attempt', $lastAttempt);
return CerberusInterface::HALF_OPEN;
}
return CerberusInterface::OPEN;
}
示例3: get
/**
* @param mixed $key
* @return mixed
*/
public function get($key)
{
if (!$this->isKey($key)) {
$key = $this->createKey($key);
}
return unserialize($this->cache->getItem($key));
}
示例4: provide
public function provide($container)
{
$instance = $this->instanceManager->getInstanceFromRequest();
$pages = [];
try {
$container = $this->navigationManager->findContainerByNameAndInstance($container, $instance);
} catch (ContainerNotFoundException $e) {
return [];
}
$key = hash('sha256', serialize($container));
if ($this->storage->hasItem($key)) {
return $this->storage->getItem($key);
}
foreach ($container->getPages() as $page) {
$addPage = $this->buildPage($page);
$hasUri = isset($addPage['uri']);
$hasMvc = isset($addPage['action']) || isset($addPage['controller']) || isset($addPage['route']);
$hasProvider = isset($addPage['provider']);
if ($hasUri || $hasMvc || $hasProvider) {
$pages[] = $addPage;
}
}
$this->storage->setItem($key, $pages);
return $pages;
}
示例5: getValue
/**
* @param null|array $arguments Must be serializable.
* @return mixed
*/
public function getValue($arguments = null)
{
$cacheKey = Cache::makeCacheKey($this->name, $arguments);
if (!$this->storage->hasItem($cacheKey)) {
$this->warm($arguments);
}
return unserialize($this->storage->getItem($cacheKey));
}
示例6: fetch
/**
* @since 1.1
*
* {@inheritDoc}
*/
public function fetch($id)
{
if ($this->contains($id)) {
$this->cacheHits++;
return $this->cache->getItem($id);
}
$this->cacheMisses++;
return false;
}
示例7: getItem
/**
* Returns a Cache Item representing the specified key.
*
* This method must always return a CacheItemInterface object, even in case of
* a cache miss. It MUST NOT return null.
*
* @param string $key
* The key for which to return the corresponding Cache Item.
*
* @throws InvalidArgumentException
* If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException
* MUST be thrown.
*
* @return CacheItemInterface
* The corresponding Cache Item.
*/
public function getItem($key)
{
$this->validateKey($key);
try {
$cacheItem = $this->storage->getItem($key, $success);
} catch (Exception\InvalidArgumentException $e) {
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
} catch (Exception\ExceptionInterface $e) {
throw new CacheException($e->getMessage(), $e->getCode(), $e);
}
return new CacheItem($key, $success ? $cacheItem : null, $success);
}
示例8: getResult
protected function getResult()
{
if ($this->cache->hasItem('result')) {
return $this->cache->getItem('result');
}
// The bellow code do not work with zend
// $this->cache->setItem('result', $this->calculation);
// $result = $this->cache->getItem('result');
$calculation = $this->calculation;
$result = $calculation();
$this->cache->setItem('result', $result);
return $result;
}
示例9: getUnrevisedRevisions
public function getUnrevisedRevisions(TaxonomyTermInterface $term)
{
$key = hash('sha256', serialize($term));
if ($this->storage->hasItem($key)) {
return $this->storage->getItem($key);
}
$entities = $this->getEntities($term);
$collection = new ArrayCollection();
$this->iterEntities($entities, $collection, 'isRevised');
$iterator = $collection->getIterator();
$iterator->ksort();
$collection = new ArrayCollection(iterator_to_array($iterator));
$this->storage->setItem($key, $collection);
return $collection;
}
示例10: findSourceByAlias
public function findSourceByAlias($alias, $useCache = false)
{
if (!is_string($alias)) {
throw new Exception\InvalidArgumentException(sprintf('Expected alias to be string but got "%s"', gettype($alias)));
}
$key = 'source:by:alias:' . $alias;
if ($useCache && $this->storage->hasItem($key)) {
// The item is null so it didn't get found.
$item = $this->storage->getItem($key);
if ($item === self::CACHE_NONEXISTENT) {
throw new Exception\AliasNotFoundException(sprintf('Alias `%s` not found.', $alias));
}
return $item;
}
/* @var $entity Entity\AliasInterface */
$criteria = ['alias' => $alias];
$order = ['timestamp' => 'DESC'];
$results = $this->getAliasRepository()->findBy($criteria, $order);
$entity = current($results);
if (!is_object($entity)) {
$this->storage->setItem($key, self::CACHE_NONEXISTENT);
throw new Exception\AliasNotFoundException(sprintf('Alias `%s` not found.', $alias));
}
$source = $entity->getSource();
if ($useCache) {
$this->storage->setItem($key, $source);
}
return $source;
}
示例11: getItemsByPage
/**
* Returns the items for a given page.
*
* @param integer $pageNumber
* @return mixed
*/
public function getItemsByPage($pageNumber)
{
$pageNumber = $this->normalizePageNumber($pageNumber);
if ($this->cacheEnabled()) {
$data = self::$cache->getItem($this->_getCacheId($pageNumber));
if ($data) {
return $data;
}
}
$offset = ($pageNumber - 1) * $this->getItemCountPerPage();
$items = $this->adapter->getItems($offset, $this->getItemCountPerPage());
$filter = $this->getFilter();
if ($filter !== null) {
$items = $filter->filter($items);
}
if (!$items instanceof Traversable) {
$items = new ArrayIterator($items);
}
if ($this->cacheEnabled()) {
$cacheId = $this->_getCacheId($pageNumber);
self::$cache->setItem($cacheId, $items);
self::$cache->setTags($cacheId, array($this->_getCacheInternalId()));
}
return $items;
}
示例12: testDecrementItemsReturnsEmptyArrayIfNonWritable
public function testDecrementItemsReturnsEmptyArrayIfNonWritable()
{
$this->_storage->setItem('key', 10);
$this->_options->setWritable(false);
$this->assertSame(array(), $this->_storage->decrementItems(array('key' => 5)));
$this->assertEquals(10, $this->_storage->getItem('key'));
}
示例13: getPluginByInstanceId
/**
* Get a plugin by instance Id
*
* @param integer $pluginInstanceId Plugin Instance Id
*
* @return array|mixed
* @throws \Rcm\Exception\PluginInstanceNotFoundException
* @deprecated
*/
public function getPluginByInstanceId($pluginInstanceId)
{
$cacheId = 'rcmPluginInstance_' . $pluginInstanceId;
if ($this->cache->hasItem($cacheId)) {
$return = $this->cache->getItem($cacheId);
$return['fromCache'] = true;
return $return;
}
$pluginInstance = $this->getInstanceEntity($pluginInstanceId);
if (empty($pluginInstance)) {
throw new PluginInstanceNotFoundException('Plugin for instance id ' . $pluginInstanceId . ' not found.');
}
$instanceConfig = $this->getInstanceConfigFromEntity($pluginInstance);
$return = $this->getPluginViewData($pluginInstance->getPlugin(), $pluginInstanceId, $instanceConfig);
if ($pluginInstance->isSiteWide()) {
$return['siteWide'] = true;
$displayName = $pluginInstance->getDisplayName();
if (!empty($displayName)) {
$return['displayName'] = $displayName;
}
}
$return['md5'] = $pluginInstance->getMd5();
if ($return['canCache']) {
$this->cache->setItem($cacheId, $return);
}
return $return;
}
示例14: getItem
/**
* @param $cacheKey
* @param Closure $closure
* @param null $lifetime
* @return mixed
*/
public function getItem($cacheKey, Closure $closure, $lifetime = null)
{
// we have to check if we enable the caching in config
if (!$this->isCachingEnable()) {
return $closure();
}
$data = $this->cachingService->getItem($cacheKey);
if (!$data) {
$data = $closure();
if ($lifetime > 0) {
$this->cachingService->setOptions($this->cachingService->getOptions()->setTtl($lifetime));
}
$this->cachingService->setItem($cacheKey, $data);
}
return $data;
}
示例15: callWidget
/**
* Call widget
*
* @param string $position
* @param integer $pageId
* @param integer $userRole
* @param array $widgetInfo
* @param boolean $useLayout
* @throws \Page\Exception\PageException
* @return string|boolean
*/
protected function callWidget($position, $pageId, $userRole, array $widgetInfo, $useLayout = true)
{
// don't call any widgets
if (true === self::$widgetRedirected) {
return false;
}
// check a widget visibility
if ($userRole != AclBaseModel::DEFAULT_ROLE_ADMIN) {
if (!empty($widgetInfo['hidden']) && in_array($userRole, $widgetInfo['hidden'])) {
return false;
}
}
// call the widget
$widget = $this->getView()->{$widgetInfo['widget_name']}();
// check the widget
if (!$widget instanceof IPageWidget) {
throw new PageException(sprintf($widgetInfo['widget_name'] . ' must be an object implementing IPageWidget'));
}
// init the widget
$widget->setPageId($pageId)->setWidgetPosition($position)->setWidgetConnectionId($widgetInfo['widget_connection_id']);
$widgetCacheName = null;
if ((int) $widgetInfo['widget_cache_ttl']) {
// generate a cache name
$widgetCacheName = CacheUtility::getCacheName($widgetInfo['widget_name'], [$widgetInfo['widget_connection_id']]);
// check the widget data in a cache
if (null !== ($cachedWidgetData = $this->dynamicCache->getItem($widgetCacheName))) {
// check a local widget lifetime
if ($cachedWidgetData['widget_expire'] >= time()) {
// include widget's css and js files
if (false !== $cachedWidgetData['widget_content'] && !$this->request->isXmlHttpRequest()) {
$widget->includeJsCssFiles();
}
return $cachedWidgetData['widget_content'];
}
// clear cache
$this->dynamicCache->removeItem($widgetCacheName);
}
}
if (false !== ($widgetContent = $widget->getContent())) {
self::$widgetRedirected = $widget->isWidgetRedirected();
// include widget's css and js files
if (!$this->request->isXmlHttpRequest()) {
$widget->includeJsCssFiles();
}
// add the widget's layout
if ($useLayout) {
if (!empty($widgetInfo['widget_layout'])) {
$widgetContent = $this->getView()->partial($this->layoutPath . $widgetInfo['widget_layout'], ['title' => $this->getView()->pageWidgetTitle($widgetInfo), 'content' => $widgetContent]);
} else {
$widgetContent = $this->getView()->partial($this->layoutPath . 'default', ['title' => $this->getView()->pageWidgetTitle($widgetInfo), 'content' => $widgetContent]);
}
}
}
// cache the widget data
if ($widgetCacheName) {
$this->dynamicCache->setItem($widgetCacheName, ['widget_content' => $widgetContent, 'widget_expire' => time() + $widgetInfo['widget_cache_ttl']]);
}
return $widgetContent;
}