Skip to content
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
10 changes: 4 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
},
Comment on lines 36 to 40
"config": {
"bin-dir": "bin"
Expand Down
10 changes: 9 additions & 1 deletion src/Setting/Dao/Impl/SettingDaoImpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -25,7 +33,7 @@ public function declares()
{
return array(
'serializes' => array('data' => 'php'),
'cache' => 'table',
'cache' => 'row',
);
}
}
36 changes: 1 addition & 35 deletions src/Setting/Service/Impl/SettingServiceImpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public function set($name, $data)
'data' => $data,
));
}
$this->clearCache();
}

public function remove($name)
Expand Down Expand Up @@ -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)
Expand All @@ -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();
}
}
59 changes: 59 additions & 0 deletions tests/Setting/SettingServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down