我为PHP应用程序编写了一个解决方案,主要解决 2 类问题:
- 如何在托管在相同/不同服务器上的 PHP 进程之间共享数据/变量
- 如何同步数据/变量中的读/写操作
我的项目托管在 GitHub ANYEM 项目中
首先:使用命令行启动ANYEM_SERVER
php ANYEM/ANYEM_SERVER/anyem.app.server.impl/ServerImpl.php
现在,在您的 PHP 应用程序中,您可以执行以下操作:
<?php
// load server's connection configuration (ANYEM_SERVER IP and Port Number ...)
$clientConnection = ClientConnectionImpl::newClient();
// build a key for your variable that will be stored in server
// the key is composed on 3 Parts : [1] => URL, [2] => Variable Namespace, [3] => Variable Name
$identifier = new ResourceIdentifierImpl("anyem.com", "anyemNameSpace", "a");
$a = 5;
$anyemClient = new AnyemClientImpl($clientConnection, $identifier);
try {
// if $a is reserved by another PHP Process, so this process
// will sleep (1/10) seconds and retry the reservation of the resource
// here, the max number of reservation attempts is 5, if reservation
// fails, an Exception will be thrown
$responseWrapper = $anyemClient->get($a, 5, 100000);
// here we have reserved the variable $a, and we have the unserialized contents
$a = $responseWrapper->getResource()->getData();
// here, we update the contents, send it to ANYEM_SERVER and releasing the lock (we unreserve it)
$anyemClient->put(++$a);
}
catch (Exception $e) {
print $e->getMessage() . "\n";
continue;
}
希望可以帮助某人:)