Repository navigation
Improve performance #9186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Improve performance #9186
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,11 @@ class SettingCacheManager extends CacheManagerBase implements CacheInterface | |
|
|
||
| const DEFAULT_CACHE_PREFIX = 'settings:'; | ||
|
|
||
| /** | ||
| * Envelope marker so cached null is distinguishable from a Laravel cache miss. | ||
| */ | ||
| private const ENVELOPE_FLAG = '__pm_setting'; | ||
|
|
||
| protected CacheManager $manager; | ||
|
|
||
| protected Repository $cacheManager; | ||
|
|
@@ -28,8 +33,6 @@ public function __construct(CacheManager $cacheManager) | |
| /** | ||
| * Determine and set the cache driver to use. | ||
| * | ||
| * @param CacheManager $cacheManager | ||
| * | ||
| * @return void | ||
| */ | ||
| private function setCacheDriver(): void | ||
|
|
@@ -44,14 +47,10 @@ private function setCacheDriver(): void | |
| } | ||
|
|
||
| /** | ||
| * Create a cache key for a screen | ||
| * Create a cache key for a setting. | ||
| * | ||
| * @param int $processId Process ID | ||
| * @param int $processVersionId Process Version ID | ||
| * @param string $language Language code | ||
| * @param int $screenId Screen ID | ||
| * @param int $screenVersionId Screen Version ID | ||
| * @return string The generated cache key | ||
| * @param array $params | ||
| * @return string | ||
| */ | ||
| public function createKey(array $params): string | ||
| { | ||
|
|
@@ -88,7 +87,80 @@ public function __call($method, $arguments): mixed | |
| */ | ||
| public function get(string $key, mixed $default = null): mixed | ||
| { | ||
| return $this->cacheManager->get($key, $default); | ||
| SettingMemoryCache::syncEpoch(); | ||
|
|
||
| if (SettingMemoryCache::isEpochUnreliable()) { | ||
| return $default; | ||
| } | ||
|
|
||
| if (SettingMemoryCache::exists($key)) { | ||
| return SettingMemoryCache::get($key); | ||
| } | ||
|
|
||
| $entry = $this->readStoreEntry($key); | ||
| if (!$entry['hit']) { | ||
| return $default; | ||
| } | ||
|
|
||
| SettingMemoryCache::put($key, $entry['value']); | ||
|
|
||
| return $entry['value']; | ||
| } | ||
|
|
||
| /** | ||
| * Resolve a setting from L1, then Redis, then the loader (typically DB). | ||
| * | ||
| * @param string $key | ||
| * @param callable $loader | ||
| * | ||
| * @return mixed | ||
| */ | ||
| public function resolve(string $key, callable $loader): mixed | ||
| { | ||
| SettingMemoryCache::syncEpoch(); | ||
|
|
||
| // Freshness oracle unavailable: fail closed to DB, do not trust L1/L2. | ||
| if (SettingMemoryCache::isEpochUnreliable()) { | ||
| return $loader(); | ||
| } | ||
|
|
||
| if (SettingMemoryCache::exists($key)) { | ||
| return SettingMemoryCache::get($key); | ||
| } | ||
|
|
||
| $epochAtStart = SettingMemoryCache::localEpoch(); | ||
| $entry = $this->readStoreEntry($key); | ||
|
|
||
| if ($entry['hit']) { | ||
| SettingMemoryCache::put($key, $entry['value']); | ||
|
|
||
| return $entry['value']; | ||
| } | ||
|
|
||
| $value = $loader(); | ||
|
|
||
| // If another worker published a committed value while we loaded, do not | ||
| // overwrite Redis with a possibly pre-commit row. | ||
| SettingMemoryCache::forceResyncEpoch(); | ||
| if (SettingMemoryCache::isEpochUnreliable()) { | ||
| return $value; | ||
| } | ||
|
|
||
| if (SettingMemoryCache::localEpoch() !== $epochAtStart) { | ||
| $entry = $this->readStoreEntry($key); | ||
| if ($entry['hit']) { | ||
| $value = $entry['value']; | ||
| } else { | ||
| $value = $loader(); | ||
| $this->writeStoreEntry($key, $value); | ||
| } | ||
| } else { | ||
| $this->writeStoreEntry($key, $value); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stale resolve can overwrite RedisHigh Severity
Additional Locations (1)Reviewed by Cursor Bugbot for commit 13d40b1. Configure here. |
||
|
|
||
| SettingMemoryCache::put($key, $value); | ||
|
|
||
| return $value; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -125,7 +197,7 @@ public function getOrCache(string $key, callable $callback): mixed | |
| } | ||
|
|
||
| /** | ||
| * Store a value in the settings cache. | ||
| * Store a committed setting value, then publish a new epoch so other workers refresh L1. | ||
| * | ||
| * @param string $key | ||
| * @param mixed $value | ||
|
|
@@ -135,7 +207,14 @@ public function getOrCache(string $key, callable $callback): mixed | |
| */ | ||
| public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔒 Agentic Security Review Severity: MEDIUM Description: Security-setting saves advance the shared L1 epoch in SettingObserver::saving before the SQL update commits, and SettingCacheManager::set() later stores the committed model in Redis without advancing the epoch again. A concurrent Octane worker that calls Setting::byKey in that window syncs to the new epoch, misses Redis, loads the still-old row, and keeps it in its process-local L1. On later requests the epochs match, so syncEpoch leaves that map in place and the worker keeps enforcing the pre-commit value. This sticks for session-control.ip_restriction and session-control.device_restriction (Setting::configByKey on every web request), for session.lifetime (SettingsConfigRepository always reloads it through byKey), and for sso.default.login when package-auth is installed. password-policies.* read through config() is memoized in SettingsConfigRepository::$items after the first lookup, so that path is not what makes the epoch bug sticky. Impact: After an admin tightens session IP/device restrictions, idle timeout, or the default SSO login, Octane workers that resolved that key between the pre-commit epoch bump and the post-commit cache fill keep applying the previous weaker policy on the public web stack until some later setting write bumps the epoch. Users who land on those workers bypass the updated session or login control. Winning the race is timing-dependent; once won, the stale L1 entry persists across requests because epoch equality is treated as proof the memory map matches Redis. Remediation: Publish the epoch only after the committed setting is the value stored in Redis: move the bump from the saving() invalidate path to the post-commit set() (or a saved() hook) after put(). Do not let resolve() cache a loader result under an epoch that was already advanced for an uncommitted write; a put of the pre-commit row that lands after set() must not become the value later workers treat as current. Keep invalidation and the committed fill atomic so a concurrent byKey cannot repopulate L1 with the old row at the epoch set() just published. |
||
| { | ||
| return $this->cacheManager->put($key, $value, $ttl); | ||
| $result = $this->writeStoreEntry($key, $value, $ttl); | ||
|
|
||
| if ($result) { | ||
| SettingMemoryCache::bumpEpoch(); | ||
| SettingMemoryCache::put($key, $value); | ||
| } | ||
|
|
||
| return $result; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -147,7 +226,11 @@ public function set(string $key, mixed $value, null|int|\DateInterval $ttl = nul | |
| */ | ||
| public function delete(string $key): bool | ||
| { | ||
| return $this->cacheManager->forget($key); | ||
| $result = $this->cacheManager->forget($key); | ||
| SettingMemoryCache::forget($key); | ||
| SettingMemoryCache::bumpEpoch(); | ||
|
|
||
| return $result; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -157,7 +240,10 @@ public function delete(string $key): bool | |
| */ | ||
| public function clear(): bool | ||
| { | ||
| return $this->cacheManager->clear(); | ||
| $result = $this->cacheManager->clear(); | ||
| SettingMemoryCache::bumpEpochAfterStoreCleared(); | ||
|
|
||
| return $result; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -181,6 +267,8 @@ public function clearBy(string $pattern): void | |
| if (!empty($matchedKeys)) { | ||
| Redis::connection($defaultDriver)->del($matchedKeys); | ||
| } | ||
|
|
||
| SettingMemoryCache::bumpEpoch(); | ||
| } catch (\Exception $e) { | ||
| Log::error('SettingCacheException' . $e->getMessage()); | ||
|
|
||
|
|
@@ -197,6 +285,16 @@ public function clearBy(string $pattern): void | |
| */ | ||
| public function has(string $key): bool | ||
| { | ||
| SettingMemoryCache::syncEpoch(); | ||
|
|
||
| if (SettingMemoryCache::isEpochUnreliable()) { | ||
| return false; | ||
| } | ||
|
|
||
| if (SettingMemoryCache::exists($key)) { | ||
| return true; | ||
| } | ||
|
|
||
| return $this->keyExists($key); | ||
| } | ||
|
|
||
|
|
@@ -213,22 +311,78 @@ public function missing(string $key): bool | |
| } | ||
|
|
||
| /** | ||
| * Invalidate a value in the settings cache. | ||
| * Drop a settings cache key from Redis/L1. | ||
| * | ||
| * @param string $key | ||
| * By default does NOT bump the shared epoch — that must happen after the | ||
| * committed value is published via set()/saved()/delete. Pass | ||
| * bump_epoch=true for hard invalidations (e.g. deleted settings). | ||
| * | ||
| * @param array $params | ||
| * | ||
| * @return void | ||
| */ | ||
| public function invalidate($params): void | ||
| { | ||
| try { | ||
| //extract the params from the array | ||
| $key = $params['key']; | ||
| $bumpEpoch = (bool) ($params['bump_epoch'] ?? false); | ||
|
|
||
| $this->cacheManager->forget($key); | ||
| SettingMemoryCache::forget($key); | ||
|
|
||
| if ($bumpEpoch) { | ||
| SettingMemoryCache::bumpEpoch(); | ||
| } | ||
| } catch (\Exception $e) { | ||
| Log::error($e->getMessage()); | ||
|
|
||
| throw new SettingCacheException('Failed to invalidate cache KEY:' . $key); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Publish a new shared epoch without changing Redis setting payloads. | ||
| */ | ||
| public function publishEpoch(): void | ||
| { | ||
| SettingMemoryCache::bumpEpoch(); | ||
| } | ||
|
|
||
| /** | ||
| * Expose the underlying cache store (used for tests). | ||
| */ | ||
| public function store(): Repository | ||
| { | ||
| return $this->cacheManager; | ||
| } | ||
|
|
||
| /** | ||
| * @return array{hit: bool, value: mixed} | ||
| */ | ||
| private function readStoreEntry(string $key): array | ||
| { | ||
| $sentinel = SettingMemoryCache::sentinel(); | ||
| $stored = $this->cacheManager->get($key, $sentinel); | ||
|
|
||
| if ($stored === $sentinel) { | ||
| return ['hit' => false, 'value' => null]; | ||
| } | ||
|
|
||
| if (is_array($stored) && ($stored[self::ENVELOPE_FLAG] ?? false) === true) { | ||
| return ['hit' => true, 'value' => $stored['v']]; | ||
| } | ||
|
|
||
| // Legacy unwrapped values written before envelopes. | ||
| return ['hit' => true, 'value' => $stored]; | ||
| } | ||
|
|
||
| private function writeStoreEntry(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool | ||
| { | ||
| $envelope = [ | ||
| self::ENVELOPE_FLAG => true, | ||
| 'v' => $value, | ||
| ]; | ||
|
|
||
| return $this->cacheManager->put($key, $envelope, $ttl); | ||
| } | ||
| } | ||


Uh oh!
There was an error while loading. Please reload this page.