| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace WindPress\WindPress\Core\Scanner; |
| 5 |
|
| 6 |
use WIND_PRESS; |
| 7 |
/** |
| 8 |
* Reuses deterministic transformations of freshly read source inputs. |
| 9 |
*/ |
| 10 |
class ExtractionCache |
| 11 |
{ |
| 12 |
private const VERSION = 1; |
| 13 |
private const TTL = 7 * 24 * 60 * 60; |
| 14 |
private const MAX_ENTRY_BYTES = 2 * 1024 * 1024; |
| 15 |
public static function remember(string $namespace, string $identity, array $inputs, callable $extract) |
| 16 |
{ |
| 17 |
$key = 'windpress_extract_' . hash('sha256', serialize([self::VERSION, WIND_PRESS::VERSION, get_current_blog_id(), get_current_user_id(), $namespace, $identity])); |
| 18 |
$fingerprint = hash('sha256', serialize($inputs)); |
| 19 |
$cached = get_transient($key); |
| 20 |
if (is_array($cached) && ($cached['fingerprint'] ?? null) === $fingerprint && array_key_exists('result', $cached)) { |
| 21 |
return $cached['result']; |
| 22 |
} |
| 23 |
$result = $extract(); |
| 24 |
$entry = ['fingerprint' => $fingerprint, 'result' => $result]; |
| 25 |
// Cache storage is optional; the freshly extracted source remains authoritative. |
| 26 |
if (strlen(serialize($entry)) <= self::MAX_ENTRY_BYTES) { |
| 27 |
set_transient($key, $entry, self::TTL); |
| 28 |
} |
| 29 |
return $result; |
| 30 |
} |
| 31 |
} |
| 32 |
|