You are here

final class CompanyMembershipObjectCache in Apigee Edge 8

Providers a persistent & non-persistent cache for company membership objects.

@internal You should use the team membership manager service instead of this.

Hierarchy

Expanded class hierarchy of CompanyMembershipObjectCache

1 string reference to 'CompanyMembershipObjectCache'
apigee_edge_teams.services.yml in modules/apigee_edge_teams/apigee_edge_teams.services.yml
modules/apigee_edge_teams/apigee_edge_teams.services.yml
1 service uses CompanyMembershipObjectCache
apigee_edge_teams.cache.company_membership_object in modules/apigee_edge_teams/apigee_edge_teams.services.yml
Drupal\apigee_edge_teams\CompanyMembershipObjectCache

File

modules/apigee_edge_teams/src/CompanyMembershipObjectCache.php, line 36

Namespace

Drupal\apigee_edge_teams
View source
final class CompanyMembershipObjectCache implements CompanyMembershipObjectCacheInterface {

  /**
   * The default persistent cache bin used by this service.
   *
   * @var string
   */
  public const DEFAULT_CACHE_BIN = 'apigee_edge_teams_company_membership_object';

  /**
   * Persistent cache backend.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  private $persistentCacheBackend;

  /**
   * Number of seconds until an entity can be served from cache.
   *
   * -1 is also an allowed, which means the item should never be removed unless
   * explicitly deleted.
   *
   * @var int
   */
  private $persistentCacheExpiration = CacheBackendInterface::CACHE_PERMANENT;

  /**
   * Non-persistent cache backend.
   *
   * @var \Drupal\Core\Cache\MemoryCache\MemoryCacheInterface
   */
  private $memoryCache;

  /**
   * The system time service.
   *
   * @var \Drupal\Component\Datetime\TimeInterface
   */
  private $systemTime;

  /**
   * CompanyMembershipObjectCache constructor.
   *
   * @param \Drupal\Core\Cache\CacheFactoryInterface $cache_factory
   *   The cache factory.
   * @param \Drupal\apigee_edge\MemoryCacheFactoryInterface $memory_cache_factory
   *   The module specific memory cache factory.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config
   *   The config factory.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The system time service.
   */
  public function __construct(CacheFactoryInterface $cache_factory, MemoryCacheFactoryInterface $memory_cache_factory, ConfigFactoryInterface $config, TimeInterface $time) {
    $this->persistentCacheBackend = $cache_factory
      ->get(self::DEFAULT_CACHE_BIN);

    // TODO Should we introduce dedicated cache expiration configuration for
    // this?
    $this->persistentCacheExpiration = $config
      ->get('apigee_edge_teams.team_settings')
      ->get('cache_expiration');
    $this->memoryCache = $memory_cache_factory
      ->get('company_membership_object');
    $this->systemTime = $time;
  }

  /**
   * {@inheritdoc}
   */
  public function saveMembership(string $company, CompanyMembership $membership) : void {
    $expiration = $this->persistentCacheExpiration;

    // Do not save in cache, if expiration is set to 0.
    if (0 === $expiration) {
      return;
    }

    // Tag company membership cache entries with members' (developers') email
    // addresses for easier cache invalidation when a developer gets removed.
    $tags = [];
    foreach (array_keys($membership
      ->getMembers()) as $developer_email) {
      $tags[] = "developer:{$developer_email}";
    }
    $this->memoryCache
      ->set($company, $membership, CacheBackendInterface::CACHE_PERMANENT, $tags);
    if ($expiration !== CacheBackendInterface::CACHE_PERMANENT) {
      $expiration = $this->systemTime
        ->getCurrentTime() + $expiration;
    }
    $this->persistentCacheBackend
      ->set($company, $membership, $expiration, $tags);
  }

  /**
   * {@inheritdoc}
   */
  public function removeMembership(string $company) : void {
    $this->memoryCache
      ->invalidate($company);
    $this->persistentCacheBackend
      ->invalidate($company);
  }

  /**
   * {@inheritdoc}
   */
  public function invalidateMemberships(array $tags) : void {
    $this->memoryCache
      ->invalidateTags($tags);
    if ($this->persistentCacheBackend instanceof CacheTagsInvalidatorInterface) {
      $this->persistentCacheBackend
        ->invalidateTags($tags);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getMembership(string $company) : ?CompanyMembership {
    if ($data = $this->memoryCache
      ->get($company)) {
      return $data->data;
    }
    if ($data = $this->persistentCacheBackend
      ->get($company)) {

      // Next time return this from the memory cache.
      $this->memoryCache
        ->set($company, $data->data);
      return $data->data;
    }
    return NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CompanyMembershipObjectCache::$memoryCache private property Non-persistent cache backend.
CompanyMembershipObjectCache::$persistentCacheBackend private property Persistent cache backend.
CompanyMembershipObjectCache::$persistentCacheExpiration private property Number of seconds until an entity can be served from cache.
CompanyMembershipObjectCache::$systemTime private property The system time service.
CompanyMembershipObjectCache::DEFAULT_CACHE_BIN public constant The default persistent cache bin used by this service.
CompanyMembershipObjectCache::getMembership public function Returns membership object from the cache. Overrides CompanyMembershipObjectCacheInterface::getMembership
CompanyMembershipObjectCache::invalidateMemberships public function Invalidate membership objects by tags. Overrides CompanyMembershipObjectCacheInterface::invalidateMemberships
CompanyMembershipObjectCache::removeMembership public function Removes company membership object from the cache. Overrides CompanyMembershipObjectCacheInterface::removeMembership
CompanyMembershipObjectCache::saveMembership public function Saves company membership object to the cache. Overrides CompanyMembershipObjectCacheInterface::saveMembership
CompanyMembershipObjectCache::__construct public function CompanyMembershipObjectCache constructor.