diff --git a/core/lib/Drupal/Core/KeyValueStore/DatabaseStorageExpirable.php b/core/lib/Drupal/Core/KeyValueStore/DatabaseStorageExpirable.php index 58785c4..1bcbe47 100644 --- a/core/lib/Drupal/Core/KeyValueStore/DatabaseStorageExpirable.php +++ b/core/lib/Drupal/Core/KeyValueStore/DatabaseStorageExpirable.php @@ -8,6 +8,7 @@ namespace Drupal\Core\KeyValueStore; use Drupal\Core\Database\Query\Merge; +use Drupal\Core\Database\Database; /** * Defines a default key/value store implementation for expiring items. @@ -32,13 +33,14 @@ class DatabaseStorageExpirable extends DatabaseStorage implements KeyValueStoreE * @param array $options * An associative array of options for the key/value storage collection. * Keys used: - * - connection: The database connection to use for storing the data. + * - connection: (optional) The database connection to use for storing the + * data. Defaults to the current connection. * - table: (optional) The name of the SQL table to use. Defaults to * key_value_expire. */ public function __construct($collection, array $options = array()) { parent::__construct($collection, $options); - $this->connection = $options['connection']; + $this->connection = isset($options['connection']) ? $options['connection'] : Database::getConnection(); $this->table = isset($options['table']) ? $options['table'] : 'key_value_expire'; } @@ -46,41 +48,27 @@ public function __construct($collection, array $options = array()) { * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::getMultiple(). */ public function getMultiple(array $keys) { - $values = array(); - try { - $result = $this->connection->query('SELECT name, value, expire FROM {' . $this->connection->escapeTable($this->table) . '} WHERE expire > :now AND name IN (:keys) AND collection = :collection', - array( - ':now' => REQUEST_TIME, - ':keys' => $keys, - ':collection' => $this->collection, - ))->fetchAllAssoc('name'); - foreach ($keys as $key) { - if (isset($result[$key])) { - $values[$key] = unserialize($result[$key]->value); - } - } - } - catch (\Exception $e) { - // @todo Perhaps if the database is never going to be available, - // key/value requests should return FALSE in order to allow exception - // handling to occur but for now, keep it an array, always. - } - return $values; + $values = $this->connection->query( + 'SELECT name, value FROM {' . $this->connection->escapeTable($this->table) . '} WHERE expire > :now AND name IN (:keys) AND collection = :collection', + array( + ':now' => REQUEST_TIME, + ':keys' => $keys, + ':collection' => $this->collection, + ))->fetchAllKeyed(); + return array_map('unserialize', $values); } /** * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::getAll(). */ public function getAll() { - $result = $this->connection->query('SELECT name, value FROM {' . $this->connection->escapeTable($this->table) . '} WHERE collection = :collection AND expire > :now', array(':collection' => $this->collection, ':now' => REQUEST_TIME)); - $values = array(); - - foreach ($result as $item) { - if ($item) { - $values[$item->name] = unserialize($item->value); - } - } - return $values; + $values = $this->connection->query( + 'SELECT name, value FROM {' . $this->connection->escapeTable($this->table) . '} WHERE collection = :collection AND expire > :now', + array( + ':collection' => $this->collection, + ':now' => REQUEST_TIME + ))->fetchAllKeyed(); + return array_map('unserialize', $values); } /** diff --git a/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/DatabaseStorageExpirableTest.php b/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/DatabaseStorageExpirableTest.php new file mode 100644 index 0000000..db85d35 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/DatabaseStorageExpirableTest.php @@ -0,0 +1,42 @@ + 'Expirable database storage', + 'description' => 'Tests the expirable key-value database storage.', + 'group' => 'Key-value store', + ); + } + + protected function setUp() { + parent::setUp(); + module_load_install('system'); + $schema = system_schema(); + db_create_table('key_value_expire', $schema['key_value_expire']); + } + + protected function tearDown() { + db_drop_table('key_value_expire'); + parent::tearDown(); + } + +} diff --git a/core/modules/system/system.install b/core/modules/system/system.install index a177b3e..3382fdf 100644 --- a/core/modules/system/system.install +++ b/core/modules/system/system.install @@ -858,10 +858,10 @@ function system_schema() { 'translatable' => TRUE, ), 'expire' => array( - 'description' => 'The time since Unix epoch in seconds when this item expires.', + 'description' => 'The time since Unix epoch in seconds when this item expires. Defaults to January 18 2038 (the end of the Unix epoch).', 'type' => 'int', 'not null' => TRUE, - 'default' => 0, + 'default' => 2147483647, ), ), 'primary key' => array('collection', 'name'), @@ -2172,7 +2172,7 @@ function system_update_8023() { 'description' => 'The time since Unix epoch in seconds when this item expires.', 'type' => 'int', 'not null' => TRUE, - 'default' => 0, + 'default' => 2147483647, ), ), 'primary key' => array('collection', 'name'), diff --git a/core/modules/user/lib/Drupal/user/TempStore.php b/core/modules/user/lib/Drupal/user/TempStore.php index 58f0495..3ee8732 100644 --- a/core/modules/user/lib/Drupal/user/TempStore.php +++ b/core/modules/user/lib/Drupal/user/TempStore.php @@ -119,9 +119,6 @@ function setIfNotExists($key, $value) { * The key of the data to store. * @param mixed $value * The data to store. - * - * @todo Should we throw an exception here if the lock cannot be acquired - * like we do in delete()? */ function set($key, $value) { if (!$this->lockBackend->acquire($key)) {