From 0a80bf55739ef9d30cfef376377f5c54dd5a9a9c Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 9 Dec 2015 14:39:12 +0100 Subject: Add interface for memcache backends that support setting ttl on exisiting keys --- lib/private/memcache/redis.php | 8 ++++++-- lib/public/imemcachettl.php | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 lib/public/imemcachettl.php diff --git a/lib/private/memcache/redis.php b/lib/private/memcache/redis.php index 83be662eabf..68b62e7534a 100644 --- a/lib/private/memcache/redis.php +++ b/lib/private/memcache/redis.php @@ -25,9 +25,9 @@ namespace OC\Memcache; -use OCP\IMemcache; +use OCP\IMemcacheTTL; -class Redis extends Cache implements IMemcache { +class Redis extends Cache implements IMemcacheTTL { /** * @var \Redis $cache */ @@ -195,6 +195,10 @@ class Redis extends Cache implements IMemcache { return false; } + public function setTTL($key, $ttl) { + self::$cache->expire($this->getNamespace() . $key, $ttl); + } + static public function isAvailable() { return extension_loaded('redis') && version_compare(phpversion('redis'), '2.2.5', '>='); diff --git a/lib/public/imemcachettl.php b/lib/public/imemcachettl.php new file mode 100644 index 00000000000..aae26accb48 --- /dev/null +++ b/lib/public/imemcachettl.php @@ -0,0 +1,37 @@ + + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ + +namespace OCP; + +/** + * Interface for memcache backends that support setting ttl after the value is set + * + * @since 9.0.0 + */ +interface IMemcacheTTL extends IMemcache { + /** + * Set the ttl for an existing value + * + * @param string $key + * @param int $ttl time to live in seconds + */ + public function setTTL($key, $ttl); +} -- cgit v1.2.3 From 693a3c353ef88df2372426d4d1ea9f6527a59d62 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 9 Dec 2015 14:41:15 +0100 Subject: ttl for memcache locking backends that support it --- lib/private/lock/abstractlockingprovider.php | 2 ++ lib/private/lock/dblockingprovider.php | 2 -- lib/private/lock/memcachelockingprovider.php | 9 +++++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/private/lock/abstractlockingprovider.php b/lib/private/lock/abstractlockingprovider.php index c7a29380efe..db5f1c72dd7 100644 --- a/lib/private/lock/abstractlockingprovider.php +++ b/lib/private/lock/abstractlockingprovider.php @@ -28,6 +28,8 @@ use OCP\Lock\ILockingProvider; * to release any left over locks at the end of the request */ abstract class AbstractLockingProvider implements ILockingProvider { + const TTL = 3600; // how long until we clear stray locks in seconds + protected $acquiredLocks = [ 'shared' => [], 'exclusive' => [] diff --git a/lib/private/lock/dblockingprovider.php b/lib/private/lock/dblockingprovider.php index 90657e6725f..b3a12bb9a07 100644 --- a/lib/private/lock/dblockingprovider.php +++ b/lib/private/lock/dblockingprovider.php @@ -51,8 +51,6 @@ class DBLockingProvider extends AbstractLockingProvider { private $sharedLocks = []; - const TTL = 3600; // how long until we clear stray locks in seconds - /** * Check if we have an open shared lock for a path * diff --git a/lib/private/lock/memcachelockingprovider.php b/lib/private/lock/memcachelockingprovider.php index e4158dcdfdf..af95200d159 100644 --- a/lib/private/lock/memcachelockingprovider.php +++ b/lib/private/lock/memcachelockingprovider.php @@ -21,6 +21,7 @@ namespace OC\Lock; +use OCP\IMemcacheTTL; use OCP\Lock\LockedException; use OCP\IMemcache; @@ -37,6 +38,12 @@ class MemcacheLockingProvider extends AbstractLockingProvider { $this->memcache = $memcache; } + private function setTTL($path) { + if ($this->memcache instanceof IMemcacheTTL) { + $this->memcache->setTTL($path, self::TTL); + } + } + /** * @param string $path * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE @@ -69,6 +76,7 @@ class MemcacheLockingProvider extends AbstractLockingProvider { throw new LockedException($path); } } + $this->setTTL($path); $this->markAcquire($path, $type); } @@ -106,6 +114,7 @@ class MemcacheLockingProvider extends AbstractLockingProvider { throw new LockedException($path); } } + $this->setTTL($path); $this->markChange($path, $targetType); } } -- cgit v1.2.3 From e41f7b005db6b03d2ab29f37dbbeac3ba892e5fe Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 9 Dec 2015 14:45:08 +0100 Subject: add since --- lib/public/imemcachettl.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/public/imemcachettl.php b/lib/public/imemcachettl.php index aae26accb48..a4a7a530549 100644 --- a/lib/public/imemcachettl.php +++ b/lib/public/imemcachettl.php @@ -32,6 +32,7 @@ interface IMemcacheTTL extends IMemcache { * * @param string $key * @param int $ttl time to live in seconds + * @since 9.0.0 */ public function setTTL($key, $ttl); } -- cgit v1.2.3