From 8f54e93b81749feaec601e0cea58f9f0d9b40a87 Mon Sep 17 00:00:00 2001 From: zxin <14545600+NHZEX@users.noreply.github.com> Date: Tue, 9 Apr 2024 08:16:49 +0800 Subject: [PATCH 1/2] =?UTF-8?q?[3.0]=20=E4=BF=AE=E5=A4=8D`Cache`=E5=89=8D?= =?UTF-8?q?=E7=BC=80=E9=80=89=E9=A1=B9=E4=B8=8E`setMultiple`=E4=B8=8D?= =?UTF-8?q?=E5=85=BC=E5=AE=B9=E9=97=AE=E9=A2=98=20(#688)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update: 修复`Cache`前缀选项与`setMultiple`不兼容问题 * Update: 优化实现 * Update: 优化实现 * Update: 优化实现 --- src/Cache/Handler/Redis.php | 22 +++++----- tests/unit/Component/Tests/CacheRedisTest.php | 40 +++++++++++++++++++ 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/src/Cache/Handler/Redis.php b/src/Cache/Handler/Redis.php index 7999a37729..432f8c2f20 100644 --- a/src/Cache/Handler/Redis.php +++ b/src/Cache/Handler/Redis.php @@ -77,11 +77,14 @@ public function clear(): bool */ public function getMultiple(iterable $keys, mixed $default = null): iterable { - foreach ($keys as &$key) + $newKeys = []; + $parsedKeys = []; + foreach ($keys as $key) { - $key = $this->parseKey($key); + $parsedKeys[] = $this->parseKey($key); + $newKeys[] = $key; } - $mgetResult = ImiRedis::use(static fn (\Imi\Redis\RedisHandler $redis) => $redis->mget($keys), $this->poolName, true); + $mgetResult = ImiRedis::use(static fn (\Imi\Redis\RedisHandler $redis) => $redis->mget($parsedKeys), $this->poolName, true); $result = []; if ($mgetResult) { @@ -89,11 +92,11 @@ public function getMultiple(iterable $keys, mixed $default = null): iterable { if (false === $v) { - $result[$keys[$i]] = $default; + $result[$newKeys[$i]] = $default; } else { - $result[$keys[$i]] = $this->decode($v); + $result[$newKeys[$i]] = $this->decode($v); } } } @@ -114,21 +117,22 @@ public function setMultiple(iterable $values, null|int|\DateInterval $ttl = null { $setValues = $values; } + $values = []; foreach ($setValues as $k => $v) { - $setValues[$this->parseKey((string) $k)] = $this->encode($v); + $values[$this->parseKey((string) $k)] = $this->encode($v); } // ttl 支持 \DateInterval 格式 if ($ttl instanceof \DateInterval) { $ttl = DateTime::getSecondsByInterval($ttl); } - $result = ImiRedis::use(static function (\Imi\Redis\RedisHandler $redis) use ($setValues, $ttl) { + $result = ImiRedis::use(static function (\Imi\Redis\RedisHandler $redis) use ($values, $ttl) { $redis->multi(); - $redis->mset($setValues); + $redis->mset($values); if (null !== $ttl) { - foreach ($setValues as $k => $v) + foreach ($values as $k => $v) { $redis->expire((string) $k, $ttl); } diff --git a/tests/unit/Component/Tests/CacheRedisTest.php b/tests/unit/Component/Tests/CacheRedisTest.php index 1f84884d54..31c44aaa8f 100644 --- a/tests/unit/Component/Tests/CacheRedisTest.php +++ b/tests/unit/Component/Tests/CacheRedisTest.php @@ -4,10 +4,50 @@ namespace Imi\Test\Component\Tests; +use Imi\Bean\BeanFactory; +use Imi\Cache\Handler\Redis; +use PHPUnit\Framework\Assert; + /** * @testdox Cache Redis Handler */ class CacheRedisTest extends BaseCacheTestCase { protected string $cacheName = 'redis'; + + public function testMultipleWithPrefix(): void + { + $cache = BeanFactory::newInstance(Redis::class, [ + 'poolName' => 'redis_test', + 'prefix' => 'imi-test:', + 'formatHandlerClass' => \Imi\Util\Format\Json::class, + ]); + + $value = bin2hex(random_bytes(8)); + $values = [ + 'k1' => 'v1' . $value, + 'k2' => 'v2' . $value, + 'k3' => 'v3' . $value, + ]; + Assert::assertTrue($cache->setMultiple($values)); + $getValues = $cache->getMultiple([0 => 'k1', 2 => 'k2', 'A' => 'k3']); + Assert::assertEquals($values, $getValues); + + $this->go(static function () use ($cache): void { + $value = bin2hex(random_bytes(8)); + + $values = [ + 'k1' => 'v1' . $value, + 'k2' => 'v2' . $value, + ]; + Assert::assertTrue($cache->setMultiple($values, 1)); + $getValues = $cache->getMultiple(array_keys_string($values)); + Assert::assertEquals($values, $getValues); + sleep(2); + Assert::assertEquals([ + 'k1' => 'none', + 'k2' => 'none', + ], $cache->getMultiple(array_keys_string($values), 'none')); + }, null, 3); + } } From f687b084e4fbc1882ff8bad9faffbcbb971e3ccf Mon Sep 17 00:00:00 2001 From: zxin <14545600+NHZEX@users.noreply.github.com> Date: Tue, 9 Apr 2024 08:19:29 +0800 Subject: [PATCH 2/2] =?UTF-8?q?[3.0]=20=E6=9B=B4=E6=96=B0=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=20CI=20(#689)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update: 更新 CI docker 的使用 * Update: 按照组件重要程度调整测试顺序 * Update: 添加 php8.3 预构建 * Update: 更新 CI 弃用 * Update: 更新 CI 弃用 --- .github/actions/ci-prepare/action.yml | 2 +- .github/workflows/ci.yml | 72 ++++++++++++------------- .github/workflows/codecov.yml | 2 +- .github/workflows/daily-image-build.yml | 1 + .github/workflows/daily-test.yml | 4 +- .github/workflows/phpcs.yml | 6 +-- .github/workflows/phpstan.yml | 6 +-- .github/workflows/rector.yml | 6 +-- 8 files changed, 50 insertions(+), 49 deletions(-) diff --git a/.github/actions/ci-prepare/action.yml b/.github/actions/ci-prepare/action.yml index 2e694ebfe7..bf3847bec2 100644 --- a/.github/actions/ci-prepare/action.yml +++ b/.github/actions/ci-prepare/action.yml @@ -12,7 +12,7 @@ runs: - name: Setup Services shell: bash run: | - docker-compose -f ./.github/docker-compose.yml up -d ${{ inputs.env }} + docker compose -f ./.github/docker-compose.yml up -d ${{ inputs.env }} docker exec ${{ inputs.env }} php -v docker exec ${{ inputs.env }} php -m docker exec ${{ inputs.env }} php --ri swoole diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2545a4dcce..08f3fb8914 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -45,7 +45,7 @@ jobs: echo "REPOSITORY_OWNER=${REPOSITORY_OWNER,,}" >>${GITHUB_ENV} mkdir -p /tmp/base_cache/composer - name: Cache dependencies - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: /tmp/base_cache/composer key: ${{ runner.os }}-composer-${{ env.IMAGE_VERSION }}-${{ hashFiles('*/composer.json', 'src/Components/*/composer.json') }} @@ -59,6 +59,12 @@ jobs: - name: Test if: ${{ env.test_prepared && always() }} run: docker exec ${ENV_SERVICE} composer test + - name: Test connection-center + if: ${{ env.test_prepared && always() }} + run: docker exec ${ENV_SERVICE} composer test-connection-center + - name: Test database + if: ${{ env.test_prepared && always() }} + run: docker exec ${ENV_SERVICE} composer test-database - name: Test swoole if: ${{ env.test_prepared && always() }} run: docker exec ${ENV_SERVICE} composer test-swoole @@ -104,12 +110,6 @@ jobs: - name: Test phar if: ${{ env.test_prepared && always() }} run: docker exec ${ENV_SERVICE} composer test-phar - - name: Test connection-center - if: ${{ env.test_prepared && always() }} - run: docker exec ${ENV_SERVICE} composer test-connection-center - - name: Test database - if: ${{ env.test_prepared && always() }} - run: docker exec ${ENV_SERVICE} composer test-database - name: Test model if: ${{ env.test_prepared && always() }} run: docker exec ${ENV_SERVICE} composer test-model @@ -142,7 +142,7 @@ jobs: echo "REPOSITORY_OWNER=${REPOSITORY_OWNER,,}" >>${GITHUB_ENV} mkdir -p /tmp/base_cache/composer - name: Cache dependencies - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: /tmp/base_cache/composer key: ${{ runner.os }}-composer-${{ env.IMAGE_VERSION }}-${{ hashFiles('*/composer.json', 'src/Components/*/composer.json') }} @@ -156,6 +156,12 @@ jobs: - name: Test if: ${{ env.test_prepared && always() }} run: docker exec ${ENV_SERVICE} composer test + - name: Test connection-center + if: ${{ env.test_prepared && always() }} + run: docker exec ${ENV_SERVICE} composer test-connection-center + - name: Test database + if: ${{ env.test_prepared && always() }} + run: docker exec ${ENV_SERVICE} composer test-database - name: Test swoole if: ${{ env.test_prepared && always() }} run: docker exec ${ENV_SERVICE} composer test-swoole @@ -198,12 +204,6 @@ jobs: - name: Test pgsql if: ${{ env.test_prepared && always() }} run: docker exec ${ENV_SERVICE} composer test-pgsql - - name: Test connection-center - if: ${{ env.test_prepared && always() }} - run: docker exec ${ENV_SERVICE} composer test-connection-center - - name: Test database - if: ${{ env.test_prepared && always() }} - run: docker exec ${ENV_SERVICE} composer test-database - name: Test model if: ${{ env.test_prepared && always() }} run: docker exec ${ENV_SERVICE} composer test-model @@ -228,7 +228,7 @@ jobs: shell: bash run: | sudo apt update && sudo apt install -y rsync - docker-compose -f ./.github/docker-compose.yml up -d mysql postgres redis rabbitmq kafka1 + docker compose -f ./.github/docker-compose.yml up -d mysql postgres redis rabbitmq kafka1 echo "127.0.0.1 kafka1" | sudo tee -a /etc/hosts - name: Setup swoole-cli run: .github/script/install-swoole-cli.sh ${{ matrix.swoole-cli }} @@ -243,7 +243,7 @@ jobs: id: composer-cache run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT - name: Cache dependencies - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: ${{ steps.composer-cache.outputs.dir }} key: ${{ runner.os }}-composer-swoole-cli-${{ matrix.swoole-cli }}-${{ hashFiles('*/composer.json', 'src/Components/*/composer.json') }} @@ -270,6 +270,12 @@ jobs: - name: Test if: ${{ env.test_prepared && always() }} run: composer test + - name: Test connection-center + if: ${{ env.test_prepared && always() }} + run: composer test-connection-center + - name: Test database + if: ${{ env.test_prepared && always() }} + run: composer test-database - name: Test swoole if: ${{ env.test_prepared && always() }} run: composer test-swoole @@ -309,12 +315,6 @@ jobs: - name: Test phar if: ${{ env.test_prepared && always() }} run: composer test-phar - - name: Test connection-center - if: ${{ env.test_prepared && always() }} - run: composer test-connection-center - - name: Test database - if: ${{ env.test_prepared && always() }} - run: composer test-database - name: Test model if: ${{ env.test_prepared && always() }} run: composer test-model @@ -381,7 +381,7 @@ jobs: id: composer-cache run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT - name: Cache dependencies - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: ${{ steps.composer-cache.outputs.dir }} key: ${{ runner.os }}-composer-${{ env.php-versions }}-${{ hashFiles('*/composer.json', 'src/Components/*/composer.json') }} @@ -412,6 +412,12 @@ jobs: - name: Test if: ${{ env.test_prepared && always() }} run: composer test + - name: Test connection-center + if: ${{ env.test_prepared && always() }} + run: composer test-connection-center + - name: Test database + if: ${{ env.test_prepared && always() }} + run: composer test-database - name: Test swoole if: ${{ env.test_prepared && always() }} run: composer test-swoole @@ -448,12 +454,6 @@ jobs: - name: Test phar if: ${{ env.test_prepared && always() }} run: composer test-phar - - name: Test connection-center - if: ${{ env.test_prepared && always() }} - run: composer test-connection-center - - name: Test database - if: ${{ env.test_prepared && always() }} - run: composer test-database - name: Test model if: ${{ env.test_prepared && always() }} run: composer test-model @@ -512,7 +512,7 @@ jobs: "dir=$CacheDir" >> $env:GITHUB_OUTPUT - name: Cache dependencies - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: ${{ steps.composer-cache.outputs.dir }} key: ${{ runner.os }}-composer-${{ matrix.php }}-${{ hashFiles('*/composer.json', 'src/Components/*/composer.json') }} @@ -544,6 +544,12 @@ jobs: - name: Test if: ${{ env.test_prepared && always() }} run: composer test + - name: Test connection-center + if: ${{ env.test_prepared && always() }} + run: composer test-connection-center-common + - name: Test database + if: ${{ env.test_prepared && always() }} + run: composer test-database - name: Test fpm if: ${{ env.test_prepared && always() }} run: composer test-fpm @@ -562,12 +568,6 @@ jobs: - name: Test snowflake if: ${{ env.test_prepared && always() }} run: composer test-snowflake - - name: Test connection-center - if: ${{ env.test_prepared && always() }} - run: composer test-connection-center-common - - name: Test database - if: ${{ env.test_prepared && always() }} - run: composer test-database - name: Test model if: ${{ env.test_prepared && always() }} run: composer test-model diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index eb035c96a6..c669235027 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -147,7 +147,7 @@ jobs: id: composer-cache run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT - name: Cache vendor - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: ${{ steps.composer-cache.outputs.dir }} key: ${{ runner.os }}-composer-${{ env.PHP_VERSION }}-${{ env.SWOOLE_VERSION }}-${{ hashFiles('*/composer.json', 'src/Components/*/composer.json') }} diff --git a/.github/workflows/daily-image-build.yml b/.github/workflows/daily-image-build.yml index 407645f1e3..382c097177 100644 --- a/.github/workflows/daily-image-build.yml +++ b/.github/workflows/daily-image-build.yml @@ -25,6 +25,7 @@ jobs: strategy: matrix: image: + - {tag: php8.3-swoole-5.1, version: 5.1-php8.3} - {tag: php8.2-swoole-5.1, version: 5.1-php8.2} - {tag: php8.1-swoole-5.1, version: 5.1-php8.1} - {tag: php8.2-swoole-5.0, version: 5.0-php8.2} diff --git a/.github/workflows/daily-test.yml b/.github/workflows/daily-test.yml index 5db937a692..a90c8bb8f6 100644 --- a/.github/workflows/daily-test.yml +++ b/.github/workflows/daily-test.yml @@ -36,7 +36,7 @@ jobs: with: ref: "3.0" - name: Cache dependencies - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: /tmp/base_cache/composer key: ${{ runner.os }}-daily-test-3.0-composer-${{ matrix.php }}-${{ hashFiles('*/composer.json', 'src/Components/*/composer.json') }} @@ -133,7 +133,7 @@ jobs: with: ref: "2.1" - name: Cache dependencies - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: /tmp/base_cache/composer key: ${{ runner.os }}-daily-test-2.1-composer-${{ matrix.php }}-${{ hashFiles('*/composer.json', 'src/Components/*/composer.json') }} diff --git a/.github/workflows/phpcs.yml b/.github/workflows/phpcs.yml index e74f3ce87a..a13fada701 100644 --- a/.github/workflows/phpcs.yml +++ b/.github/workflows/phpcs.yml @@ -33,7 +33,7 @@ jobs: mkdir -p /tmp/base_cache/phpcsfixer - name: Cache dependencies - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: /tmp/base_cache/composer key: ${{ runner.os }}-composer-${{ env.IMAGE_VERSION }}-${{ hashFiles('*/composer.json', 'src/Components/*/composer.json') }} @@ -42,7 +42,7 @@ jobs: ${{ runner.os }}-composer- - name: Cache phpcsfixer - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: /tmp/base_cache/phpcsfixer key: ${{ runner.os }}-phpcsfixer-${{ env.IMAGE_VERSION }}-${{ hashFiles('*/.php-cs-fixer.php') }} @@ -53,7 +53,7 @@ jobs: - name: Prepare2 run: | echo "::group::Env prepare" - docker-compose -f ./.github/docker-compose.yml up -d ${ENV_SERVICE} + docker compose -f ./.github/docker-compose.yml up -d ${ENV_SERVICE} echo "::endgroup::" echo "::group::Env info" docker exec ${ENV_SERVICE} php -v diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index 854b497b41..e018282367 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -37,7 +37,7 @@ jobs: mkdir -p /tmp/base_cache/phpstan - name: Cache dependencies - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: /tmp/base_cache/composer key: ${{ runner.os }}-composer-${{ env.IMAGE_VERSION }}-${{ hashFiles('*/composer.json', 'src/Components/*/composer.json') }} @@ -46,7 +46,7 @@ jobs: ${{ runner.os }}-composer- - name: Cache phpstan - uses: actions/cache@v3 + uses: actions/cache@v4 if: ${{ false }} with: path: /tmp/base_cache/phpstan @@ -61,7 +61,7 @@ jobs: - name: Prepare2 run: | echo "::group::Env prepare" - docker-compose -f ./.github/docker-compose.yml up -d ${ENV_SERVICE} + docker compose -f ./.github/docker-compose.yml up -d ${ENV_SERVICE} echo "::endgroup::" echo "::group::Env info" docker exec ${ENV_SERVICE} php -v diff --git a/.github/workflows/rector.yml b/.github/workflows/rector.yml index 008738df55..c6ea13c4d9 100644 --- a/.github/workflows/rector.yml +++ b/.github/workflows/rector.yml @@ -34,7 +34,7 @@ jobs: mkdir -p /tmp/base_cache/rector - name: Cache dependencies - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: /tmp/base_cache/composer key: ${{ runner.os }}-composer-${{ env.IMAGE_VERSION }}-${{ hashFiles('*/composer.json', 'src/Components/*/composer.json') }} @@ -45,7 +45,7 @@ jobs: - name: Prepare2 run: | echo "::group::Env prepare" - docker-compose -f ./.github/docker-compose.yml up -d ${ENV_SERVICE} + docker compose -f ./.github/docker-compose.yml up -d ${ENV_SERVICE} echo "::endgroup::" echo "::group::Env info" docker exec ${ENV_SERVICE} php -v @@ -61,7 +61,7 @@ jobs: echo "test_prepared=1" >> $GITHUB_ENV - name: Cache Rector - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: /tmp/base_cache/rector key: ${{ runner.os }}-rector-${{ env.IMAGE_VERSION }}-${{ github.run_id }}