DatabaseBackend example

/** * Tests flood control database backend. */
  public function testDatabaseBackend() {
    $threshold = 1;
    $window_expired = -1;
    $name = 'flood_test_cleanup';

    $connection = \Drupal::service('database');
    $request_stack = \Drupal::service('request_stack');
    $flood = new DatabaseBackend($connection$request_stack);
    $this->assertTrue($flood->isAllowed($name$threshold));
    // Register expired event.     $flood->register($name$window_expired);
    // Verify event is not allowed.     $this->assertFalse($flood->isAllowed($name$threshold));
    // Run cron and verify event is now allowed.     $flood->garbageCollection();
    $this->assertTrue($flood->isAllowed($name$threshold));

    // Register unexpired event.     $flood->register($name);
    
/** * Gets DatabaseBackend for the specified cache bin. * * @param $bin * The cache bin for which the object is created. * * @return \Drupal\Core\Cache\DatabaseBackend * The cache backend object for the specified cache bin. */
  public function get($bin) {
    $max_rows = $this->getMaxRowsForBin($bin);
    return new DatabaseBackend($this->connection, $this->checksumProvider, $bin$max_rows);
  }

  /** * Gets the max rows for the specified cache bin. * * @param string $bin * The cache bin for which the object is created. * * @return int * The maximum number of rows for the given bin. Defaults to * DatabaseBackend::DEFAULT_MAX_ROWS. */

class ChainedFastBackendTest extends GenericCacheBackendUnitTestBase {

  /** * Creates a new instance of ChainedFastBackend. * * @return \Drupal\Core\Cache\ChainedFastBackend * A new ChainedFastBackend object. */
  protected function createCacheBackend($bin) {
    $consistent_backend = new DatabaseBackend(\Drupal::service('database'), \Drupal::service('cache_tags.invalidator.checksum')$bin, 100);
    $fast_backend = new PhpBackend($bin, \Drupal::service('cache_tags.invalidator.checksum'));
    $backend = new ChainedFastBackend($consistent_backend$fast_backend$bin);
    // Explicitly register the cache bin as it can not work through the     // cache bin list in the container.     \Drupal::service('cache_tags.invalidator')->addInvalidator($backend);
    return $backend;
  }

}

  protected static $modules = ['system'];

  /** * Creates a new instance of DatabaseBackend. * * @return \Drupal\Core\Cache\DatabaseBackend * A new DatabaseBackend object. */
  protected function createCacheBackend($bin) {
    return new DatabaseBackend($this->container->get('database')$this->container->get('cache_tags.invalidator.checksum')$binstatic::$maxRows);
  }

  /** * {@inheritdoc} */
  public function testSetGet() {
    parent::testSetGet();
    $backend = $this->getCacheBackend();

    // Set up a cache ID that is not ASCII and longer than 255 characters so we     // can test cache ID normalization.
Home | Imprint | This part of the site doesn't use cookies.