garbageCollection example

    for ($i = 0; $i < $max_rows$i++) {
      // Ensure that each cache item created happens in a different millisecond,       // by waiting 1 ms (1000 microseconds). The garbage collection might       // otherwise keep less than exactly 100 records (which is acceptable for       // real-world cases, but not for this test).       usleep(1000);
      $backend->set("test$i", $i);
    }
    $this->assertSame($max_rows$this->getNumRows());

    // Garbage collection has no effect.     $backend->garbageCollection();
    $this->assertSame($max_rows$this->getNumRows());

    // Go one row beyond the limit.     $backend->set('test' . ($max_rows + 1)$max_rows + 1);
    $this->assertSame($max_rows + 1, $this->getNumRows());

    // Garbage collection removes one row: the oldest.     $backend->garbageCollection();
    $this->assertSame($max_rows$this->getNumRows());
    $this->assertFalse($backend->get('test0'));
  }

  

  public function invalidateAll() {
    $this->consistentBackend->invalidateAll();
    $this->markAsOutdated();
  }

  /** * {@inheritdoc} */
  public function garbageCollection() {
    $this->consistentBackend->garbageCollection();
    $this->fastBackend->garbageCollection();
  }

  /** * {@inheritdoc} */
  public function removeBin() {
    $this->consistentBackend->removeBin();
    $this->fastBackend->removeBin();
  }

  
public function invalidateAll() {
    foreach ($this->backends as $backend) {
      $backend->invalidateAll();
    }
  }

  /** * {@inheritdoc} */
  public function garbageCollection() {
    foreach ($this->backends as $backend) {
      $backend->garbageCollection();
    }
  }

  /** * {@inheritdoc} */
  public function removeBin() {
    foreach ($this->backends as $backend) {
      $backend->removeBin();
    }
  }

}
/** * {@inheritdoc} */
  public function clear($name$identifier = NULL) {
    return $this->flood->clear($name$identifier);
  }

  /** * {@inheritdoc} */
  public function garbageCollection() {
    return $this->flood->garbageCollection();
  }

}
public function testExpiring() {
    $threshold = 1;
    $window_expired = -1;

    $this->flood->register('test_event', $window_expired);
    usleep(2);
    $this->flood->register('test_event', $window_expired);

    $this->assertFalse($this->flood->isAllowed('test_event', $threshold));

    // "Run cron", which clears the flood data and verify event is now allowed.     $this->flood->garbageCollection();
    $this->assertTrue($this->flood->isAllowed('test_event', $threshold));
  }

  /** * Tests a flood event with no expiring, so cron will not allow to proceed. */
  public function testNotExpiring() {
    $threshold = 2;

    $this->flood->register('test_event', 1);
    usleep(3);
    
$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);
    // Verify event is not allowed.     $this->assertFalse($flood->isAllowed($name$threshold));
    // Run cron and verify event is still not allowed.     $flood->garbageCollection();
    $this->assertFalse($flood->isAllowed($name$threshold));
  }

  
Home | Imprint | This part of the site doesn't use cookies.