Commit 032c3986 authored by webchick's avatar webchick
Browse files

Issue #2216459 by sun: Add a KeyValueStoreInterface::has() method.

parent 96115e6d
......@@ -46,6 +46,16 @@ public function __construct($collection, Connection $connection, $table = 'key_v
$this->table = $table;
}
/**
* {@inheritdoc}
*/
public function has($key) {
return (bool) $this->connection->query('SELECT 1 FROM {' . $this->connection->escapeTable($this->table) . '} WHERE collection = :collection AND name = :key', array(
':collection' => $this->collection,
':key' => $key,
))->fetchField();
}
/**
* Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::getMultiple().
*/
......
......@@ -56,6 +56,17 @@ public function __construct($collection, Connection $connection, $table = 'key_v
parent::__construct($collection, $connection, $table);
}
/**
* {@inheritdoc}
*/
public function has($key) {
return (bool) $this->connection->query('SELECT 1 FROM {' . $this->connection->escapeTable($this->table) . '} WHERE collection = :collection AND name = :key AND expire > :now', array(
':collection' => $this->collection,
':key' => $key,
':now' => REQUEST_TIME,
))->fetchField();
}
/**
* Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::getMultiple().
*/
......
......@@ -20,6 +20,17 @@ interface KeyValueStoreInterface {
*/
public function getCollectionName();
/**
* Returns whether a given key exists in the store.
*
* @param string $key
* The key to check.
*
* @return bool
* TRUE if the key exists, FALSE otherwise.
*/
public function has($key);
/**
* Returns the stored value for a given key.
*
......
......@@ -19,6 +19,13 @@ class MemoryStorage extends StorageBase {
*/
protected $data = array();
/**
* {@inheritdoc}
*/
public function has($key) {
return array_key_exists($key, $this->data);
}
/**
* Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::get().
*/
......
......@@ -33,6 +33,13 @@ public function __construct($collection) {
$this->collection = $collection;
}
/**
* {@inheritdoc}
*/
public function has($key) {
return FALSE;
}
/**
* Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::get().
*/
......
......@@ -141,7 +141,9 @@ public function testExpiration() {
$stores[0]->set('troubles', 'here to stay');
// Only the non-expired item should be returned.
$this->assertFalse($stores[0]->has('yesterday'));
$this->assertFalse($stores[0]->get('yesterday'));
$this->assertTrue($stores[0]->has('troubles'));
$this->assertIdentical($stores[0]->get('troubles'), 'here to stay');
$this->assertIdentical(count($stores[0]->getMultiple(array('yesterday', 'troubles'))), 1);
......
......@@ -83,8 +83,10 @@ public function testCRUD() {
// Verify that an item can be stored.
$stores[0]->set('foo', $this->objects[0]);
$this->assertTrue($stores[0]->has('foo'));
$this->assertIdenticalObject($this->objects[0], $stores[0]->get('foo'));
// Verify that the other collection is not affected.
$this->assertFalse($stores[1]->has('foo'));
$this->assertFalse($stores[1]->get('foo'));
// Verify that an item can be updated.
......@@ -100,9 +102,11 @@ public function testCRUD() {
// Verify that an item can be deleted.
$stores[0]->delete('foo');
$this->assertFalse($stores[0]->has('foo'));
$this->assertFalse($stores[0]->get('foo'));
// Verify that the other collection is not affected.
$this->assertTrue($stores[1]->has('foo'));
$this->assertIdenticalObject($this->objects[2], $stores[1]->get('foo'));
$stores[1]->delete('foo');
$this->assertFalse($stores[1]->get('foo'));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment