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

fix: set default TTL for APCu cache as per docs #46395

Merged
merged 5 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
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
36 changes: 8 additions & 28 deletions lib/private/Memcache/APCu.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public function get($key) {
}

public function set($key, $value, $ttl = 0) {
if ($ttl === 0) {
$ttl = self::DEFAULT_TTL;
}
return apcu_store($this->getPrefix() . $key, $value, $ttl);
}

Expand Down Expand Up @@ -56,6 +59,9 @@ public function clear($prefix = '') {
* @return bool
*/
public function add($key, $value, $ttl = 0) {
if ($ttl === 0) {
$ttl = self::DEFAULT_TTL;
}
return apcu_add($this->getPrefix() . $key, $value, $ttl);
}

Expand All @@ -67,22 +73,8 @@ public function add($key, $value, $ttl = 0) {
* @return int | bool
*/
public function inc($key, $step = 1) {
$this->add($key, 0);
/**
* TODO - hack around a PHP 7 specific issue in APCu
*
* on PHP 7 the apcu_inc method on a non-existing object will increment
* "0" and result in "1" as value - therefore we check for existence
* first
*
* on PHP 5.6 this is not the case
*
* see https://github.com/krakjoe/apcu/issues/183#issuecomment-244038221
* for details
*/
return apcu_exists($this->getPrefix() . $key)
? apcu_inc($this->getPrefix() . $key, $step)
: false;
$success = null;
return apcu_inc($this->getPrefix() . $key, $step, $success, self::DEFAULT_TTL);
}

/**
Expand All @@ -93,18 +85,6 @@ public function inc($key, $step = 1) {
* @return int | bool
*/
public function dec($key, $step = 1) {
/**
* TODO - hack around a PHP 7 specific issue in APCu
*
* on PHP 7 the apcu_dec method on a non-existing object will decrement
* "0" and result in "-1" as value - therefore we check for existence
* first
*
* on PHP 5.6 this is not the case
*
* see https://github.com/krakjoe/apcu/issues/183#issuecomment-244038221
* for details
*/
return apcu_exists($this->getPrefix() . $key)
? apcu_dec($this->getPrefix() . $key, $step)
: false;
Expand Down
1 change: 0 additions & 1 deletion lib/private/Memcache/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ 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

/**
Expand Down
5 changes: 5 additions & 0 deletions lib/public/ICache.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
* @since 6.0.0
*/
interface ICache {
/**
* @since 30.0.0
*/
public const DEFAULT_TTL = 24 * 60 * 60;

/**
* Get a value from the user cache
* @param string $key
Expand Down
6 changes: 6 additions & 0 deletions lib/public/IMemcache.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public function add($key, $value, $ttl = 0);
/**
* Increase a stored number
*
* If no value is stored with the key, it will behave as if a 0 was stored.
* If a non-numeric value is stored, the operation will fail and `false` is returned.
*
* @param string $key
* @param int $step
* @return int | bool
Expand All @@ -40,6 +43,9 @@ public function inc($key, $step = 1);
/**
* Decrease a stored number
*
* If no value is stored with the key, the operation will fail and `false` is returned.
* If a non-numeric value is stored, the operation will fail and `false` is returned.
*
* @param string $key
* @param int $step
* @return int | bool
Expand Down
7 changes: 7 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,12 @@
<referencedClass name="OCA\GlobalSiteSelector\Service\SlaveService"/>
</errorLevel>
</UndefinedDocblockClass>
<AmbiguousConstantInheritance>
<errorLevel type="suppress">
<!-- false positive: https://github.com/vimeo/psalm/issues/7818 -->
<referencedConstant name="OC\Memcache\Redis::DEFAULT_TTL" />
<referencedConstant name="OC\Memcache\LoggerWrapperCache::DEFAULT_TTL" />
</errorLevel>
</AmbiguousConstantInheritance>
</issueHandlers>
</psalm>
Loading