You are here

public function KeyValueStoreExpirable::ensureIndexes in MongoDB 8.2

Ensure a TTL index for server-side expirations.

2 calls to KeyValueStoreExpirable::ensureIndexes()
KeyValueStoreExpirable::deleteAll in modules/mongodb_storage/src/KeyValueStoreExpirable.php
Deletes all items from the key/value store.
KeyValueStoreExpirable::__construct in modules/mongodb_storage/src/KeyValueStoreExpirable.php

File

modules/mongodb_storage/src/KeyValueStoreExpirable.php, line 44

Class

KeyValueStoreExpirable
KeyValueStore provides a KeyValueStoreExpirable as a MongoDB collection.

Namespace

Drupal\mongodb_storage

Code

public function ensureIndexes() {
  $name = $this->mongoDbCollection
    ->getCollectionName();
  $indexMissing = TRUE;
  foreach ($this->mongoDbCollection
    ->listIndexes() as $index) {
    if ($index
      ->isTtl()) {
      $indexMissing = FALSE;
      break;
    }
  }
  if ($indexMissing) {
    $indexes = [
      [
        'expireAfterSeconds' => 0,
        'key' => [
          'expire' => 1,
        ],
        'name' => "ttl_" . $name,
      ],
    ];
    $this->mongoDbCollection
      ->createIndexes($indexes);
  }
}