Overview

Namespaces

  • Webmozart
    • KeyValueStore
      • Api
      • Decorator
      • Util

Interfaces

  • CountableStore
  • KeyValueStore
  • SortableStore

Exceptions

  • InvalidKeyException
  • NoSuchKeyException
  • ReadException
  • SerializationFailedException
  • UnserializationFailedException
  • UnsupportedValueException
  • WriteException
  • Overview
  • Namespace
  • Class

Interface KeyValueStore

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.

Direct known implementers

Webmozart\KeyValueStore\AbstractRedisStore, Webmozart\KeyValueStore\Api\CountableStore, Webmozart\KeyValueStore\Api\SortableStore, Webmozart\KeyValueStore\DbalStore, Webmozart\KeyValueStore\Decorator\AbstractDecorator, Webmozart\KeyValueStore\MongoDbStore, Webmozart\KeyValueStore\RiakStore

Indirect known implementers

Webmozart\KeyValueStore\ArrayStore, Webmozart\KeyValueStore\Decorator\CachingDecorator, Webmozart\KeyValueStore\Decorator\CountableDecorator, Webmozart\KeyValueStore\Decorator\SortableDecorator, Webmozart\KeyValueStore\JsonFileStore, Webmozart\KeyValueStore\NullStore, Webmozart\KeyValueStore\PhpRedisStore, Webmozart\KeyValueStore\PredisStore, Webmozart\KeyValueStore\SerializingArrayStore
Namespace: Webmozart\KeyValueStore\Api
Since: 1.0
Author: Bernhard Schussek bschussek@gmail.com
Located at Api/KeyValueStore.php
Methods summary
public
# set( integer|string $key, mixed $value )

Sets the value for a key in the store.

Sets the value for a key in the store.

The key-value store accepts any serializable value. If a value is not serializable, a Webmozart\KeyValueStore\Api\SerializationFailedException is thrown. Additionally, implementations may put further restrictions on their accepted values. If an unsupported value is passed, an Webmozart\KeyValueStore\Api\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 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->set($key, $value);
} catch (WriteException $e) { // write failed
}

Parameters

$key
The key to set.
$value
The value to set for the key.

Throws

Webmozart\KeyValueStore\Api\WriteException
If the store cannot be written.
Webmozart\KeyValueStore\Api\InvalidKeyException
If the key is not a string or integer.
Webmozart\KeyValueStore\Api\SerializationFailedException
If the value cannot be serialized.
Webmozart\KeyValueStore\Api\UnsupportedValueException

If the value is not supported by the implementation.

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) { // read failed
}

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

Webmozart\KeyValueStore\Api\ReadException
If the store cannot be read.
Webmozart\KeyValueStore\Api\InvalidKeyException
If the key is not a string or integer.
Webmozart\KeyValueStore\Api\UnserializationFailedException

If the stored value cannot be unserialized.

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) { // read failed
}

Parameters

$key
The key to get.

Returns

mixed
The value of the key.

Throws

Webmozart\KeyValueStore\Api\ReadException
If the store cannot be read.
Webmozart\KeyValueStore\Api\NoSuchKeyException
If the key was not found.
Webmozart\KeyValueStore\Api\InvalidKeyException
If the key is not a string or integer.
Webmozart\KeyValueStore\Api\UnserializationFailedException

If the stored value cannot be unserialized.

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) { // read failed
}

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

Webmozart\KeyValueStore\Api\ReadException
If the store cannot be read.
Webmozart\KeyValueStore\Api\InvalidKeyException
If a key is not a string or integer.
Webmozart\KeyValueStore\Api\UnserializationFailedException

If a stored value cannot be unserialized.

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) { // read failed
}

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

Webmozart\KeyValueStore\Api\ReadException
If the store cannot be read.
Webmozart\KeyValueStore\Api\NoSuchKeyException
If a key was not found.
Webmozart\KeyValueStore\Api\InvalidKeyException
If a key is not a string or integer.
Webmozart\KeyValueStore\Api\UnserializationFailedException

If a stored value cannot be unserialized.

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) { // write failed
}

Parameters

$key
The key to remove.

Returns

boolean
Returns true if a key was removed from the store.

Throws

Webmozart\KeyValueStore\Api\WriteException
If the store cannot be written.
Webmozart\KeyValueStore\Api\InvalidKeyException
If the key is not a string or integer.
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) { // read failed
}

Parameters

$key
The key to test.

Returns

boolean
Whether the key exists in the store.

Throws

Webmozart\KeyValueStore\Api\ReadException
If the store cannot be read.
Webmozart\KeyValueStore\Api\InvalidKeyException
If the key is not a string or integer.
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) { // write failed
}

Throws

Webmozart\KeyValueStore\Api\WriteException
If the store cannot be written.
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) { // read failed
}

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

Webmozart\KeyValueStore\Api\ReadException
If the store cannot be read.
Webmozart Key-Value Store API API documentation generated by ApiGen