Skip to content
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

set a default and max ttl for redis keys #38562

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions lib/private/Memcache/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class Redis extends Cache implements IMemcacheTTL {
],
];

private const DEFAULT_TTL = 24 * 60 * 60; // 1 day
private const MAX_TTL = 30 * 24 * 60 * 60; // 1 month

/**
* @var \Redis|\RedisCluster $cache
*/
Expand Down Expand Up @@ -83,11 +86,12 @@ public function get($key) {

public function set($key, $value, $ttl = 0) {
$value = self::encodeValue($value);
if ($ttl > 0) {
return $this->getCache()->setex($this->getPrefix() . $key, $ttl, $value);
} else {
return $this->getCache()->set($this->getPrefix() . $key, $value);
if ($ttl === 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m a tiny bit worried that we may have code path not sending an int in $ttl since it is not strong typed, so here passing '0' would still result in infinite TTL I think?
Not sure if this worry is justified.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were already checking for an int, I'd say LGTM

// having infinite TTL can lead to leaked keys as the prefix changes with version upgrades
$ttl = self::DEFAULT_TTL;
}
$ttl = min($ttl, self::MAX_TTL);
return $this->getCache()->setex($this->getPrefix() . $key, $ttl, $value);
}

public function hasKey($key) {
Expand Down Expand Up @@ -121,11 +125,14 @@ public function clear($prefix = '') {
*/
public function add($key, $value, $ttl = 0) {
$value = self::encodeValue($value);
if ($ttl === 0) {
// having infinite TTL can lead to leaked keys as the prefix changes with version upgrades
$ttl = self::DEFAULT_TTL;
}
$ttl = min($ttl, self::MAX_TTL);

$args = ['nx'];
if ($ttl !== 0 && is_int($ttl)) {
$args['ex'] = $ttl;
}
$args['ex'] = $ttl;

return $this->getCache()->set($this->getPrefix() . $key, $value, $args);
}
Expand Down Expand Up @@ -182,6 +189,11 @@ public function cad($key, $old) {
}

public function setTTL($key, $ttl) {
if ($ttl === 0) {
// having infinite TTL can lead to leaked keys as the prefix changes with version upgrades
$ttl = self::DEFAULT_TTL;
}
$ttl = min($ttl, self::MAX_TTL);
$this->getCache()->expire($this->getPrefix() . $key, $ttl);
}

Expand Down
Loading