Skip to content

Commit

Permalink
新增本地连接上下文存储处理器:ConnectContextLocal,可用于 SWOOLE_BASE 模式
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Jan 17, 2020
1 parent 8bcd318 commit 10275a6
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 2 deletions.
98 changes: 98 additions & 0 deletions src/Server/ConnectContext/StoreHandler/Local.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
namespace Imi\Server\ConnectContext\StoreHandler;

use Imi\Lock\Lock;
use Imi\Bean\Annotation\Bean;
use Imi\Server\ConnectContext\StoreHandler\IHandler;

/**
* 连接上下文存储处理器-Local
* @Bean("ConnectContextLocal")
*/
class Local implements IHandler
{
/**
* 存储集合
*
* @var array
*/
private $storeMap = [];

/**
* 锁 ID
*
* @var string
*/
protected $lockId;

/**
* 读取数据
*
* @param string $key
* @return array
*/
public function read(string $key): array
{
var_dump($key);
return $this->storeMap[$key] ?? [];
}

/**
* 保存数据
*
* @param string $key
* @param array $data
* @return void
*/
public function save(string $key, array $data)
{
$this->storeMap[$key] = $data;
}

/**
* 销毁数据
*
* @param string $key
* @return void
*/
public function destroy(string $key)
{
if(isset($this->storeMap[$key]))
{
unset($this->storeMap[$key]);
}
}

/**
* 数据是否存在
*
* @param string $key
* @return void
*/
public function exists(string $key)
{
return isset($this->storeMap[$key]);
}

/**
* 加锁
*
* @param callable $callable
* @return boolean
*/
public function lock($callable = null)
{
return Lock::lock($this->lockId, $callable);
}

/**
* 解锁
*
* @return boolean
*/
public function unlock()
{
return Lock::unlock($this->lockId);
}

}
6 changes: 5 additions & 1 deletion tests/unit/HttpServer/Http2TestServer/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
],
],
'ConnectContextStore' => [
'handlerClass' => \Imi\Server\ConnectContext\StoreHandler\MemoryTable::class,
'handlerClass' => \Imi\Server\ConnectContext\StoreHandler\Redis::class,
],
'ConnectContextRedis' => [
'redisPool' => 'redis',
'lockId' => 'redisConnectContextLock',
],
'ConnectContextMemoryTable' => [
'tableName' => 'connectContext',
Expand Down
1 change: 1 addition & 0 deletions tests/unit/HttpServer/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
'type' => Imi\Server\Type::HTTP,
'host' => '127.0.0.1',
'port' => 13000,
'mode' => SWOOLE_BASE,
'configs' => [
'worker_num' => 4,
'task_worker_num' => 1,
Expand Down
5 changes: 4 additions & 1 deletion tests/unit/WebSocketServer/MainServer/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@
'ConnectContextRedis' => [
'redisPool' => 'redis',
],
'ConnectContextLocal' => [
'lockId' => 'redisConnectContextLock',
],
'ConnectContextStore' => [
'handlerClass' => \Imi\Server\ConnectContext\StoreHandler\MemoryTable::class,
'handlerClass' => \Imi\Server\ConnectContext\StoreHandler\Local::class,
],
'ConnectContextMemoryTable' => [
'tableName' => 'connectContext',
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/WebSocketServer/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@
'atomicName' => 'atomic1',
],
],
'redisConnectContextLock' => [
'class' => 'RedisLock',
'options' => [
'poolName' => 'redis',
],
],
]
],
];

0 comments on commit 10275a6

Please sign in to comment.