diff -u b/modules/redirect_404/redirect_404.module b/modules/redirect_404/redirect_404.module --- b/modules/redirect_404/redirect_404.module +++ b/modules/redirect_404/redirect_404.module @@ -48,7 +48,6 @@ if ($row_limit > 0) { $query = \Drupal::database()->select('redirect_404', 'r404') ->extend('Drupal\Core\Database\Query\TableSortExtender') - ->orderBy('count', 'DESC') ->orderBy('timestamp', 'DESC') ->extend('Drupal\Core\Database\Query\PagerSelectExtender') ->limit($row_limit - 1) diff -u b/modules/redirect_404/src/EventSubscriber/Redirect404Subscriber.php b/modules/redirect_404/src/EventSubscriber/Redirect404Subscriber.php --- b/modules/redirect_404/src/EventSubscriber/Redirect404Subscriber.php +++ b/modules/redirect_404/src/EventSubscriber/Redirect404Subscriber.php @@ -75,6 +75,7 @@ $langcode = $this->languageManager->getCurrentLanguage()->getId(); $this->redirectStorage->logRequest($path, $langcode); } + debug($this->redirectStorage->purgeOldRequests()); } } diff -u b/modules/redirect_404/src/RedirectNotFoundStorage.php b/modules/redirect_404/src/RedirectNotFoundStorage.php --- b/modules/redirect_404/src/RedirectNotFoundStorage.php +++ b/modules/redirect_404/src/RedirectNotFoundStorage.php @@ -70,6 +70,32 @@ ->execute(); } + public function purgeOldRequests() { + // Cleanup the redirect_404 table. + $row_limit = \Drupal::config('redirect_404.settings')->get('row_limit'); + + // For row limit n, get the wid of the nth row in descending wid order. + // Counting the most recent n rows avoids issues with wid number sequences, + // e.g. auto_increment value > 1 or rows deleted directly from the table. + $query = $this->database->select('redirect_404', 'r404'); + //$query->fields('r404'); + $query->addExpression('pow(2, -(UNIX_TIMESTAMP(NOW()) - timestamp)/86400)', 'boh'); + //$query->condition('relevancy', 0.99, '>'); + $query->orderBy('boh', 'DESC'); + + $query->fields('r404'); + $query->condition('relevancy', 0.99, '>'); + $result = $query->execute()->fetchAll(); + + if ($result) { + $delete = $this->database->select('redirect_404', 'r404'); + $delete->condition('path', $result, 'NOT IN'); + $result = $delete->execute()->fetchAll(); + } + + return $result; + } + /** * {@inheritdoc} */ diff -u b/modules/redirect_404/src/RedirectNotFoundStorageInterface.php b/modules/redirect_404/src/RedirectNotFoundStorageInterface.php --- b/modules/redirect_404/src/RedirectNotFoundStorageInterface.php +++ b/modules/redirect_404/src/RedirectNotFoundStorageInterface.php @@ -47,2 +47,4 @@ + public function purgeOldRequests(); + }