-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
31a50f4
commit 40be743
Showing
6 changed files
with
279 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Nette Framework (https://nette.org) | ||
* Copyright (c) 2004 David Grudl (https://davidgrudl.com) | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Nette\Caching; | ||
use Nette\InvalidStateException; | ||
use Nette\NotSupportedException; | ||
|
||
|
||
/** | ||
* Cache storage with a bulk write support. | ||
*/ | ||
interface BulkWriter | ||
{ | ||
/** | ||
* Writes to cache in bulk. | ||
* <p>Similar to <code>write()</code>, but instead of a single key/value item, it works on multiple items specified in <code>items</code></p> | ||
* | ||
* @param array{string, mixed} $items <p>An array of key/data pairs to store on the server</p> | ||
* @param array $dp Global dependencies of each stored value | ||
* @return bool <p>Returns <b><code>true</code></b> on success or <b><code>false</code></b> on failure</p> | ||
* @throws NotSupportedException | ||
* @throws InvalidStateException | ||
*/ | ||
function bulkWrite(array $items, array $dp): bool; | ||
|
||
/** | ||
* Removes multiple items from cache | ||
*/ | ||
function bulkRemove(array $keys): void; | ||
} |
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 |
---|---|---|
|
@@ -173,6 +173,56 @@ public function bulkLoad(array $keys, ?callable $generator = null): array | |
} | ||
|
||
|
||
/** | ||
* Writes multiple items into cache | ||
* | ||
* @param array $items | ||
* @param array|null $dependencies | ||
* @return array Stored items | ||
* | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function bulkSave(array $items, ?array $dependencies = null): array | ||
Check failure on line 185 in src/Caching/Cache.php GitHub Actions / PHPStan
|
||
{ | ||
$storedItems = []; | ||
|
||
if (!$this->storage instanceof BulkWriter) { | ||
|
||
foreach ($items as $key => $data) { | ||
$storedItems[$key] = $this->save($key, $data, $dependencies); | ||
} | ||
return $storedItems; | ||
} | ||
|
||
$dependencies = $this->completeDependencies($dependencies); | ||
|
||
if (isset($dependencies[self::Expire]) && $dependencies[self::Expire] <= 0) { | ||
$this->storage->bulkRemove(array_map(fn($key): string => $this->generateKey($key), array_keys($items))); | ||
return []; | ||
} | ||
|
||
$removals = []; | ||
$toCache = []; | ||
foreach ($items as $key => $data) { | ||
$cKey = $this->generateKey($key); | ||
|
||
if ($data === null) { | ||
$removals[] = $cKey; | ||
} else { | ||
$storedItems[$key] = $toCache[$cKey] = $data; | ||
} | ||
} | ||
|
||
if (!empty($removals)) { | ||
$this->storage->bulkRemove($removals); | ||
} | ||
|
||
$this->storage->bulkWrite($toCache, $dependencies); | ||
|
||
return $storedItems; | ||
} | ||
|
||
|
||
/** | ||
* Writes item into the cache. | ||
* Dependencies are: | ||
|
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,53 @@ | ||
<?php | ||
|
||
/** | ||
* Test: Nette\Caching\Cache save(). | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
use Nette\Caching\Cache; | ||
use Tester\Assert; | ||
|
||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
require __DIR__ . '/Cache.php'; | ||
|
||
|
||
test('storage without bulk write support', function () { | ||
$storage = new TestStorage; | ||
$cache = new Cache($storage, 'ns'); | ||
Assert::same([1, 2], $cache->bulkSave([1, 2]), 'data'); | ||
Assert::same([1 => 'value1', 2 => 'value2'], $cache->bulkSave([1 => 'value1', 2 => 'value2']), 'data'); | ||
|
||
$data = $cache->bulkLoad([1, 2]); | ||
Assert::same('value1', $data[1]['data']); | ||
Assert::same('value2', $data[2]['data']); | ||
}); | ||
|
||
test('storage with bulk write support', function () { | ||
$storage = new BulkWriteTestStorage; | ||
$cache = new Cache($storage, 'ns'); | ||
Assert::same([1, 2], $cache->bulkSave([1, 2]), 'data'); | ||
Assert::same([1 => 'value1', 2 => 'value2'], $cache->bulkSave([1 => 'value1', 2 => 'value2']), 'data'); | ||
|
||
$data = $cache->bulkLoad([1, 2]); | ||
Assert::same('value1', $data[1]['data']); | ||
Assert::same('value2', $data[2]['data']); | ||
}); | ||
|
||
test('dependencies', function () { | ||
$storage = new BulkWriteTestStorage; | ||
$cache = new Cache($storage, 'ns'); | ||
$dependencies = [Cache::Tags => ['tag']]; | ||
$cache->bulkSave([1 => 'value1', 2 => 'value2'], $dependencies); | ||
|
||
$data = $cache->bulkLoad([1, 2]); | ||
Assert::same($dependencies, $data[1]['dependencies']); | ||
Assert::same($dependencies, $data[2]['dependencies']); | ||
|
||
$cache->clean($dependencies); | ||
|
||
Assert::same([1 => null, 2 => null], $cache->bulkLoad([1, 2])); | ||
}); |
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,37 @@ | ||
<?php | ||
|
||
/** | ||
* Test: Nette\Caching\Storages\MemcachedStorage and bulkWrite | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
use Nette\Caching\Cache; | ||
use Nette\Caching\Storages\MemcachedStorage; | ||
use Nette\Caching\Storages\SQLiteJournal; | ||
use Tester\Assert; | ||
|
||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
|
||
if (!MemcachedStorage::isAvailable()) { | ||
Tester\Environment::skip('Requires PHP extension Memcached.'); | ||
} | ||
|
||
Tester\Environment::lock('memcached-files', getTempDir()); | ||
|
||
|
||
$storage = new MemcachedStorage('localhost', 11211, '', new SQLiteJournal(getTempDir() . '/journal-memcached.s3db')); | ||
$cache = new Cache($storage); | ||
|
||
//standard | ||
$cache->bulkSave(["foo" => "bar"]); | ||
Assert::same(['foo' => 'bar', 'lorem' => null], $cache->bulkLoad(['foo', 'lorem'])); | ||
|
||
//tags | ||
$dependencies = [Cache::Tags => ['tag']]; | ||
$cache->bulkSave(["foo" => "bar"], $dependencies); | ||
Assert::same(['foo' => 'bar'], $cache->bulkLoad(['foo'])); | ||
$cache->clean($dependencies); | ||
Assert::same(['foo' => null], $cache->bulkLoad(['foo'])); |