8000 新增redis单测和重构 by stelin · Pull Request #69 · swoft-cloud/swoft · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

新增redis单测和重构 #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 93 additions & 11 deletions app/Controllers/RedisController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@


use Swoft\Bean\Annotation\Controller;
use Swoft\Bean\Annotation\RequestMapping;
use Swoft\Cache\Redis\RedisClient;
use Swoft\Bean\Annotation\Inject;
use Swoft\Cache\Redis\CacheRedis;
use Swoft\Cache\Cache;


/**
Expand All @@ -19,18 +20,99 @@
class RedisController
{
/**
* @RequestMapping()
* @return bool|string
* @Inject("cache")
* @var Cache
*/
public function test()
private $cache;

/**
* @Inject()
* @var CacheRedis
*/
private $redis;

public function testCache()
{
$setResult = RedisClient::set('test', 123321);
$getResult = RedisClient::get('test');
$result = $this->cache->set('name', 'stelin');
$name = $this->cache->get('name');

return [
'setResult' => $setResult,
'getResult' => $getResult
];
return [$result, $name];
}

public function testRedis()
{
$result = $this->redis->set('nameRedis', 'stelin2');
$name = $this->redis->get('nameRedis');

return [$result, $name];
}

public function testFunc()
{
$result = cache()->set('nameFunc', 'stelin3');
$name = cache()->get('nameFunc');

return [$result, $name];
}

public function testFunc2()
{
$result = cache()->set('nameFunc2', 'stelin3');
$name = cache('nameFunc2');
$name2 = cache('nameFunc3', 'value3');

return [$result, $name, $name2];
}

public function testDelete()
{
$result = $this->cache->set('name', 'stelin');
$del = $this->cache->delete('name');

return [$result, $del];
}

public function clear()
{
$result = $this->cache->clear();

return [$result];
}

public function setMultiple()
{
$result = $this->cache->setMultiple(['name6' => 'stelin6', 'name8' => 'stelin8']);
$ary = $this->cache->getMultiple(['name6', 'name8']);

return [$result, $ary];
}

public function deleteMultiple()
{
$result = $this->cache->setMultiple(['name6' => 'stelin6', 'name8' => 'stelin8']);
$ary = $this->cache->deleteMultiple(['name6', 'name8']);

return [$result, $ary];
}

public function has()
{
$result = $this->cache->set("name666", 'stelin666');
$ret = $this->cache->has('name666');

return [$result, $ret];
}

public function testDefer()
{
$ret1 = $this->redis->deferCall('set', ['name1', 'stelin1']);
$ret2 = $this->redis->deferCall('set', ['name2', 'stelin2']);

$r1 = $ret1->getResult();
$r2 = $ret2->getResult();

$ary = $this->redis->getMultiple(['name1', 'name2']);

return [$r1, $r2, $ary];
}
}
6 changes: 2 additions & 4 deletions app/Tasks/TestTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Swoft\Bean\Annotation\Inject;
use Swoft\Bean\Annotation\Scheduled;
use Swoft\Bean\Annotation\Task;
use Swoft\Cache\Redis\RedisClient;
use Swoft\Db\EntityManager;
use Swoft\Http\HttpClient;
use Swoft\Service\Service;
Expand Down Expand Up @@ -49,8 +48,7 @@ public function corTask($p1, $p2)
$status++;
echo "this cor task \n";
App::trace("this is task log");
// RedisClient::set('name', 'stelin boy');
$name = RedisClient::get('name');
$name = cache()->get('name');
return 'cor' . " $p1" . " $p2 " . $status . " " . $name;
}

Expand Down Expand Up @@ -129,7 +127,7 @@ public function asyncTask()
static $status = 1;
$status++;
echo "this async task \n";
$name = RedisClient::get('name');
$name = cache()->get('name');
App::trace("this is task log");
return 'async-' . $status . '-' . $name;
}
Expand Down
4 changes: 4 additions & 0 deletions config/beans/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@
'eventManager' => [
'class' => \Swoft\Event\EventManager::class,
],
'cache' => [
'class' => \Swoft\Cache\Cache::class,
'driver' => 'redis',
]
];
15 changes: 8 additions & 7 deletions config/properties/cache.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
<?php
return [
'redis' => [
'name' => 'redis',
'name' => 'redis',
"uri" => [
'127.0.0.1:6379',
'127.0.0.1:6379',
],
"maxIdel" => 8,
"maxActive" => 8,
"maxWait" => 8,
"timeout" => 8,
"balancer" => 'random',
"useProvider" => false,
'maxIdel' => 8,
'maxActive' => 8,
'maxWait' => 8,
'timeout' => 8,
'balancer' => 'random',
'useProvider' => false,
'provider' => 'consul',
'db' => 1,
],
];
95 changes: 86 additions & 9 deletions test/Web/RedisControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,96 @@
class RedisControllerTest extends AbstractTestCase
{

/**
* @test
* @requires extension redis
* @covers \App\Controllers\RedisController
*/
public function testTest()
public function testCache()
{
$expected = [
'setResult' => true,
'getResult' => 123321
true,
'stelin',
];
$response = $this->request('GET', '/redis/test', [], parent::ACCEPT_JSON);
$response = $this->request('GET', '/redis/testCache', [], parent::ACCEPT_JSON);
$response->assertSuccessful()->assertJson($expected);
}

public function testRedis()
{
$expected = [
true,
'stelin2',
];
$response = $this->request('GET', '/redis/testRedis', [], parent::ACCEPT_JSON);
$response->assertSuccessful()->assertJson($expected);
}

public function testFunc()
{
$expected = [
true,
'stelin3',
];
$response = $this->request('GET', '/redis/testFunc', [], parent::ACCEPT_JSON);
$response->assertSuccessful()->assertJson($expected);
}

public function testFunc2()
{
$expected = [
true,
'stelin3',
'value3'
];
$response = $this->request('GET', '/redis/testFunc2', [], parent::ACCEPT_JSON);
$response->assertSuccessful()->assertJson($expected);
}

public function testDelete()
{
$expected = [
true,
1,
];
$response = $this->request('GET', '/redis/testDelete', [], parent::ACCEPT_JSON);
$response->assertSuccessful()->assertJson($expected);
}

public function testClear()
{
$expected = [
true,
];
$response = $this->request('GET', '/redis/clear', [], parent::ACCEPT_JSON);
$response->assertSuccessful()->assertJson($expected);
}

public function testMultiple()
{
$expected = [
true,
[
'stelin6',
'stelin8',
],
];
$response = $this->request('GET', '/redis/setMultiple', [], parent::ACCEPT_JSON);
$response->assertSuccessful()->assertJson($expected);
}

public function testDeleteMultiple()
{
$expected = [
true,
2,
];
$response = $this->request('GET', '/redis/deleteMultiple', [], parent::ACCEPT_JSON);
$response->assertSuccessful()->assertJson($expected);
}

public function testHas()
{
$expected = [
true,
true,
];
$response = $this->request('GET', '/redis/has', [], parent::ACCEPT_JSON);
$response->assertSuccessful()->assertJson($expected);
}
}
6 changes: 3 additions & 3 deletions test/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require_once dirname(dirname(__FILE__)) . '/config/define.php';

// init
\Swoft\App::$isInTest = true;

$server = new \Swoft\Server\HttpServer();
\Swoft\Bean\BeanFactory::reload([
'application' => [
Expand All @@ -11,6 +13,4 @@
],
]);
$initApplicationContext = new \Swoft\Base\InitApplicationContext();
$initApplicationContext->init();

\Swoft\App::$isInTest = true;
$initApplicationContext->init();
0