1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295:
<?php
/*
* This file is part of the webmozart/key-value-store package.
*
* (c) Bernhard Schussek <bschussek@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Webmozart\KeyValueStore\Api;
/**
* A key-value store.
*
* KeyUtil-value stores support storing values for integer or string keys chosen
* by the user. Any serializable value can be stored, although an implementation
* of this interface may further restrict the range of accepted values. See the
* documentation of the implementation for more information.
*
* @since 1.0
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
interface KeyValueStore
{
/**
* Sets the value for a key in the store.
*
* The key-value store accepts any serializable value. If a value is not
* serializable, a {@link SerializationFailedException} is thrown.
* Additionally, implementations may put further restrictions on their
* accepted values. If an unsupported value is passed, an
* {@link UnsupportedValueException} is thrown. Check the documentation of
* the implementation to learn more about its supported values.
*
* Any integer or string value is accepted as key. If any other type is
* passed for the key, an {@link InvalidKeyException} is thrown. You should
* make sure that you only pass valid keys to the store.
*
* If the backend of the store cannot be written, a {@link WriteException}
* is thrown. You should always handle this exception in your code:
*
* ```php
* try {
* $store->set($key, $value);
* } catch (WriteException $e) {
* // write failed
* }
* ```
*
* @param int|string $key The key to set.
* @param mixed $value The value to set for the key.
*
* @throws WriteException If the store cannot be written.
* @throws InvalidKeyException If the key is not a string or integer.
* @throws SerializationFailedException If the value cannot be serialized.
* @throws UnsupportedValueException If the value is not supported by the
* implementation.
*/
public function set($key, $value);
/**
* Returns the value of a key in the store.
*
* If a key does not exist in the store, the default value passed in the
* second parameter is returned.
*
* Any integer or string value is accepted as key. If any other type is
* passed for the key, an {@link InvalidKeyException} is thrown. You should
* make sure that you only pass valid keys to the store.
*
* If the backend of the store cannot be read, a {@link ReadException}
* is thrown. You should always handle this exception in your code:
*
* ```php
* try {
* $value = $store->get($key);
* } catch (ReadException $e) {
* // read failed
* }
* ```
*
* @param int|string $key The key to get.
* @param mixed $default The default value to return if the key does
* not exist.
*
* @return mixed The value of the key or the default value if the key does
* not exist.
*
* @throws ReadException If the store cannot be read.
* @throws InvalidKeyException If the key is not a string or integer.
* @throws UnserializationFailedException If the stored value cannot be
* unserialized.
*/
public function get($key, $default = null);
/**
* Returns the value of a key in the store.
*
* If the key does not exist in the store, an exception is thrown.
*
* Any integer or string value is accepted as key. If any other type is
* passed for the key, an {@link InvalidKeyException} is thrown. You should
* make sure that you only pass valid keys to the store.
*
* If the backend of the store cannot be read, a {@link ReadException}
* is thrown. You should always handle this exception in your code:
*
* ```php
* try {
* $value = $store->getOrFail($key);
* } catch (ReadException $e) {
* // read failed
* }
* ```
*
* @param int|string $key The key to get.
*
* @return mixed The value of the key.
*
* @throws ReadException If the store cannot be read.
* @throws NoSuchKeyException If the key was not found.
* @throws InvalidKeyException If the key is not a string or integer.
* @throws UnserializationFailedException If the stored value cannot be
* unserialized.
*/
public function getOrFail($key);
/**
* Returns the values of multiple keys in the store.
*
* The passed default value is returned for keys that don't exist.
*
* Any integer or string value is accepted as key. If any other type is
* passed for the key, an {@link InvalidKeyException} is thrown. You should
* make sure that you only pass valid keys to the store.
*
* If the backend of the store cannot be read, a {@link ReadException}
* is thrown. You should always handle this exception in your code:
*
* ```php
* try {
* $value = $store->getMultiple(array($key1, $key2));
* } catch (ReadException $e) {
* // read failed
* }
* ```
*
* @param array $keys The keys to get. The keys must be strings or integers.
* @param mixed $default The default value to return for keys that are not
* found.
*
* @return array The values of the passed keys, indexed by the keys.
*
* @throws ReadException If the store cannot be read.
* @throws InvalidKeyException If a key is not a string or integer.
* @throws UnserializationFailedException If a stored value cannot be
* unserialized.
*/
public function getMultiple(array $keys, $default = null);
/**
* Returns the values of multiple keys in the store.
*
* If a key does not exist in the store, an exception is thrown.
*
* Any integer or string value is accepted as key. If any other type is
* passed for the key, an {@link InvalidKeyException} is thrown. You should
* make sure that you only pass valid keys to the store.
*
* If the backend of the store cannot be read, a {@link ReadException}
* is thrown. You should always handle this exception in your code:
*
* ```php
* try {
* $value = $store->getMultipleOrFail(array($key1, $key2));
* } catch (ReadException $e) {
* // read failed
* }
* ```
*
* @param array $keys The keys to get. The keys must be strings or integers.
*
* @return array The values of the passed keys, indexed by the keys.
*
* @throws ReadException If the store cannot be read.
* @throws NoSuchKeyException If a key was not found.
* @throws InvalidKeyException If a key is not a string or integer.
* @throws UnserializationFailedException If a stored value cannot be
* unserialized.
*/
public function getMultipleOrFail(array $keys);
/**
* Removes a key from the store.
*
* If the store does not contain the key, this method returns `false`.
*
* Any integer or string value is accepted as key. If any other type is
* passed for the key, an {@link InvalidKeyException} is thrown. You should
* make sure that you only pass valid keys to the store.
*
* If the backend of the store cannot be written, a {@link WriteException}
* is thrown. You should always handle this exception in your code:
*
* ```php
* try {
* $store->remove($key);
* } catch (WriteException $e) {
* // write failed
* }
* ```
*
* @param int|string $key The key to remove.
*
* @return bool Returns `true` if a key was removed from the store.
*
* @throws WriteException If the store cannot be written.
* @throws InvalidKeyException If the key is not a string or integer.
*/
public function remove($key);
/**
* Returns whether a key exists.
*
* Any integer or string value is accepted as key. If any other type is
* passed for the key, an {@link InvalidKeyException} is thrown. You should
* make sure that you only pass valid keys to the store.
*
* If the backend of the store cannot be read, a {@link ReadException}
* is thrown. You should always handle this exception in your code:
*
* ```php
* try {
* if ($store->exists($key)) {
* // ...
* }
* } catch (ReadException $e) {
* // read failed
* }
* ```
*
* @param int|string $key The key to test.
*
* @return bool Whether the key exists in the store.
*
* @throws ReadException If the store cannot be read.
* @throws InvalidKeyException If the key is not a string or integer.
*/
public function exists($key);
/**
* Removes all keys from the store.
*
* If the backend of the store cannot be written, a {@link WriteException}
* is thrown. You should always handle this exception in your code:
*
* ```php
* try {
* $store->clear();
* } catch (WriteException $e) {
* // write failed
* }
* ```
*
* @throws WriteException If the store cannot be written.
*/
public function clear();
/**
* Returns all keys currently stored in the store.
*
* If the backend of the store cannot be read, a {@link ReadException}
* is thrown. You should always handle this exception in your code:
*
* ```php
* try {
* foreach ($store->keys() as $key) {
* // ...
* }
* } catch (ReadException $e) {
* // read failed
* }
* ```
*
* @return array The keys stored in the store. Each key is either a string
* or an integer. The order of the keys is undefined.
*
* @throws ReadException If the store cannot be read.
*/
public function keys();
}