Add Redis-based permission caching for users and subusers

- Introduced Redis-backed caching for user and subuser permission evaluations in the `route_t` trait, reducing database queries.
- Enhanced `Redis` class with methods for permission caching: `cache_permission`, `get_permission`, and `clear_permission`.
- Added test coverage for the new caching logic in `PermissionRedisCacheTest.php`.
- Implemented Redis caching for authentication sessions with `cache_auth_session`, `get_auth_session`, and `clear_auth_session`.
- Improved CORS handling for preflight requests in `index.php`.
This commit is contained in:
Jeppe Bundgaard
2026-02-24 15:00:22 +01:00
parent 2534ffb1b9
commit 5cc311ae32
7 changed files with 367 additions and 12 deletions
+58
View File
@@ -332,6 +332,34 @@ class redis implements redis_i
return $this;
}
/**
* Cache auth session payload for a token with TTL
*/
public function cache_auth_session(string $token, array $data, int $ttl = 60): self
{
$key = 'auth_session_' . $token;
$this->set_array($key, $data);
$this->expire($key, $ttl);
return $this;
}
/**
* Get cached auth session payload by token
*/
public function get_auth_session(string $token): array|null
{
return $this->get_array('auth_session_' . $token);
}
/**
* Clear cached auth session payload by token
*/
public function clear_auth_session(string $token): self
{
$this->delete('auth_session_' . $token);
return $this;
}
/**
* @inheritDoc
*/
@@ -427,4 +455,34 @@ class redis implements redis_i
// Get multiple keys from Redis
return $this->redis->mget($array_map);
}
/**
* @inheritDoc
*/
public function cache_permission(string $cache_key, bool $allowed, int $ttl = 300): self
{
$this->setEx('perm:' . $cache_key, $allowed ? '1' : '0', $ttl);
return $this;
}
/**
* @inheritDoc
*/
public function get_permission(string $cache_key): bool|null
{
$val = $this->get('perm:' . $cache_key);
if ($val === null) {
return null;
}
return $val === '1';
}
/**
* @inheritDoc
*/
public function clear_permission(string $cache_key): self
{
$this->delete('perm:' . $cache_key);
return $this;
}
}