-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ReadFileQueryCreator to cache read-only calls DBFile::exists(). O…
…ther small optimisation tweaks.
- Loading branch information
1 parent
e660e76
commit dccbc90
Showing
5 changed files
with
137 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Assets\Services; | ||
|
||
use SilverStripe\Core\Injector\Injectable; | ||
|
||
/** | ||
* Used to cache results for the duration of a request during read-only file operations | ||
* Do not use this during any CRUD operations | ||
*/ | ||
class ReadOnlyCacheService | ||
{ | ||
|
||
use Injectable; | ||
|
||
private $enabled = false; | ||
|
||
private $cache = []; | ||
|
||
public function getEnabled(): bool | ||
{ | ||
return $this->enabled; | ||
} | ||
|
||
public function setEnabled(bool $enabled): void | ||
{ | ||
$this->enabled = $enabled; | ||
} | ||
|
||
public function reset(?array $cacheNameComponents = null): void | ||
{ | ||
if (is_null($cacheNameComponents)) { | ||
$this->cache = []; | ||
return; | ||
} | ||
$cacheName = $this->buildKey($cacheNameComponents); | ||
if (isset($this->cache[$cacheName])) { | ||
unset($this->cache[$cacheName]); | ||
} | ||
} | ||
|
||
public function setValue(array $cacheNameComponents, array $cacheKeyComponents, $value): void | ||
{ | ||
$cacheName = $this->buildKey($cacheNameComponents); | ||
$key = $this->buildKey($cacheKeyComponents); | ||
if (!isset($this->cache[$cacheName])) { | ||
$this->cache[$cacheName] = []; | ||
} | ||
$this->cache[$cacheName][$key] = $value; | ||
} | ||
|
||
public function getValue(array $cacheNameComponents, array $cacheKeyComponents) | ||
{ | ||
$cacheName = $this->buildKey($cacheNameComponents); | ||
$key = $this->buildKey($cacheKeyComponents); | ||
return $this->cache[$cacheName][$key] ?? null; | ||
} | ||
|
||
public function hasValue(array $cacheNameComponents, array $cacheKeyComponents): bool | ||
{ | ||
$cacheName = $this->buildKey($cacheNameComponents); | ||
$key = $this->buildKey($cacheKeyComponents); | ||
return isset($this->cache[$cacheName]); | ||
} | ||
|
||
private function buildKey(array $components) | ||
{ | ||
return implode('-', $components); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Assets\Tests\Services; | ||
|
||
use SilverStripe\Assets\Services\ReadOnlyCacheService; | ||
use SilverStripe\Dev\SapphireTest; | ||
|
||
class ReadOnlyCacheServiceTest extends SapphireTest | ||
{ | ||
|
||
public function testGetSetEnabled() | ||
{ | ||
$service = ReadOnlyCacheService::singleton(); | ||
$this->assertFalse($service->getEnabled()); | ||
$service->setEnabled(true); | ||
$this->assertTrue($service->getEnabled()); | ||
} | ||
|
||
public function testGetSetHasValue() | ||
{ | ||
$service = ReadOnlyCacheService::singleton(); | ||
$this->assertFalse($service->hasValue(['A', 'B'], ['1', '2'])); | ||
$service->setValue(['A', 'B'], ['1', '2'], 'xyz'); | ||
$this->assertTrue($service->hasValue(['A', 'B'], ['1', '2'])); | ||
$this->assertEquals('xyz', $service->getValue(['A', 'B'], ['1', '2'])); | ||
} | ||
|
||
public function testReset() | ||
{ | ||
$service = ReadOnlyCacheService::singleton(); | ||
$service->setValue(['A', 'B'], ['1', '2'], 'xyz'); | ||
$service->setValue(['C', 'D'], ['3', '4'], 'wvu'); | ||
$this->assertTrue($service->hasValue(['A', 'B'], ['1', '2'])); | ||
$service->reset(['A', 'B']); | ||
$this->assertFalse($service->hasValue(['A', 'B'], ['1', '2'])); | ||
$this->assertTrue($service->hasValue(['C', 'D'], ['3', '4'])); | ||
$service->reset(); | ||
$this->assertFalse($service->hasValue(['C', 'D'], ['3', '4'])); | ||
} | ||
} |