From 4208475497d8def73ba2d7db0f69332e6d603644 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=99=AF=E6=A3=AE?= Date: Wed, 10 Jun 2026 15:18:23 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=8E=BB=E6=8E=89=20SettingServiceImpl?= =?UTF-8?q?=20=E8=BF=9B=E7=A8=8B=E7=A7=81=E6=9C=89=E7=BC=93=E5=AD=98?= =?UTF-8?q?=EF=BC=8C=E6=94=B9=E7=94=A8=20RowStrategy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 10 ++-- src/Setting/Dao/Impl/SettingDaoImpl.php | 10 +++- .../Service/Impl/SettingServiceImpl.php | 36 +---------- tests/Setting/SettingServiceTest.php | 59 +++++++++++++++++++ 4 files changed, 73 insertions(+), 42 deletions(-) diff --git a/composer.json b/composer.json index f165e53a..c4421a94 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "symfony/finder": "^5.4.35", "symfony/console": "^5.4.35", "symfony/lock": "^5.4.35", - "doctrine/common": "3.0.3", + "doctrine/common": "^2.6", "doctrine/dbal": "~2.4", "doctrine/annotations": "~1.2", "davedevelopment/phpmig": "~1.2", @@ -34,11 +34,9 @@ "ext-json": "*" }, "require-dev": { - "mockery/mockery": "^0.9.9", - "phpunit/phpunit": "^9.1 || ^10.1", - "ramsey/uuid": "^3.9", - "phpspec/prophecy": "^1.18", - "phpspec/prophecy-phpunit": "^2.1" + "mockery/mockery": "^1.3", + "phpunit/phpunit": "^8.5", + "ramsey/uuid": "^3.9" }, "config": { "bin-dir": "bin" diff --git a/src/Setting/Dao/Impl/SettingDaoImpl.php b/src/Setting/Dao/Impl/SettingDaoImpl.php index 2e11e917..66c70ab4 100644 --- a/src/Setting/Dao/Impl/SettingDaoImpl.php +++ b/src/Setting/Dao/Impl/SettingDaoImpl.php @@ -4,11 +4,19 @@ use Codeages\Biz\Framework\Setting\Dao\SettingDao; use Codeages\Biz\Framework\Dao\GeneralDaoImpl; +use Codeages\Biz\Framework\Dao\Annotation\CacheStrategy; +use Codeages\Biz\Framework\Dao\Annotation\RowCache; +/** + * @CacheStrategy("Row") + */ class SettingDaoImpl extends GeneralDaoImpl implements SettingDao { protected $table = 'biz_setting'; + /** + * @RowCache + */ public function getByName($name) { return $this->getByFields(array('name' => $name)); @@ -25,7 +33,7 @@ public function declares() { return array( 'serializes' => array('data' => 'php'), - 'cache' => 'table', + 'cache' => 'row', ); } } diff --git a/src/Setting/Service/Impl/SettingServiceImpl.php b/src/Setting/Service/Impl/SettingServiceImpl.php index c70d6dd0..8977492e 100644 --- a/src/Setting/Service/Impl/SettingServiceImpl.php +++ b/src/Setting/Service/Impl/SettingServiceImpl.php @@ -52,7 +52,6 @@ public function set($name, $data) 'data' => $data, )); } - $this->clearCache(); } public function remove($name) @@ -80,23 +79,11 @@ public function remove($name) } else { $this->getSettingDao()->delete($setting['id']); } - $this->clearCache(); } private function getByName($name) { - $settings = $this->getCache(); - if (!$settings) { - $settings = $this->getSettingDao()->findAll(); - $settings = array_column($settings, null, 'name'); - $this->setCache($settings); - } - - if (!isset($settings[$name])) { - return null; - } - - return $settings[$name]; + return $this->getSettingDao()->getByName($name); } private function splitName($name) @@ -119,25 +106,4 @@ protected function getSettingDao() { return $this->biz->dao('Setting:SettingDao'); } - - protected function getCache() - { - $storage = $this->biz['array_storage']; - if (!isset($storage['setting_service_cache'])) { - return null; - } - - return $storage['setting_service_cache']; - } - - protected function setCache($data) - { - $storage = $this->biz['array_storage']; - $storage['setting_service_cache'] = $data; - } - - protected function clearCache() - { - $this->biz['array_storage']->flush(); - } } diff --git a/tests/Setting/SettingServiceTest.php b/tests/Setting/SettingServiceTest.php index 0e0bb373..6d09ccec 100644 --- a/tests/Setting/SettingServiceTest.php +++ b/tests/Setting/SettingServiceTest.php @@ -134,6 +134,65 @@ public function testRemoveWithDotInvalidKeyValueType() $this->getSettingService()->remove('with_string_value.subkey'); } + public function testGetAfterSetReturnsNewValue() + { + $this->seed('Tests\\Setting\\SettingSeeder'); + + $this->getSettingService()->set('with_string_value', 'new_string_value'); + $value = $this->getSettingService()->get('with_string_value'); + + $this->assertEquals('new_string_value', $value); + } + + public function testRowCacheHitOnRepeatedGetByName() + { + if (empty($this->biz['dao.cache.enabled'])) { + $this->markTestSkipped('DAO cache is not enabled, skipping RowStrategy cache test.'); + } + + $this->seed('Tests\\Setting\\SettingSeeder'); + + $first = $this->getSettingService()->get('with_array_value'); + $second = $this->getSettingService()->get('with_array_value'); + + $this->assertEquals($first, $second); + $this->assertTrue(is_array($second)); + + $keys = $this->redis->keys('dao:biz_setting:getByName:*'); + $this->assertNotEmpty($keys, 'RowStrategy should cache getByName results in Redis'); + } + + public function testRowCacheInvalidationOnUpdate() + { + if (empty($this->biz['dao.cache.enabled'])) { + $this->markTestSkipped('DAO cache is not enabled, skipping RowStrategy cache test.'); + } + + $this->seed('Tests\\Setting\\SettingSeeder'); + + $this->getSettingService()->set('with_array_value', [ + 'updated_key' => 'updated_value', + ]); + + $value = $this->getSettingService()->get('with_array_value'); + $this->assertEquals('updated_value', $value['updated_key']); + $this->assertFalse(isset($value['key1'])); + } + + public function testRowCacheInvalidationOnRemove() + { + if (empty($this->biz['dao.cache.enabled'])) { + $this->markTestSkipped('DAO cache is not enabled, skipping RowStrategy cache test.'); + } + + $this->seed('Tests\\Setting\\SettingSeeder'); + + $this->getSettingService()->remove('with_string_value'); + $value = $this->getSettingService()->get('with_string_value'); + + $this->assertNull($value); + } + protected function getSettingService() { return $this->biz->service('Setting:SettingService');