Skip to content

Commit

Permalink
优化一下代码
Browse files Browse the repository at this point in the history
  • Loading branch information
doyouhaobaby committed Jan 28, 2024
1 parent 5a017bb commit 1ec4ad8
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 51 deletions.
40 changes: 5 additions & 35 deletions app/Controller/Api/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
namespace App\Controller\Api;

use Leevel\Database\Proxy\Db;
use Swoole\Coroutine\Barrier;
use Swoole\Coroutine;

/**
* 查询数据库.
Expand All @@ -20,38 +18,10 @@ class Database
*/
public function handle(): array
{
// go(function() {
// $x = Db::table('test')->findCount();
// });
// go(function() {
// $x1 = Db::table('test')->findCount();
// });
// go(function() {
// $x2 = Db::table('test')->findCount();
// });

// $barrier = Barrier::make();
//
// $count = 0;
// $N = 4;
// $result = [];
//
// foreach (range(1, $N) as $i) {
// Coroutine::create(function () use ($barrier, &$count, &$result) {
// //sleep(1);
// $result[] = Db::table('test')->findCount();
// $count++;
// });
// }
//
// Barrier::wait($barrier);

Db::transaction(function() {
Db::table('test')->where('id',5)->find();
Db::table('test')->insert(['name' => 'neww']);
Db::table('test')->insert(['name' => 'new33w222']);
});

return ['count' => 1111];
return [
'count' => Db::connect('common')
->table('test')
->findCount()
];
}
}
7 changes: 2 additions & 5 deletions app/Infra/GenerateDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Infra;

use Leevel\Cache\Manager;
use Leevel\Cache\Redis;
use Leevel\Support\Dto;

Expand Down Expand Up @@ -74,11 +75,7 @@ public function handle(\Closure $sourceNext = null): string

protected function getRedisSequence(): RedisSequence
{
/** @var Redis $phpRedis */
$phpRedis = \App::make('caches')->connect('redis');

/** @var \Redis $redis */
$redis = $phpRedis->getHandle();
$redis = redis_cache();

return (new RedisSequence($redis))->setCachePrefix('redis_sequence:'.$this->guid.':');
}
Expand Down
14 changes: 3 additions & 11 deletions app/Infra/Provider/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Infra\PermissionCache;
use Godruoyi\Snowflake\RedisSequenceResolver;
use Godruoyi\Snowflake\Snowflake;
use Leevel\Cache\Manager;
use Leevel\Cache\Redis;
use Leevel\Di\IContainer;
use Leevel\Di\Provider;
Expand Down Expand Up @@ -72,11 +73,7 @@ private function permission(): void
private function redisSequence(): void
{
$this->container->singleton('redis_sequence', function (IContainer $container): RedisSequenceResolver {
/** @var Redis $phpRedis */
$phpRedis = \App::make('caches')->connect('redis');

/** @var \Redis $redis */
$redis = $phpRedis->getHandle();
$redis = redis_cache();

return (new RedisSequenceResolver($redis))->setCachePrefix('redis_sequence:');
});
Expand All @@ -85,12 +82,7 @@ private function redisSequence(): void
private function snowflake(): void
{
$this->container->singleton('snowflake', function (IContainer $container): Snowflake {
/** @var Redis $phpRedis */
$phpRedis = \App::make('caches')->connect('redis');

/** @var \Redis $redis */
$redis = $phpRedis->getHandle();

$redis = redis_cache();
$redisSequence = (new RedisSequenceResolver($redis))->setCachePrefix('snowflake_redis_sequence:');

return (new Snowflake(1, 1))
Expand Down
19 changes: 19 additions & 0 deletions app/Infra/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use App\Infra\Service\ApiQL\ApiQLUpdate;
use App\Infra\Service\ApiQL\ApiQLUpdateParams;
use App\Infra\Service\Support\ReadParams;
use Leevel\Cache\Manager;
use Leevel\Cache\Redis;
use Leevel\Config\Proxy\Config;
use Leevel\Database\Ddd\Entity;
use Leevel\Database\Ddd\UnitOfWork;
Expand Down Expand Up @@ -1368,3 +1370,20 @@ function api_ql_prepare(
return $data;
}
}

if (!function_exists('redis_cache')) {
/**
* 获取 redis.
*/
function redis_cache(): \Redis
{
/** @var Manager $manager */
$manager = \App::make('caches');

/** @var Redis $phpRedis */
$phpRedis = $manager->connect('redis');

// @phpstan-ignore-next-line
return $phpRedis->getHandle();
}
}
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ parameters:
checkGenericClassInNonGenericObjectType: false
excludePaths:
- %currentWorkingDirectory%/*Test.php
- %currentWorkingDirectory%/app/X
ignoreErrors:
- '#Access to an undefined property [\S\s]+::\$[\S\s]+\.#'
- '#should return [\S\s]+ but returns Leevel\\Database\\Ddd\\Entity\.#'
Expand Down

0 comments on commit 1ec4ad8

Please sign in to comment.