本文整理汇总了PHP中Zend\Cache\Storage\StorageInterface::setItem方法的典型用法代码示例。如果您正苦于以下问题:PHP StorageInterface::setItem方法的具体用法?PHP StorageInterface::setItem怎么用?PHP StorageInterface::setItem使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Zend\Cache\Storage\StorageInterface
的用法示例。
在下文中一共展示了StorageInterface::setItem方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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;
}
示例2: setValue
/**
* Sets or removes value from cache
*
* @param string $name
* @param string $value
* @return void
*/
public function setValue($name, $value = null)
{
if ($value === null) {
$this->container->removeItem($name);
} else {
$this->container->setItem($name, $value);
}
}
示例3: set
/**
* @param mixed $key
* @param mixed $data
* @return bool
*/
public function set($key, $data)
{
if (!$this->isKey($key)) {
$key = $this->createKey($key);
}
$data = serialize($data);
return $this->cache->setItem($key, $data);
}
示例4: 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;
}
示例5: 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;
}
示例6: setDelayedItem
public function setDelayedItem($key, Closure $closure)
{
$this->storage->setItem($this->getDelayedKey($key), self::UNDER_CONSTRUCTION_VALUE);
$setReturn = $this->storage->setItem($key, $closure());
$this->storage->removeItem($this->getDelayedKey($key));
return $setReturn;
}
示例7: 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;
}
示例8: 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;
}
示例9: 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;
}
示例10: save
/**
* Persists a cache item immediately.
*
* @param CacheItemInterface $item
* The cache item to save.
*
* @return bool
* True if the item was successfully persisted. False if there was an error.
*/
public function save(CacheItemInterface $item)
{
if (!$item instanceof CacheItem) {
throw new InvalidArgumentException('$item must be an instance of ' . CacheItem::class);
}
$this->validateKey($item->getKey());
try {
$options = false;
$expiration = $item->getExpiration();
// @todo I can't see any way to set the TTL on an individual item except by temporarily overwriting the
// option on the storage adapter. Not sure if all storage adapters will support this...
if ($expiration instanceof DateTime) {
$options = $this->storage->getOptions();
$new = clone $options;
$interval = $expiration->diff(new DateTime(), true);
$new->setTtl($interval->format('%s'));
$this->storage->setOptions($new);
}
$saved = $this->storage->setItem($item->getKey(), $item->get());
if ($options) {
$this->storage->setOptions($options);
}
} 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 $saved;
}
示例11: 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'));
}
示例12: sendAction
/**
* @return string
*/
public function sendAction()
{
$queueSize = $this->options->getQueueSize();
$throttleSize = $this->mailThrottle->getThrottleSize();
if ($queueSize < 1 || 0 === $throttleSize) {
$this->logger->err('Can not send anything.');
return '';
}
$currentQueueSize = $throttleSize > 0 ? min($queueSize, $throttleSize) : $queueSize;
$readyQueue = $this->queueItemRepository->getReadyQueue($currentQueueSize);
$this->processQueueToSend($readyQueue);
if ($this->hasCacheStorage()) {
$this->cacheStorage->setItem(\DmMailer\View\DataProvider\Kpi::CS_LAST_MESSAGE_SENDING, $this->getTime());
}
return '';
}
示例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: 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;
}
示例15: resolve
/**
* Retrieve the filesystem path to a view script
*
* @param string $name
* @param null|Renderer $renderer
* @throws \Zend\View\Exception\DomainException
* @return string
*/
public function resolve($name, Renderer $renderer = null)
{
if (!self::$currentLayoutId) {
$activeLayouts = LayoutService::getCurrentLayouts();
self::$currentLayoutId = end($activeLayouts)['name'];
}
// generate a cache name
$cacheName = CacheUtility::getCacheName(self::CACHE_TEMPLATE_PATH, [$name, $renderer, self::$currentLayoutId]);
// check data in cache
if (null === ($templatePath = $this->dynamicCacheInstance->getItem($cacheName))) {
if (false !== ($templatePath = parent::resolve($name, $renderer))) {
// save data in cache
$this->dynamicCacheInstance->setItem($cacheName, $templatePath);
}
}
return $templatePath;
}