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.
Methods summary
public
|
#
set( integer|string $key, mixed $value )
Sets the value for a key in the store.
|
public
mixed
|
#
get( integer|string $key, mixed $default = null )
Returns the value of a key in the store.
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 Webmozart\KeyValueStore\Api\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 Webmozart\KeyValueStore\Api\ReadException
is thrown. You should always handle this exception in your code:
try { $value = $store->get($key);
} catch (ReadException $e) {
}
Parameters
- $key
- The key to get.
- $default
The default value to return if the key does
not exist.
Returns
mixed The value of the key or the default value if the key does
not exist.
Throws
|
public
mixed
|
#
getOrFail( integer|string $key )
Returns the value of a key in the store.
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 Webmozart\KeyValueStore\Api\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 Webmozart\KeyValueStore\Api\ReadException
is thrown. You should always handle this exception in your code:
try { $value = $store->getOrFail($key);
} catch (ReadException $e) {
}
Parameters
Returns
mixed The value of the key.
Throws
|
public
array
|
#
getMultiple( array $keys, mixed $default = null )
Returns the values of multiple keys in the store.
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 Webmozart\KeyValueStore\Api\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 Webmozart\KeyValueStore\Api\ReadException
is thrown. You should always handle this exception in your code:
try { $value = $store->getMultiple(array($key1, $key2));
} catch (ReadException $e) {
}
Parameters
- $keys
- The keys to get. The keys must be strings or integers.
- $default
The default value to return for keys that are not
found.
Returns
array The values of the passed keys, indexed by the keys.
Throws
|
public
array
|
#
getMultipleOrFail( array $keys )
Returns the values of multiple keys in the store.
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 Webmozart\KeyValueStore\Api\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 Webmozart\KeyValueStore\Api\ReadException
is thrown. You should always handle this exception in your code:
try { $value = $store->getMultipleOrFail(array($key1, $key2));
} catch (ReadException $e) {
}
Parameters
- $keys
- The keys to get. The keys must be strings or integers.
Returns
array The values of the passed keys, indexed by the keys.
Throws
|
public
boolean
|
#
remove( integer|string $key )
Removes a key from the store.
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 Webmozart\KeyValueStore\Api\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 Webmozart\KeyValueStore\Api\WriteException
is thrown. You should always handle this exception in your code:
try { $store->remove($key);
} catch (WriteException $e) {
}
Parameters
Returns
boolean Returns true if a key was removed from the store.
Throws
|
public
boolean
|
#
exists( integer|string $key )
Returns whether a key exists.
Returns whether a key exists.
Any integer or string value is accepted as key. If any other type is
passed for the key, an Webmozart\KeyValueStore\Api\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 Webmozart\KeyValueStore\Api\ReadException
is thrown. You should always handle this exception in your code:
try { if ($store->exists($key)) {
} catch (ReadException $e) {
}
Parameters
Returns
boolean Whether the key exists in the store.
Throws
|
public
|
#
clear( )
Removes all keys from the store.
Removes all keys from the store.
If the backend of the store cannot be written, a Webmozart\KeyValueStore\Api\WriteException
is thrown. You should always handle this exception in your code:
try { $store->clear();
} catch (WriteException $e) {
}
Throws
|
public
array
|
#
keys( )
Returns all keys currently stored in the store.
Returns all keys currently stored in the store.
If the backend of the store cannot be read, a Webmozart\KeyValueStore\Api\ReadException
is thrown. You should always handle this exception in your code:
try { foreach ($store->keys() as $key) {
} catch (ReadException $e) {
}
Returns
array The keys stored in the store. Each key is either a string
or an integer. The order of the keys is undefined.
Throws
|