An Aerospike client library for PHP 8+.
This is the documentation for the Aerospike PHP Client. The PHP client comprises of two essential components. Firstly, we have a robust PHP client written in Rust and a connection manager written in Go, serving as a shared resource among PHP processes. The connection manager daemon efficiently handles all requests and responses between the PHP processes and the Aerospike server.
cd php-client
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
cd php-client/aerospike-connection-manager
sudo make
NOTE: Please view the README.md in the php-client/aerospike-connection-manager
directory for more information about the setting up the aerospike-connection-manager and configuring the client policy.
php -v
PHP-Client
in the default paths run the makefile
cd php-client
sudo make
PHP-Client
in manually, run the following commands:
cd php-client
cargo clean && cargo build --release
target/release/libaerospike$(EXTENSION)
[$EXTENSION = .so for Linux and .dylib for Mac and Windows] to the PHP extension directory path. extension=libaerospike$(EXTENSION)
to your php.ini
file. phpunit tests/
to ensure the setup and build was successful. NOTE: The Aerospike server must be running for the tests to run successfully.
$socket = "/tmp/asld_grpc.sock";
$client = Client::connect($socket);
NOTE: If the connection manager daemon crashes, you will have to manually remove the file /tmp/asld_grpc.sock
from its path.
sudo rm -r /tmp/asld_grpc.soc
// Instantiate the WritePolicy object
$writePolicy = new WritePolicy();
$writePolicy->setRecordExistsAction(RecordExistsAction::Update);
$writePolicy->setGenerationPolicy(GenerationPolicy::ExpectGenEqual);
$writePolicy->setExpiration(Expiration::seconds(3600)); // Expiring in 1 hour
$writePolicy->setMaxRetries(3);
$writePolicy->setSocketTimeout(5000);
If there are any bugs, feature requests or feedback -> please create an issue on GitHub. Issues will be regularly reviewed by the Aerospike Client Engineering Team.
The following is a very simple example of CRUD operations in an Aerospike database.
<?php
namespace Aerospike;
try{
$socket = "/tmp/asld_grpc.sock";
$client = Client::connect($socket);
var_dump($client->socket);
}catch(AerospikeException $e){
var_dump($e);
}
$key = new Key("namespace", "set_name", 1);
//PUT on differnet types of values
$wp = new WritePolicy();
$bin1 = new Bin("bin1", 111);
$bin2 = new Bin("bin2", "string");
$bin3 = new Bin("bin3", 333.333);
$bin4 = new Bin("bin4", [
"str",
1984,
333.333,
[1, "string", 5.1],
[
"integer" => 1984,
"float" => 333.333,
"list" => [1, "string", 5.1]
]
]);
$bin5 = new Bin("bin5", [
"integer" => 1984,
"float" => 333.333,
"list" => [1, "string", 5.1],
null => [
"integer" => 1984,
"float" => 333.333,
"list" => [1, "string", 5.1]
],
"" => [ 1, 2, 3 ],
]);
$client->put($wp, $key, [$bin1, $bin2, $bin3, $bin4, $bin5]);
//GET
$rp = new ReadPolicy();
$record = $client->get($rp, $key);
var_dump($record->bins);
//UPDATE
$client->prepend($wp, $key, [new Bin("bin2", "prefix_")]);
$client->append($wp, $key, [new Bin("bin2", "_suffix")]);
//DELETE
$deleted = $client->delete($wp, $key);
var_dump($deleted);
$client->close()
Batch Operations Examples:
<?php
namespace Aerospike;
$namespace = "test";
$set = "test";
$socket = "/tmp/asld_grpc.sock";
$client = Client::connect($socket);
echo "* Connected to the local daemon: $client->hosts \n";
$key = new Key($namespace, $set, 1);
$wp = new WritePolicy();
$client->put($wp, $key, [new Bin("bini", 1), new Bin("bins", "b"), new Bin("bin1", [1, 2, 3, 4])]);
$bwp = new BatchWritePolicy();
$exp = Expression::lt(Expression::intBin("bin1"), Expression::intVal(1));
$batchWritePolicy->setFilterExpression($exp);
$ops = [Operation::put(new Bin("put_op", "put_val"))];
$bw = new BatchWrite($bwp, $key, $ops);
$brp = new BatchReadPolicy();
$br = new BatchRead($brp, $key, []);
$bdp = new BatchDeletePolicy();
$bd = new BatchDelete($bdp, $key);
$bp = new BatchPolicy();
$recs = $client->batch($bp, [$bw, $br, $bd]);
var_dump($recs);
For more detailed examples you can see the examples direcotry php-client/examples