You are here

final class DeveloperCache in Apigee Edge 8

Default developer cache implementation.

Generates additional cache entries for developers by using their email addresses as cache ids. This way developers can be served from cache both by their developer id (UUID) an email address.

Hierarchy

Expanded class hierarchy of DeveloperCache

1 string reference to 'DeveloperCache'
apigee_edge.services.yml in ./apigee_edge.services.yml
apigee_edge.services.yml
1 service uses DeveloperCache
apigee_edge.controller.cache.developer in ./apigee_edge.services.yml
Drupal\apigee_edge\Entity\Controller\Cache\DeveloperCache

File

src/Entity/Controller/Cache/DeveloperCache.php, line 33

Namespace

Drupal\apigee_edge\Entity\Controller\Cache
View source
final class DeveloperCache extends EntityCache implements EntityCacheInterface {

  /**
   * An internal cache for developer ids and emails.
   *
   * Associative array, keys are developer ids and values are email addresses.
   *
   * @var array
   */
  private $developerIdEmailMap = [];

  /**
   * DeveloperCache constructor.
   *
   * @param \Drupal\apigee_edge\MemoryCacheFactoryInterface $memory_cache_factory
   *   The memory cache factory service.
   * @param \Drupal\apigee_edge\Entity\Controller\Cache\EntityIdCacheInterface $entity_id_cache
   *   The developer entity id cache.
   */
  public function __construct(MemoryCacheFactoryInterface $memory_cache_factory, EntityIdCacheInterface $entity_id_cache) {
    parent::__construct($memory_cache_factory, $entity_id_cache, 'developer');
  }

  /**
   * {@inheritdoc}
   */
  protected function prepareCacheItem(EntityInterface $entity) : array {

    /** @var \Apigee\Edge\Api\Management\Entity\DeveloperInterface $entity */
    $item = parent::prepareCacheItem($entity);

    // Add developer's email as tag to generated cache items by the parent
    // class.
    foreach ($item as $cid => $developer) {
      $item[$cid]['tags'][] = $entity
        ->getEmail();
    }
    $this->developerIdEmailMap[$entity
      ->getDeveloperId()] = $entity
      ->getEmail();
    return $item;
  }

  /**
   * {@inheritdoc}
   */
  protected function doSaveEntities(array $entities) : void {
    parent::doSaveEntities($entities);
    $items = [];
    foreach ($entities as $entity) {

      // Add new cache item that uses developer's email address as a cid instead
      // of developer's id (UUID).
      $items[$entity
        ->getEmail()] = [
        'data' => $entity,
        'tags' => [
          $entity
            ->getDeveloperId(),
          $entity
            ->getEmail(),
        ],
      ];
    }
    $this->cacheBackend
      ->setMultiple($items);
  }

  /**
   * {@inheritdoc}
   */
  protected function doRemoveEntities(array $ids) : void {
    if (!empty($ids)) {

      // If ids are developer ids (UUIDs).
      $dev_id_email_address_matches = array_intersect_key($this->developerIdEmailMap, array_flip($ids));

      // If ids are email addresses.
      $dev_id_email_address_matches += array_flip(array_intersect_key(array_flip($this->developerIdEmailMap), array_flip($ids)));

      // Because all cached entries tagged with email address and developer
      // id (UUID) this should invalidate everything that is needed in this
      // cache...
      $this->cacheBackend
        ->invalidateTags($ids);

      // ... although if $ids are developer ids (UUIDs) then the entity id cache
      // does not get invalidated properly in parent.
      $this->entityIdCache
        ->removeIds($dev_id_email_address_matches);

      // Remove removed items from the internal cache.
      $this->developerIdEmailMap = array_diff_key($this->developerIdEmailMap, $dev_id_email_address_matches);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DeveloperCache::$developerIdEmailMap private property An internal cache for developer ids and emails.
DeveloperCache::doRemoveEntities protected function Allows to perform additional tasks after entities got deleted from cache. Overrides EntityCache::doRemoveEntities
DeveloperCache::doSaveEntities protected function Allows to perform additional tasks after entities got saved to cache. Overrides EntityCache::doSaveEntities
DeveloperCache::prepareCacheItem protected function Generates cache items for an entity. Overrides EntityCache::prepareCacheItem
DeveloperCache::__construct public function DeveloperCache constructor. Overrides EntityCache::__construct
EntityCache::$allEntitiesInCache private property Indicates whether all entities in the cache or not.
EntityCache::$cacheBackend protected property The memory cache backend used by this cache.
EntityCache::$cacheIds private property Array of entity ids stored in the cache.
EntityCache::$entityIdCache protected property The entity id cache related to this entity type.
EntityCache::allEntitiesInCache final public function Changes whether all entities in the cache or not. Overrides EntityCacheInterface::allEntitiesInCache
EntityCache::getEntities final public function Returns entities from the cache. Overrides EntityCacheInterface::getEntities
EntityCache::getEntity final public function Returns an entity from the cache by its id. Overrides EntityCacheInterface::getEntity
EntityCache::isAllEntitiesInCache final public function Returns whether all entities in cache or not. Overrides EntityCacheInterface::isAllEntitiesInCache
EntityCache::removeEntities final public function Removes entities from the cache by their ids. Overrides EntityCacheInterface::removeEntities
EntityCache::saveEntities final public function Saves entities to the cache. Overrides EntityCacheInterface::saveEntities
EntityCache::__sleep public function Prevents data stored in entity cache from being serialized.