本文整理汇总了PHP中Drupal\Core\Cache\CacheBackendInterface类的典型用法代码示例。如果您正苦于以下问题:PHP CacheBackendInterface类的具体用法?PHP CacheBackendInterface怎么用?PHP CacheBackendInterface使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CacheBackendInterface类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$this->keyValueFactory = $this->prophesize(KeyValueFactoryInterface::class);
$this->entityTypeManager = $this->prophesize(EntityTypeManagerInterface::class);
$this->entityFieldManager = $this->prophesize(EntityFieldManagerInterface::class);
$this->cacheBackend = $this->prophesize(CacheBackendInterface::class);
$this->fieldDefinitionListener = new FieldDefinitionListener($this->entityTypeManager->reveal(), $this->entityFieldManager->reveal(), $this->keyValueFactory->reveal(), $this->cacheBackend->reveal());
}
示例2: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$this->moduleHandler = $this->prophesize(ModuleHandlerInterface::class);
$this->moduleHandler->getImplementations('entity_type_build')->willReturn([]);
$this->moduleHandler->alter('entity_type', Argument::type('array'))->willReturn(NULL);
$this->cacheBackend = $this->prophesize(CacheBackendInterface::class);
$this->translationManager = $this->prophesize(TranslationInterface::class);
$this->entityTypeManager = new TestEntityTypeManager(new \ArrayObject(), $this->moduleHandler->reveal(), $this->cacheBackend->reveal(), $this->translationManager->reveal(), $this->getClassResolverStub());
$this->discovery = $this->prophesize(DiscoveryInterface::class);
$this->entityTypeManager->setDiscovery($this->discovery->reveal());
}
示例3: 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;
}
示例4: 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];
}
示例5: 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;
}
示例6: testDestruct
/**
* Tests the destruct method.
*
* @covers ::destruct
*/
public function testDestruct()
{
$this->libraryDiscoveryParser->expects($this->once())->method('buildByExtension')->with('test')->will($this->returnValue($this->libraryData));
$lock_key = 'library_info:Drupal\\Core\\Cache\\CacheCollector';
$this->lock->expects($this->once())->method('acquire')->with($lock_key)->will($this->returnValue(TRUE));
$this->cache->expects($this->exactly(2))->method('get')->with('library_info')->will($this->returnValue(FALSE));
$this->cache->expects($this->once())->method('set')->with('library_info', array('test' => $this->libraryData), Cache::PERMANENT, array('library_info'));
$this->lock->expects($this->once())->method('release')->with($lock_key);
// This should get data and persist the key.
$this->libraryDiscoveryCollector->get('test');
$this->libraryDiscoveryCollector->destruct();
}
示例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: 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;
}
示例9: onFieldDefinitionDelete
/**
* {@inheritdoc}
*/
public function onFieldDefinitionDelete(FieldDefinitionInterface $field_definition)
{
$entity_type_id = $field_definition->getTargetEntityTypeId();
$bundle = $field_definition->getTargetBundle();
$field_name = $field_definition->getName();
// Notify the storage about the field deletion.
$this->entityTypeManager->getStorage($entity_type_id)->onFieldDefinitionDelete($field_definition);
// Unset the bundle from the bundle field map key value collection.
$bundle_field_map = $this->keyValueFactory->get('entity.definitions.bundle_field_map')->get($entity_type_id);
unset($bundle_field_map[$field_name]['bundles'][$bundle]);
if (empty($bundle_field_map[$field_name]['bundles'])) {
// If there are no bundles left, remove the field from the map.
unset($bundle_field_map[$field_name]);
}
$this->keyValueFactory->get('entity.definitions.bundle_field_map')->set($entity_type_id, $bundle_field_map);
// Delete the cache entry.
$this->cacheBackend->delete('entity_field_map');
// If the field map is initialized, update it as well, so that calls to it
// do not have to rebuild it again.
if ($field_map = $this->entityFieldManager->getFieldMap()) {
unset($field_map[$entity_type_id][$field_name]['bundles'][$bundle]);
if (empty($field_map[$entity_type_id][$field_name]['bundles'])) {
unset($field_map[$entity_type_id][$field_name]);
}
$this->entityFieldManager->setFieldMap($field_map);
}
}
示例10: testDisable
/**
* @covers ::disable
* @depends testSetStatus
*/
public function testDisable()
{
$this->cacheBackend->expects($this->once())->method('invalidateTags')->with(array($this->entityTypeId . ':' . $this->id));
$this->entity->setStatus(TRUE);
$this->assertSame($this->entity, $this->entity->disable());
$this->assertFalse($this->entity->status());
}
示例11: testGetInfo
/**
* Tests the getInfo method.
*
* @covers ::getInfo
* @covers ::buildInfo
*
* @dataProvider providerTestGetInfo
*/
public function testGetInfo($type, $expected_info, $element_info, callable $alter_callback = NULL)
{
$this->moduleHandler->expects($this->once())->method('invokeAll')->with('element_info')->will($this->returnValue($element_info));
$this->moduleHandler->expects($this->once())->method('alter')->with('element_info', $this->anything())->will($this->returnCallback($alter_callback ?: function ($info) {
return $info;
}));
$this->themeManager->expects($this->once())->method('getActiveTheme')->willReturn(new ActiveTheme(['name' => 'test']));
$this->themeManager->expects($this->once())->method('alter')->with('element_info', $this->anything())->will($this->returnCallback($alter_callback ?: function ($info) {
return $info;
}));
$this->cache->expects($this->at(0))->method('get')->with('element_info_build:test')->will($this->returnValue(FALSE));
$this->cache->expects($this->at(1))->method('get')->with('element_info')->will($this->returnValue(FALSE));
$this->cache->expects($this->at(2))->method('set')->with('element_info');
$this->cache->expects($this->at(3))->method('set')->with('element_info_build:test');
$this->assertEquals($expected_info, $this->elementInfo->getInfo($type));
}
示例12: 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.');
}
示例13: onRequestSent
/**
* Responds after a request has finished, but before it is sent to the client.
*
* @param \Guzzle\Common\Event $event
* The Guzzle event object.
*/
public function onRequestSent(Event $event)
{
$request = $event['request'];
$response = $event['response'];
// Handle permanent redirects by setting the redirected URL so that the
// client can grab it quickly.
$redirect = FALSE;
$url = $old_url = $request->getUrl();
if ($previous_response = $response->getPreviousResponse()) {
if ($previous_response->getStatusCode() == 301 && ($location = $previous_response->getLocation())) {
$response->getParams()->set('feeds.redirect', $location);
$redirect = TRUE;
$url = $request->getUrl();
}
}
$cache_hit = $response->getStatusCode() == 304;
if ($redirect) {
// Delete the old cache entry.
$this->cacheBackend->delete($this->getCacheKey($old_url));
// Not sure if the repeated requests are smart enough to find the
// redirect, so cache the old URL with the new response.
static::$downloadCache[$old_url] = $response;
}
if ($redirect || !$cache_hit) {
$cache = new \stdClass();
$cache->headers = array_change_key_case($response->getHeaders()->toArray());
// @todo We should only cache for certain status codes.
$cache->code = $response->getStatusCode();
$this->cacheBackend->set($this->getCacheKey($url), $cache);
}
// Set in-page download cache.
static::$downloadCache[$url] = $response;
}
示例14: setCachedDefinitions
/**
* Sets a cache of plugin definitions for the decorated discovery class.
*
* @param array $definitions
* List of definitions to store in cache.
*/
protected function setCachedDefinitions($definitions)
{
if ($this->cacheBackend) {
$this->cacheBackend->set($this->cacheKey, $definitions, Cache::PERMANENT, $this->cacheTags);
}
$this->definitions = $definitions;
}
示例15: testGetAliasByPathUncachedMissWithAlias
/**
* Tests the getAliasByPath cache with an unpreloaded path with alias.
*
* @covers ::getAliasByPath
* @covers ::writeCache
*/
public function testGetAliasByPathUncachedMissWithAlias()
{
$path_part1 = $this->randomMachineName();
$path_part2 = $this->randomMachineName();
$path = '/' . $path_part1 . '/' . $path_part2;
$cached_path = $this->randomMachineName();
$cached_no_alias_path = $this->randomMachineName();
$cached_alias = $this->randomMachineName();
$new_alias = $this->randomMachineName();
$language = $this->setUpCurrentLanguage();
$cached_paths = array($language->getId() => array($cached_path, $cached_no_alias_path));
$this->cache->expects($this->once())->method('get')->with($this->cacheKey)->will($this->returnValue((object) array('data' => $cached_paths)));
// Simulate a request so that the preloaded paths are fetched.
$this->aliasManager->setCacheKey($this->path);
$this->aliasWhitelist->expects($this->any())->method('get')->with($path_part1)->will($this->returnValue(TRUE));
$this->aliasStorage->expects($this->once())->method('preloadPathAlias')->with($cached_paths[$language->getId()], $language->getId())->will($this->returnValue(array($cached_path => $cached_alias)));
$this->aliasStorage->expects($this->once())->method('lookupPathAlias')->with($path, $language->getId())->will($this->returnValue($new_alias));
$this->assertEquals($new_alias, $this->aliasManager->getAliasByPath($path));
// Call it twice to test the static cache.
$this->assertEquals($new_alias, $this->aliasManager->getAliasByPath($path));
// There is already a cache entry, so this should not write out to the
// cache.
$this->cache->expects($this->never())->method('set');
$this->aliasManager->writeCache();
}