-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(performance): improved strategies and processors
- Loading branch information
1 parent
9577793
commit 757c82a
Showing
66 changed files
with
1,846 additions
and
926 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
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,43 @@ | ||
<?php | ||
|
||
namespace Ninja\Censor\Cache; | ||
|
||
final class PatternCache | ||
{ | ||
/** @var array<string, string> */ | ||
private array $patterns = []; | ||
|
||
/** @var array<string, int> */ | ||
private array $lastUsed = []; | ||
|
||
public function __construct( | ||
private readonly int $maxSize = 1000 | ||
) {} | ||
|
||
public function get(string $key): ?string | ||
{ | ||
if (isset($this->patterns[$key])) { | ||
$this->lastUsed[$key] = time(); | ||
|
||
return $this->patterns[$key]; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function set(string $key, string $pattern): void | ||
{ | ||
if (count($this->patterns) >= $this->maxSize) { | ||
$oldest = array_key_first( | ||
array_filter( | ||
$this->lastUsed, | ||
fn ($time) => $time === min(array_values($this->lastUsed)) | ||
) | ||
); | ||
unset($this->patterns[$oldest], $this->lastUsed[$oldest]); | ||
} | ||
|
||
$this->patterns[$key] = $pattern; | ||
$this->lastUsed[$key] = time(); | ||
} | ||
} |
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
Oops, something went wrong.