本文整理汇总了PHP中Drupal\Core\Cache\CacheBackendInterface::get方法的典型用法代码示例。如果您正苦于以下问题:PHP CacheBackendInterface::get方法的具体用法?PHP CacheBackendInterface::get怎么用?PHP CacheBackendInterface::get使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Core\Cache\CacheBackendInterface
的用法示例。
在下文中一共展示了CacheBackendInterface::get方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: onRequestBeforeSend
/**
* Intervenes before a request starts to add cache headers.
*
* @param \Guzzle\Common\Event $event
* The Guzzle event object.
*/
public function onRequestBeforeSend(Event $event)
{
$request = $event['request'];
// We're only handling GET requests for now. That's all we do anyway.
if ($request->getMethod() != RequestInterface::GET) {
return;
}
$url = $request->getUrl();
// In-memory download cache. Sometimes we fetch the same URL more than
// once in a page load.
// @todo Be smarter.
if (isset(static::$downloadCache[$url])) {
$request->setResponse(static::$downloadCache[$url]);
return;
}
if ($cache = $this->cacheBackend->get($this->getCacheKey($url))) {
// Add any headers that could be useful.
// @todo Look at Guzzle's own cache plugin, or add a smarter cache here.
if (!empty($cache->data->headers['etag'])) {
$request->addHeader('If-None-Match', $cache->data->headers['etag']);
}
if (!empty($cache->data->headers['last-modified'])) {
$request->addHeader('If-Modified-Since', $cache->data->headers['last-modified']);
}
}
}
示例2: user
/**
* User object.
*
* @return \Drupal\moodle\Sql\User
*/
public function user()
{
// Static cache of already retrieved user data.
$data =& drupal_static(__METHOD__, array());
$user_cid = "moodle-user:{$this->user->id()}";
// If we do not have this user id in the static cache, check {cache_data}.
if (!isset($data[$user_cid])) {
$cache = $this->cacheBackend->get($user_cid);
if ($cache && $cache->data && isset($cache->data[$user_cid])) {
$data[$user_cid] = $cache->data[$user_cid];
}
}
// If nothing in the cache then retrieve it from the database.
if (!isset($data[$user_cid])) {
$user = new User();
$this->query();
$this->addFields();
$statement = $this->query->execute();
$statement->setFetchMode(\PDO::FETCH_INTO, $user);
$data[$user_cid] = $statement->fetch();
// Store the results for a day.
$this->cacheBackend->set($user_cid, $data, REQUEST_TIME + 86400);
}
return $data[$user_cid];
}
示例3: isFresh
/**
* Checks if the compiled template needs an update.
*/
protected function isFresh($cache_filename, $name)
{
$cid = 'twig:' . $cache_filename;
$obj = $this->cache_object->get($cid);
$mtime = isset($obj->data) ? $obj->data : FALSE;
return $mtime === FALSE || $this->isTemplateFresh($name, $mtime);
}
示例4: generate
/**
* {@inheritdoc}
*
* Cached by role, invalidated whenever permissions change.
*/
public function generate(AccountInterface $account)
{
// User 1 is the super user, and can always access all permissions. Use a
// different, unique identifier for the hash.
if ($account->id() == 1) {
return $this->hash('is-super-user');
}
$sorted_roles = $account->getRoles();
sort($sorted_roles);
$role_list = implode(',', $sorted_roles);
$cid = "user_permissions_hash:{$role_list}";
if ($static_cache = $this->static->get($cid)) {
return $static_cache->data;
} else {
$tags = Cache::buildTags('config:user.role', $sorted_roles, '.');
if ($cache = $this->cache->get($cid)) {
$permissions_hash = $cache->data;
} else {
$permissions_hash = $this->doGenerate($sorted_roles);
$this->cache->set($cid, $permissions_hash, Cache::PERMANENT, $tags);
}
$this->static->set($cid, $permissions_hash, Cache::PERMANENT, $tags);
}
return $permissions_hash;
}
示例5: cacheGet
/**
* Fetches from the cache backend, respecting the use caches flag.
*
* @param string $cid
* The cache ID of the data to retrieve.
*
* @return object|false
* The cache item or FALSE on failure.
*
* @see \Drupal\Core\Cache\CacheBackendInterface::get()
*/
protected function cacheGet($cid)
{
if ($this->useCaches && $this->cacheBackend) {
return $this->cacheBackend->get($cid);
}
return FALSE;
}
示例6: buildForm
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state)
{
// Log execution time.
$start_time = microtime(TRUE);
// Try to load the files count from cache. This function will accept two
// arguments:
// - cache object name (cid)
// - cache bin, the (optional) cache bin (most often a database table) where
// the object is to be saved.
//
// cache_get() returns the cached object or FALSE if object does not exist.
if ($cache = $this->cacheBackend->get('cache_example_files_count')) {
/*
* Get cached data. Complex data types will be unserialized automatically.
*/
$files_count = $cache->data;
} else {
// If there was no cached data available we have to search filesystem.
// Recursively get all files from Drupal's folder.
$files_count = count(file_scan_directory('.', '/.*/'));
// Since we have recalculated, we now need to store the new data into
// cache. Complex data types will be automatically serialized before
// being saved into cache.
// Here we use the default setting and create an unexpiring cache item.
// See below for an example that creates an expiring cache item.
$this->cacheBackend->set('cache_example_files_count', $files_count, CacheBackendInterface::CACHE_PERMANENT);
}
$end_time = microtime(TRUE);
$duration = $end_time - $start_time;
// Format intro message.
$intro_message = '<p>' . $this->t('This example will search the entire drupal folder and display a count of the files in it.') . ' ';
$intro_message .= $this->t('This can take a while, since there are a lot of files to be searched.') . ' ';
$intro_message .= $this->t('We will search filesystem just once and save output to the cache. We will use cached data for later requests.') . '</p>';
$intro_message .= '<p>' . $this->t('<a href="@url">Reload this page</a> to see cache in action.', array('@url' => $this->getRequest()->getRequestUri())) . ' ';
$intro_message .= $this->t('You can use the button below to remove cached data.') . '</p>';
$form['file_search'] = array('#type' => 'fieldset', '#title' => $this->t('File search caching'));
$form['file_search']['introduction'] = array('#markup' => $intro_message);
$color = empty($cache) ? 'red' : 'green';
$retrieval = empty($cache) ? $this->t('calculated by traversing the filesystem') : $this->t('retrieved from cache');
$form['file_search']['statistics'] = array('#type' => 'item', '#markup' => $this->t('%count files exist in this Drupal installation; @retrieval in @time ms. <br/>(Source: <span style="color:@color;">@source</span>)', array('%count' => $files_count, '@retrieval' => $retrieval, '@time' => number_format($duration * 1000, 2), '@color' => $color, '@source' => empty($cache) ? $this->t('actual file search') : $this->t('cached'))));
$form['file_search']['remove_file_count'] = array('#type' => 'submit', '#submit' => array(array($this, 'expireFiles')), '#value' => $this->t('Explicitly remove cached file count'));
$form['expiration_demo'] = array('#type' => 'fieldset', '#title' => $this->t('Cache expiration settings'));
$form['expiration_demo']['explanation'] = array('#markup' => $this->t('A cache item can be set as CACHE_PERMANENT, meaning that it will only be removed when explicitly cleared, or it can have an expiration time (a Unix timestamp).'));
$item = $this->cacheBackend->get('cache_example_expiring_item', TRUE);
if ($item == FALSE) {
$item_status = $this->t('Cache item does not exist');
} else {
$item_status = $item->valid ? $this->t('Cache item exists and is set to expire at %time', array('%time' => $item->data)) : $this->t('Cache_item is invalid');
}
$form['expiration_demo']['current_status'] = array('#type' => 'item', '#title' => $this->t('Current status of cache item "cache_example_expiring_item"'), '#markup' => $item_status);
$form['expiration_demo']['expiration'] = array('#type' => 'select', '#title' => $this->t('Time before cache expiration'), '#options' => array('never_remove' => $this->t('CACHE_PERMANENT'), -10 => $this->t('Immediate expiration'), 10 => $this->t('10 seconds from form submission'), 60 => $this->t('1 minute from form submission'), 300 => $this->t('5 minutes from form submission')), '#default_value' => -10, '#description' => $this->t('Any cache item can be set to only expire when explicitly cleared, or to expire at a given time.'));
$form['expiration_demo']['create_cache_item'] = array('#type' => 'submit', '#value' => $this->t('Create a cache item with this expiration'), '#submit' => array(array($this, 'createExpiringItem')));
$form['cache_clearing'] = array('#type' => 'fieldset', '#title' => $this->t('Expire and remove options'), '#description' => $this->t("We have APIs to expire cached items and also to just remove them. Unfortunately, they're all the same API, cache_clear_all"));
$form['cache_clearing']['cache_clear_type'] = array('#type' => 'radios', '#title' => $this->t('Type of cache clearing to do'), '#options' => array('expire' => $this->t('Remove items from the "cache" bin that have expired'), 'remove_all' => $this->t('Remove all items from the "cache" bin regardless of expiration'), 'remove_tag' => $this->t('Remove all items in the "cache" bin with the tag "cache_example" set to 1')), '#default_value' => 'expire');
// Submit button to clear cached data.
$form['cache_clearing']['clear_expired'] = array('#type' => 'submit', '#value' => $this->t('Clear or expire cache'), '#submit' => array(array($this, 'cacheClearing')), '#access' => $this->currentUser->hasPermission('administer site configuration'));
return $form;
}
示例7: generate
/**
* {@inheritdoc}
*
* Cached by role, invalidated whenever permissions change.
*/
public function generate(AccountInterface $account)
{
$sorted_roles = $account->getRoles();
sort($sorted_roles);
$role_list = implode(',', $sorted_roles);
if ($cache = $this->cache->get("user_permissions_hash:{$role_list}")) {
$permissions_hash = $cache->data;
} else {
$permissions_hash = $this->doGenerate($sorted_roles);
$this->cache->set("user_permissions_hash:{$role_list}", $permissions_hash, Cache::PERMANENT, array('user_role' => $sorted_roles));
}
return $permissions_hash;
}
示例8: loadBaseDefinitions
/**
* Loads the base country definitions.
*
* @return array
*/
protected function loadBaseDefinitions()
{
if (!empty($this->baseDefinitions)) {
return $this->baseDefinitions;
}
$cache_key = 'address.countries.base';
if ($cached = $this->cache->get($cache_key)) {
$this->baseDefinitions = $cached->data;
} else {
$this->baseDefinitions = json_decode(file_get_contents($this->definitionPath . 'base.json'), TRUE);
$this->cache->set($cache_key, $this->baseDefinitions, CacheBackendInterface::CACHE_PERMANENT, ['countries']);
}
return $this->baseDefinitions;
}
示例9: get
/**
* {@inheritdoc}
*/
public function get($cid, $allow_invalid = FALSE)
{
$cache = $this->cacheBackend->get($cid, $allow_invalid);
if ($cache) {
$cacheCopy = new \StdClass();
$cacheCopy->cid = $cache->cid;
$cacheCopy->expire = $cache->expire;
$cacheCopy->tags = $cache->tags;
$this->cacheDataCollector->registerCacheHit($this->bin, $cacheCopy);
} else {
$this->cacheDataCollector->registerCacheMiss($this->bin, $cid);
}
return $cache;
}
示例10: generate
/**
* {@inheritdoc}
*
* Cached by role, invalidated whenever permissions change.
*/
public function generate(AccountInterface $account)
{
$sorted_roles = $account->getRoles();
sort($sorted_roles);
$role_list = implode(',', $sorted_roles);
if ($cache = $this->cache->get("user_permissions_hash:{$role_list}")) {
$permissions_hash = $cache->data;
} else {
$permissions_hash = $this->doGenerate($sorted_roles);
$tags = Cache::buildTags('config:user.role', $sorted_roles, '.');
$this->cache->set("user_permissions_hash:{$role_list}", $permissions_hash, Cache::PERMANENT, $tags);
}
return $permissions_hash;
}
示例11: read
/**
* {@inheritdoc}
*/
public function read($name)
{
$cache_key = $this->getCacheKey($name);
if ($cache = $this->cache->get($cache_key)) {
// The cache contains either the cached configuration data or FALSE
// if the configuration file does not exist.
return $cache->data;
}
// Read from the storage on a cache miss and cache the data. Also cache
// information about missing configuration objects.
$data = $this->storage->read($name);
$this->cache->set($cache_key, $data);
return $data;
}
示例12: cacheGet
/**
* Gets data from the cache backend.
*
* @param string $cid
* The cache ID to return.
*
* @return mixed
* The cached data, if any. This will immediately return FALSE if the
* $skipCache property is TRUE.
*/
protected function cacheGet($cid)
{
if ($this->skipCache) {
return FALSE;
}
return $this->cacheBackend->get($this->prepareCid($cid));
}
示例13: generateFormatTagsSetting
/**
* Builds the "format_tags" configuration part of the CKEditor JS settings.
*
* @see getConfig()
*
* @param \Drupal\editor\Entity\Editor $editor
* A configured text editor object.
*
* @return array
* An array containing the "format_tags" configuration.
*/
protected function generateFormatTagsSetting(Editor $editor)
{
// When no text format is associated yet, assume no tag is allowed.
// @see \Drupal\Editor\EditorInterface::hasAssociatedFilterFormat()
if (!$editor->hasAssociatedFilterFormat()) {
return array();
}
$format = $editor->getFilterFormat();
$cid = 'ckeditor_internal_format_tags:' . $format->id();
if ($cached = $this->cache->get($cid)) {
$format_tags = $cached->data;
} else {
// The <p> tag is always allowed — HTML without <p> tags is nonsensical.
$format_tags = ['p'];
// Given the list of possible format tags, automatically determine whether
// the current text format allows this tag, and thus whether it should show
// up in the "Format" dropdown.
$possible_format_tags = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'pre'];
foreach ($possible_format_tags as $tag) {
$input = '<' . $tag . '>TEST</' . $tag . '>';
$output = trim(check_markup($input, $editor->id()));
if ($input == $output) {
$format_tags[] = $tag;
}
}
$format_tags = implode(';', $format_tags);
// Cache the "format_tags" configuration. This cache item is infinitely
// valid; it only changes whenever the text format is changed, hence it's
// tagged with the text format's cache tag.
$this->cache->set($cid, $format_tags, Cache::PERMANENT, $format->getCacheTags());
}
return $format_tags;
}
示例14: loadTreeData
/**
* {@inheritdoc}
*/
public function loadTreeData($menu_name, MenuTreeParameters $parameters)
{
// Build the cache ID; sort 'expanded' and 'conditions' to prevent duplicate
// cache items.
sort($parameters->expandedParents);
asort($parameters->conditions);
$tree_cid = "tree-data:{$menu_name}:" . serialize($parameters);
$cache = $this->menuCacheBackend->get($tree_cid);
if ($cache && isset($cache->data)) {
$data = $cache->data;
// Cache the definitions in memory so they don't need to be loaded again.
$this->definitions += $data['definitions'];
unset($data['definitions']);
} else {
$links = $this->loadLinks($menu_name, $parameters);
$data['tree'] = $this->doBuildTreeData($links, $parameters->activeTrail, $parameters->minDepth);
$data['definitions'] = array();
$data['route_names'] = $this->collectRoutesAndDefinitions($data['tree'], $data['definitions']);
$this->menuCacheBackend->set($tree_cid, $data, Cache::PERMANENT, ['config:system.menu.' . $menu_name]);
// The definitions were already added to $this->definitions in
// $this->doBuildTreeData()
unset($data['definitions']);
}
return $data;
}
示例15: testDeleteTagsPropagation
/**
* Test that the delete tags operation is propagated to all backends
* in the chain.
*/
public function testDeleteTagsPropagation()
{
// Create two cache entries with the same tag and tag value.
$this->chain->set('test_cid_clear1', 'foo', Cache::PERMANENT, array('test_tag:2'));
$this->chain->set('test_cid_clear2', 'foo', Cache::PERMANENT, array('test_tag:2'));
$this->assertNotSame(FALSE, $this->firstBackend->get('test_cid_clear1') && $this->firstBackend->get('test_cid_clear2') && $this->secondBackend->get('test_cid_clear1') && $this->secondBackend->get('test_cid_clear2') && $this->thirdBackend->get('test_cid_clear1') && $this->thirdBackend->get('test_cid_clear2'), 'Two cache items were created in all backends.');
// Invalidate test_tag of value 1. This should invalidate both entries.
$this->chain->invalidateTags(array('test_tag:2'));
$this->assertSame(FALSE, $this->firstBackend->get('test_cid_clear1') && $this->firstBackend->get('test_cid_clear2') && $this->secondBackend->get('test_cid_clear1') && $this->secondBackend->get('test_cid_clear2') && $this->thirdBackend->get('test_cid_clear1') && $this->thirdBackend->get('test_cid_clear2'), 'Two caches removed from all backends after clearing a cache tag.');
// Create two cache entries with the same tag and an array tag value.
$this->chain->set('test_cid_clear1', 'foo', Cache::PERMANENT, array('test_tag:1'));
$this->chain->set('test_cid_clear2', 'foo', Cache::PERMANENT, array('test_tag:1'));
$this->assertNotSame(FALSE, $this->firstBackend->get('test_cid_clear1') && $this->firstBackend->get('test_cid_clear2') && $this->secondBackend->get('test_cid_clear1') && $this->secondBackend->get('test_cid_clear2') && $this->thirdBackend->get('test_cid_clear1') && $this->thirdBackend->get('test_cid_clear2'), 'Two cache items were created in all backends.');
// Invalidate test_tag of value 1. This should invalidate both entries.
$this->chain->invalidateTags(array('test_tag:1'));
$this->assertSame(FALSE, $this->firstBackend->get('test_cid_clear1') && $this->firstBackend->get('test_cid_clear2') && $this->secondBackend->get('test_cid_clear1') && $this->secondBackend->get('test_cid_clear2') && $this->thirdBackend->get('test_cid_clear1') && $this->thirdBackend->get('test_cid_clear2'), 'Two caches removed from all backends after clearing a cache tag.');
// Create three cache entries with a mix of tags and tag values.
$this->chain->set('test_cid_clear1', 'foo', Cache::PERMANENT, array('test_tag:1'));
$this->chain->set('test_cid_clear2', 'foo', Cache::PERMANENT, array('test_tag:2'));
$this->chain->set('test_cid_clear3', 'foo', Cache::PERMANENT, array('test_tag_foo:3'));
$this->assertNotSame(FALSE, $this->firstBackend->get('test_cid_clear1') && $this->firstBackend->get('test_cid_clear2') && $this->firstBackend->get('test_cid_clear3') && $this->secondBackend->get('test_cid_clear1') && $this->secondBackend->get('test_cid_clear2') && $this->secondBackend->get('test_cid_clear3') && $this->thirdBackend->get('test_cid_clear1') && $this->thirdBackend->get('test_cid_clear2') && $this->thirdBackend->get('test_cid_clear3'), 'Three cached items were created in all backends.');
$this->chain->invalidateTags(array('test_tag_foo:3'));
$this->assertNotSame(FALSE, $this->firstBackend->get('test_cid_clear1') && $this->firstBackend->get('test_cid_clear2') && $this->secondBackend->get('test_cid_clear1') && $this->secondBackend->get('test_cid_clear2') && $this->thirdBackend->get('test_cid_clear1') && $this->thirdBackend->get('test_cid_clear2'), 'Cached items not matching the tag were not cleared from any of the backends.');
$this->assertSame(FALSE, $this->firstBackend->get('test_cid_clear3') && $this->secondBackend->get('test_cid_clear3') && $this->thirdBackend->get('test_cid_clear3'), 'Cached item matching the tag was removed from all backends.');
}