定制 aerospike/aerospike-php 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

aerospike/aerospike-php

Composer 安装命令:

composer require aerospike/aerospike-php

包简介

Aerospike PHP Extension Pre-Alpha

关键字:

README 文档

README

PHP version

Aerospike PHP 8 Client (v1.4.0)

An Aerospike client library for PHP 8

PHP-Client Introduction

This is the documentation for the Aerospike PHP-Client. The PHP-Client comprises of two essential components:

  • the client itself, written in Rust as a PHP extension
  • the connection manager (the "Aerospike Connection Manager" or "ACM") written in Go which serves as a shared resource among PHP processes. The ACM efficiently handles all requests and responses between the PHP processes and the Aerospike server, and can be configured to run as a daemonized service.

Dependencies

NOTE: Any missing dependencies will be installed by the installation script

Build & Installation

There are 2 ways to build and install the PHP-Client:

  1. direct script download and execution (also clones the repo for you)
  2. manually clone the repo first and then run script from there

The install script builds both the PHP-Client and the ACM, as well as installing all of the dependencies.

Automatic Method: Direct download and execution of installation script:

The installation script will clone the repo into a subfolder so execute this command directly above where you want the repo to go

For MacOS (Darwin):

curl -O https://raw.githubusercontent.com/aerospike/php-client/refs/heads/main/build/install_as_php_client_mac.zsh; chmod +x install_as_php_client_mac.zsh; ./install_as_php_client_mac.zsh

NOTE: the default MacOS installation is an all user-local installation, requiring no root or sudo access

For Linux:

curl -O https://raw.githubusercontent.com/aerospike/php-client/refs/heads/main/build/install_as_php_client_linux.sh; chmod +x install_as_php_client_linux.sh; sudo ./install_as_php_client_linux.sh

Manual method: repo clone followed by execution of the installation script:

  1. Clone the repo

  2. Run the installation script for your system:

    for MacOS (darwin):

    . ./php-client/build/install_as_php_client_mac.zsh

    or for linux:

    sudo ./php-client/build/install_as_php_client_linux.sh

NOTE: the default linux installation contains system-wide installations and will require root / sudo access

After the installation script completes, re-source your shell env config files to make sure your PATH is updated. Eg, on MacOS:

. ~/.zshrc

If you encounter errors during installation, you can try running the install script again as the install scripts attempt to be idempotent. As a last resort, the script commands can be run manually one-by-one as needed.

Configuring the Aerospike Connection Manager:

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.

NOTE: You should have an Aerospike server up and running to test against.

Manual Build and Install of the PHP-Client (optional)

In case the installation script fails to build the PHP-Client, or if you just want to run specific build commands, you may do so manually:

  • To manually build and install the PHP-Client in the default paths run the makefile

    Note: sudo is only needed when running a system-wide php installation, which is not the default install for MacOS (although it could be)

     cd php-client
     make
  • To build and install the PHP-Client in manually, run the following commands:

     cd php-client
     cargo clean && cargo build --release
  • Once the build is successful, copy the file from target/release/libaerospike$(EXTENSION) [$EXTENSION = .so for Linux and .dylib for Mac and Windows] to the PHP extension directory path.
  • Add extension=libaerospike$(EXTENSION) to your php.ini file.
  • Run phpunit tests/ to ensure the setup and build were successful.

NOTE: The Aerospike server must be running for the tests to run successfully.

Running your PHP Project

  • Before running your project PHP scripts, the following must be running:

    • An Aerospike Connection Manager (ACM)
    • An Aerospike Server that the ACM can connect to
  • Once the build is successful and all the pre-requisites are met, import the Aerospike namespace to your PHP script:

    namespace Aerospike;
  • To connect to the Aerospike server via the ACM add:

    $socket = "/tmp/asld_grpc.sock";
    $client = Client::connect($socket); 
  • Run the php script If there are no Errors then you have successfully connected to the Aerospike DB.

    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.sock
  • Policy Configuration (Read, Write, Batch and Info) - These policies can be set via getters and setter in the php code. On creation of an object of that policy class (eg, WritePolicy), the default values are initialized for that policy & can be overidden with associated setters. For example:

    // 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);

Documentation

Issues

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.

Usage

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

aerospike/aerospike-php 适用场景与选型建议

aerospike/aerospike-php 是一款 基于 Rust 开发的 Composer 扩展包,目前已累计 14.29k 次下载、GitHub Stars 达 16, 最近一次更新时间为 2023 年 09 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「extension」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 aerospike/aerospike-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 aerospike/aerospike-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 aerospike/aerospike-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 14.29k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 16
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 16
  • Watchers: 42
  • Forks: 8
  • 开发语言: Rust

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2023-09-15