Intro into KV
Last updated
Was this helpful?
Last updated
Was this helpful?
The RoadRunner KV (Key-Value) plugin is a powerful cache implementation written in Go language. It offers lightning-fast communication with cache drivers such as:
,
,
- does not require a separate server,
storage — temporary stores data in RAM.
It is able to handle cache operations more efficiently than the same operations in PHP, leading to faster response times and improved application performance.
It provides the ability to store arbitrary data inside the RoadRunner between different requests (in case of HTTP application) or different types of applications. Thus, using , for example, you can transfer data inside the and vice versa.
One of the key benefits of using the RoadRunner KV plugin is its RPC interface, which allows for seamless integration with your existing infrastructure.
To use the RoadRunner KV plugin, you need to define multiple key-value storages with desired storage drivers in the configuration file. Each storage must have a driver
that indicates the type of connection used by those storages. At the moment, four different types of drivers are available: boltdb
, redis
, memcached
, and memory
.
Here is a simple configuration example:
The RoadRunner KV plugin comes with a convenient PHP package that simplifies the process of integrating the plugin with your PHP application and store and request data from storages using RoadRunner PRC.
Requirements
PHP >= 8.1
ext-protobuf (optional)
You can install the package via Composer using the following command:
First, you need to create the RPC connection to the RoadRunner server.
To work with storages, you should create the Spiral\RoadRunner\KeyValue\Factory
object after creating the RPC connection. It provides a method for selecting the storage.
Here is a simple example of using:
The RoadRunner KV API provides several additional methods, such as getTtl(string)
and getMultipleTtl(string)
, which allow you to get information about the expiration of an item stored in a key-value storage.
To save and receive data from the key-value store, the data serialization mechanism is used. This way you can store and receive arbitrary serializable objects.
If you need to specify your custom serializer, you can do so by specifying it in the key-value factory constructor as a second argument, or by using the Factory::withSerializer(SerializerInterface): self
method. This will allow you to use your own serialization mechanism and store more complex objects in the key-value store.
If you require a specific serializer for a particular value stored in the key-value storage, you can use the withSerializer()
method. This allows you to use a custom serializer for that particular value while still using the default serializer for other values.
In a Linux and macOS environment, it may be installed with a simple command:
Here is an example of using the igbinary
serializer:
Some data may contain sensitive information, such as personal data of the user. In these cases, it is recommended to use data encryption.
Do not store security keys in a control versioning system (like GIT)!
After generating the keypair, you can use it to encrypt and decrypt the data.
RoadRunner provides an RPC API, which allows you to manage key-value in your applications using remote procedure calls. The RPC API provides a set of methods that map to the available methods of the Spiral\RoadRunner\KeyValue\Cache
class in PHP.
Checks for the presence of one or more keys in the specified storage.
Sets one or more key-value pairs in the specified storage.
Gets the values of one or more keys from the specified storage.
Sets the expiration time for one or more keys in the specified storage.
Gets the expiration time of a single key in the specified storage.
Deletes one or more keys from the specified storage.
Clears all keys from the specified storage.
To use the RPC API in PHP, you can create an RPC connection to the RoadRunner server and use the call()
method to perform the desired operation. For example, to call the MGet
method, you can use the following code:
All KV drivers support opentelemetry tracing. To enable tracing, you need to add section to your configuration file:
After that, you can see traces in your , , , or any other opentelemetry compatible tracing system.
The memory
and boltdb
drivers do not require additional binaries and are available immediately, while the rest require additional setup. Please see the appropriate documentation for installing and/or .
to interact with the RoadRunner KV plugin, you will need to have the RPC defined in the rpc configuration section. You can refer to the documentation page to learn more about the configuration.
You can refer to the documentation page to learn more about creating the RPC connection.
Factory::select(string)
- method receives the name of the storage as the first argument and returns the implementation of the Psr\SimpleCache\CacheInterface
for interacting with the key-value RoadRunner storage.
Please note that the memcached
driver these methods.
The serialization mechanism in PHP is not always efficient, which can impact the performance of your application. To increase the speed of serialization and deserialization, it is recommended to use the .
For the Windows OS, you can download it from the .
More detailed installation instructions are .
To use encryption, you need to install the .
Next, you should have an encryption key generated using function. You can do this using the following command:
To make it easy to use the KV proto API in PHP, we provide a , that contains all the generated PHP DTO classes proto files, making it easy to work with these files in your PHP application.