Skip to content

Commit 5d32425

Browse files
onairmarcEncoreBot
andauthored
Add Ability to Disable StaticCache (#66)
Co-authored-by: EncoreBot <[email protected]>
1 parent 290db74 commit 5d32425

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

.git-blame-ignore-revs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ adee35fd7b993cb4a7cf85df7ee997951af3e907
3434
f3cda4980fdc4c227e00250d7dfcfe4639dc60dc
3535
d676999a6145177a6f61007806f5b95f1df868bd
3636
53d9e752575f07527143ccee692673b8c6793141
37+
8334d091011e09e060e476cb29a17239627e9a39

src/Objects/Support/StaticCache.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@
33
namespace EncoreDigitalGroup\StdLib\Objects\Support;
44

55
use BackedEnum;
6+
use EncoreDigitalGroup\StdLib\Objects\Support\Traits\StaticCacheEnabled;
67

78
class StaticCache
89
{
10+
use StaticCacheEnabled;
11+
912
protected static array $cache = [];
1013

1114
public static function add(BackedEnum|string $key, mixed $value, BackedEnum|string $partition = "default"): void
1215
{
16+
if (self::disabled()) {
17+
return;
18+
}
19+
1320
$key = self::enum($key);
1421
$partition = self::enum($partition);
1522

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace EncoreDigitalGroup\StdLib\Objects\Support\Traits;
4+
5+
/** @internal */
6+
trait StaticCacheEnabled
7+
{
8+
protected static array $enabled = [];
9+
10+
protected const string ENABLED = "enabled";
11+
12+
public static function enable(): void
13+
{
14+
static::$enabled[static::class][self::ENABLED] = true;
15+
}
16+
17+
public static function disable(bool $flush = true): void
18+
{
19+
if ($flush) {
20+
static::flush();
21+
}
22+
23+
static::$enabled[static::class][self::ENABLED] = false;
24+
}
25+
26+
protected static function enabled(): bool
27+
{
28+
if (!isset(static::$enabled[static::class][self::ENABLED])) {
29+
static::$enabled[static::class][self::ENABLED] = true;
30+
}
31+
32+
return static::$enabled[static::class][self::ENABLED];
33+
}
34+
35+
protected static function disabled(): bool
36+
{
37+
return !static::enabled();
38+
}
39+
}

0 commit comments

Comments
 (0)