modifs.mineures sans commentaires
This commit is contained in:
parent
1d0aba7215
commit
614f75d540
|
@ -12,9 +12,11 @@ use nur\A;
|
|||
* une session de travail
|
||||
*/
|
||||
class CacheManager {
|
||||
function __construct() {
|
||||
function __construct(?array $includes=null, ?array $excludes=null) {
|
||||
$this->caches = [];
|
||||
$this->defaultCache = true;
|
||||
$this->includes = $includes;
|
||||
$this->excludes = $excludes;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -29,13 +31,33 @@ class CacheManager {
|
|||
*/
|
||||
protected $defaultCache;
|
||||
|
||||
/**
|
||||
* @var array|null groupes à toujours inclure dans le cache. pour les
|
||||
* identifiants de ces groupe, {@link self::shouldCache()} retourne toujours
|
||||
* true.
|
||||
*
|
||||
* $excludes est prioritaire par rapport à $includes
|
||||
*/
|
||||
protected $includes;
|
||||
|
||||
/**
|
||||
* @var array|null groupes à exclure de la mise en cache. la mise en cache est
|
||||
* toujours calculée pour les identifiants de ces groupes.
|
||||
*/
|
||||
protected $excludes;
|
||||
|
||||
function setNoCache(bool $noCache=true, bool $reset=true): self {
|
||||
if ($reset) $this->caches = [];
|
||||
$this->defaultCache = !$noCache;
|
||||
return $this;
|
||||
}
|
||||
|
||||
function shouldCache(string $id, bool $reset=true): bool {
|
||||
function shouldCache(string $id, ?string $groupId=null, bool $reset=true): bool {
|
||||
$includes = $this->includes;
|
||||
$shouldInclude = $includes !== null && in_array($groupId, $includes);
|
||||
$excludes = $this->excludes;
|
||||
$shouldExclude = $excludes !== null && in_array($groupId, $excludes);
|
||||
if ($shouldInclude && !$shouldExclude) return true;
|
||||
$cache = A::get($this->caches, $id, $this->defaultCache);
|
||||
$this->caches[$id] = $reset?: $cache;
|
||||
return $cache;
|
||||
|
|
|
@ -13,20 +13,20 @@ class CacheManagerTest extends TestCase {
|
|||
self::assertTrue($cm->shouldCache("a"));
|
||||
self::assertTrue($cm->shouldCache("a"));
|
||||
|
||||
$cm->setNoCache();
|
||||
$cm->setNoCache(true);
|
||||
self::assertFalse($cm->shouldCache("b"));
|
||||
self::assertTrue($cm->shouldCache("b"));
|
||||
self::assertTrue($cm->shouldCache("b"));
|
||||
|
||||
$cm->setNoCache();
|
||||
self::assertFalse($cm->shouldCache("c", false));
|
||||
self::assertFalse($cm->shouldCache("c", false));
|
||||
self::assertFalse($cm->shouldCache("c", false));
|
||||
$cm->setNoCache(true);
|
||||
self::assertFalse($cm->shouldCache("c", null, false));
|
||||
self::assertFalse($cm->shouldCache("c", null, false));
|
||||
self::assertFalse($cm->shouldCache("c", null, false));
|
||||
|
||||
$cm->setNoCache();
|
||||
$cm->setNoCache(true);
|
||||
self::assertFalse($cm->shouldCache("d"));
|
||||
self::assertTrue($cm->shouldCache("d"));
|
||||
$cm->setNoCache();
|
||||
$cm->setNoCache(true);
|
||||
self::assertFalse($cm->shouldCache("d"));
|
||||
self::assertTrue($cm->shouldCache("d"));
|
||||
}
|
||||
|
|
13
src/cl.php
13
src/cl.php
|
@ -315,6 +315,19 @@ class cl {
|
|||
|
||||
#############################################################################
|
||||
|
||||
static final function map(callable $callback, ?iterable $array): array {
|
||||
$result = [];
|
||||
if ($array !== null) {
|
||||
$ctx = func::_prepare($callback);
|
||||
foreach ($array as $key => $value) {
|
||||
$result[$key] = func::_call($ctx, [$value, $key]);
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
#############################################################################
|
||||
|
||||
/**
|
||||
* vérifier que le chemin $keys existe dans le tableau $array
|
||||
*
|
||||
|
|
|
@ -26,7 +26,7 @@ class G {
|
|||
$_GET[$name] = $value;
|
||||
}
|
||||
|
||||
static final function xselect(?array $includes, ?array $excludes=null): array {
|
||||
return cl::xselect($_GET, $includes, $excludes);
|
||||
static final function xselect(?array $includes, ?array $excludes=null, ?array $merge=null): array {
|
||||
return cl::merge(cl::xselect($_GET, $includes, $excludes), $merge);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue