aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorCarl Schwan <carl@carlschwan.eu>2022-06-29 15:34:06 +0200
committerCarl Schwan <carl@carlschwan.eu>2022-07-14 15:54:31 +0200
commitd5c23dbb9fed1a1b958e07ebdb7202c37bddc074 (patch)
treecd650f88e9597500a6ed9dc680e67641274d5c5b /lib
parentd3f66e2310ef790794aba81f12d7ab6a035736c3 (diff)
downloadnextcloud-server-d5c23dbb9fed1a1b958e07ebdb7202c37bddc074.tar.gz
nextcloud-server-d5c23dbb9fed1a1b958e07ebdb7202c37bddc074.zip
Move CappedMemoryCache to OCP
This is an helpful helper that should be used in more place than just server and this is already the case with groupfodlers, deck, user_oidc and more using it, so let's make it public Signed-off-by: Carl Schwan <carl@carlschwan.eu>
Diffstat (limited to 'lib')
-rw-r--r--lib/composer/composer/autoload_classmap.php1
-rw-r--r--lib/composer/composer/autoload_static.php1
-rw-r--r--lib/private/Accounts/AccountManager.php2
-rw-r--r--lib/private/AllConfig.php2
-rw-r--r--lib/private/Authentication/Token/PublicKeyTokenProvider.php2
-rw-r--r--lib/private/Cache/CappedMemoryCache.php1
-rw-r--r--lib/private/Diagnostics/QueryLogger.php2
-rw-r--r--lib/private/Encryption/File.php2
-rw-r--r--lib/private/Files/AppData/AppData.php2
-rw-r--r--lib/private/Files/Config/UserMountCache.php2
-rw-r--r--lib/private/Files/Filesystem.php2
-rw-r--r--lib/private/Files/Mount/Manager.php2
-rw-r--r--lib/private/Files/Node/Root.php2
-rw-r--r--lib/private/Files/Storage/Wrapper/Encoding.php2
-rw-r--r--lib/private/Share20/Manager.php2
-rw-r--r--lib/private/User/Database.php2
-rw-r--r--lib/public/Cache/CappedMemoryCache.php160
17 files changed, 176 insertions, 13 deletions
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php
index e3572aa833c..e8510bc865e 100644
--- a/lib/composer/composer/autoload_classmap.php
+++ b/lib/composer/composer/autoload_classmap.php
@@ -116,6 +116,7 @@ return array(
'OCP\\BackgroundJob\\QueuedJob' => $baseDir . '/lib/public/BackgroundJob/QueuedJob.php',
'OCP\\BackgroundJob\\TimedJob' => $baseDir . '/lib/public/BackgroundJob/TimedJob.php',
'OCP\\Broadcast\\Events\\IBroadcastEvent' => $baseDir . '/lib/public/Broadcast/Events/IBroadcastEvent.php',
+ 'OCP\\Cache\\CappedMemoryCache' => $baseDir . '/lib/public/Cache/CappedMemoryCache.php',
'OCP\\Calendar\\BackendTemporarilyUnavailableException' => $baseDir . '/lib/public/Calendar/BackendTemporarilyUnavailableException.php',
'OCP\\Calendar\\Exceptions\\CalendarException' => $baseDir . '/lib/public/Calendar/Exceptions/CalendarException.php',
'OCP\\Calendar\\ICalendar' => $baseDir . '/lib/public/Calendar/ICalendar.php',
diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php
index 658f2cdfe2d..26b25e89aae 100644
--- a/lib/composer/composer/autoload_static.php
+++ b/lib/composer/composer/autoload_static.php
@@ -149,6 +149,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\BackgroundJob\\QueuedJob' => __DIR__ . '/../../..' . '/lib/public/BackgroundJob/QueuedJob.php',
'OCP\\BackgroundJob\\TimedJob' => __DIR__ . '/../../..' . '/lib/public/BackgroundJob/TimedJob.php',
'OCP\\Broadcast\\Events\\IBroadcastEvent' => __DIR__ . '/../../..' . '/lib/public/Broadcast/Events/IBroadcastEvent.php',
+ 'OCP\\Cache\\CappedMemoryCache' => __DIR__ . '/../../..' . '/lib/public/Cache/CappedMemoryCache.php',
'OCP\\Calendar\\BackendTemporarilyUnavailableException' => __DIR__ . '/../../..' . '/lib/public/Calendar/BackendTemporarilyUnavailableException.php',
'OCP\\Calendar\\Exceptions\\CalendarException' => __DIR__ . '/../../..' . '/lib/public/Calendar/Exceptions/CalendarException.php',
'OCP\\Calendar\\ICalendar' => __DIR__ . '/../../..' . '/lib/public/Calendar/ICalendar.php',
diff --git a/lib/private/Accounts/AccountManager.php b/lib/private/Accounts/AccountManager.php
index bdf33d73c29..20e0add1ccb 100644
--- a/lib/private/Accounts/AccountManager.php
+++ b/lib/private/Accounts/AccountManager.php
@@ -42,7 +42,7 @@ use libphonenumber\PhoneNumber;
use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberUtil;
use OC\Profile\TProfileHelper;
-use OC\Cache\CappedMemoryCache;
+use OCP\Cache\CappedMemoryCache;
use OCA\Settings\BackgroundJobs\VerifyUserData;
use OCP\Accounts\IAccount;
use OCP\Accounts\IAccountManager;
diff --git a/lib/private/AllConfig.php b/lib/private/AllConfig.php
index f282baee146..7e01e0ca815 100644
--- a/lib/private/AllConfig.php
+++ b/lib/private/AllConfig.php
@@ -32,7 +32,7 @@
*/
namespace OC;
-use OC\Cache\CappedMemoryCache;
+use OCP\Cache\CappedMemoryCache;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IConfig;
use OCP\IDBConnection;
diff --git a/lib/private/Authentication/Token/PublicKeyTokenProvider.php b/lib/private/Authentication/Token/PublicKeyTokenProvider.php
index 96bf9a86087..d21179a35c1 100644
--- a/lib/private/Authentication/Token/PublicKeyTokenProvider.php
+++ b/lib/private/Authentication/Token/PublicKeyTokenProvider.php
@@ -34,7 +34,7 @@ use OC\Authentication\Exceptions\InvalidTokenException;
use OC\Authentication\Exceptions\TokenPasswordExpiredException;
use OC\Authentication\Exceptions\PasswordlessTokenException;
use OC\Authentication\Exceptions\WipeTokenException;
-use OC\Cache\CappedMemoryCache;
+use OCP\Cache\CappedMemoryCache;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IConfig;
diff --git a/lib/private/Cache/CappedMemoryCache.php b/lib/private/Cache/CappedMemoryCache.php
index 6063b5e7110..31e8ef3e720 100644
--- a/lib/private/Cache/CappedMemoryCache.php
+++ b/lib/private/Cache/CappedMemoryCache.php
@@ -28,6 +28,7 @@ use OCP\ICache;
*
* Uses a simple FIFO expiry mechanism
* @template T
+ * @deprecated use OCP\Cache\CappedMemoryCache instead
*/
class CappedMemoryCache implements ICache, \ArrayAccess {
private $capacity;
diff --git a/lib/private/Diagnostics/QueryLogger.php b/lib/private/Diagnostics/QueryLogger.php
index 40d68d94ae3..5f401751077 100644
--- a/lib/private/Diagnostics/QueryLogger.php
+++ b/lib/private/Diagnostics/QueryLogger.php
@@ -24,7 +24,7 @@
*/
namespace OC\Diagnostics;
-use OC\Cache\CappedMemoryCache;
+use OCP\Cache\CappedMemoryCache;
use OCP\Diagnostics\IQueryLogger;
class QueryLogger implements IQueryLogger {
diff --git a/lib/private/Encryption/File.php b/lib/private/Encryption/File.php
index 2d7e23a8883..844059923bd 100644
--- a/lib/private/Encryption/File.php
+++ b/lib/private/Encryption/File.php
@@ -27,7 +27,7 @@
*/
namespace OC\Encryption;
-use OC\Cache\CappedMemoryCache;
+use OCP\Cache\CappedMemoryCache;
use OCA\Files_External\Service\GlobalStoragesService;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
diff --git a/lib/private/Files/AppData/AppData.php b/lib/private/Files/AppData/AppData.php
index 471de799c2f..237fcb42e03 100644
--- a/lib/private/Files/AppData/AppData.php
+++ b/lib/private/Files/AppData/AppData.php
@@ -26,7 +26,7 @@ declare(strict_types=1);
*/
namespace OC\Files\AppData;
-use OC\Cache\CappedMemoryCache;
+use OCP\Cache\CappedMemoryCache;
use OC\Files\SimpleFS\SimpleFolder;
use OC\SystemConfig;
use OCP\Files\Folder;
diff --git a/lib/private/Files/Config/UserMountCache.php b/lib/private/Files/Config/UserMountCache.php
index c326eeb0b6c..f26c42938d3 100644
--- a/lib/private/Files/Config/UserMountCache.php
+++ b/lib/private/Files/Config/UserMountCache.php
@@ -28,7 +28,7 @@
*/
namespace OC\Files\Config;
-use OC\Cache\CappedMemoryCache;
+use OCP\Cache\CappedMemoryCache;
use OCA\Files_Sharing\SharedMount;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Files\Config\ICachedMountFileInfo;
diff --git a/lib/private/Files/Filesystem.php b/lib/private/Files/Filesystem.php
index 20b44e2736a..9542666b03c 100644
--- a/lib/private/Files/Filesystem.php
+++ b/lib/private/Files/Filesystem.php
@@ -37,7 +37,7 @@
*/
namespace OC\Files;
-use OC\Cache\CappedMemoryCache;
+use OCP\Cache\CappedMemoryCache;
use OC\Files\Mount\MountPoint;
use OC\User\NoUserException;
use OCP\EventDispatcher\IEventDispatcher;
diff --git a/lib/private/Files/Mount/Manager.php b/lib/private/Files/Mount/Manager.php
index 69285018d17..9ba0e504058 100644
--- a/lib/private/Files/Mount/Manager.php
+++ b/lib/private/Files/Mount/Manager.php
@@ -29,7 +29,7 @@ declare(strict_types=1);
namespace OC\Files\Mount;
-use OC\Cache\CappedMemoryCache;
+use OCP\Cache\CappedMemoryCache;
use OC\Files\Filesystem;
use OC\Files\SetupManager;
use OC\Files\SetupManagerFactory;
diff --git a/lib/private/Files/Node/Root.php b/lib/private/Files/Node/Root.php
index 6dd65a4291d..9e3d4afd8d8 100644
--- a/lib/private/Files/Node/Root.php
+++ b/lib/private/Files/Node/Root.php
@@ -32,7 +32,7 @@
namespace OC\Files\Node;
-use OC\Cache\CappedMemoryCache;
+use OCP\Cache\CappedMemoryCache;
use OC\Files\FileInfo;
use OC\Files\Mount\Manager;
use OC\Files\Mount\MountPoint;
diff --git a/lib/private/Files/Storage/Wrapper/Encoding.php b/lib/private/Files/Storage/Wrapper/Encoding.php
index d6201dc8877..ac9cc248ce6 100644
--- a/lib/private/Files/Storage/Wrapper/Encoding.php
+++ b/lib/private/Files/Storage/Wrapper/Encoding.php
@@ -28,7 +28,7 @@
*/
namespace OC\Files\Storage\Wrapper;
-use OC\Cache\CappedMemoryCache;
+use OCP\Cache\CappedMemoryCache;
use OC\Files\Filesystem;
use OCP\Files\Storage\IStorage;
use OCP\ICache;
diff --git a/lib/private/Share20/Manager.php b/lib/private/Share20/Manager.php
index b1a9783d7b8..905a006372f 100644
--- a/lib/private/Share20/Manager.php
+++ b/lib/private/Share20/Manager.php
@@ -41,7 +41,7 @@
*/
namespace OC\Share20;
-use OC\Cache\CappedMemoryCache;
+use OCP\Cache\CappedMemoryCache;
use OC\Files\Mount\MoveableMount;
use OC\KnownUser\KnownUserService;
use OC\Share20\Exception\ProviderException;
diff --git a/lib/private/User/Database.php b/lib/private/User/Database.php
index fce7551c242..0b38f04bfe3 100644
--- a/lib/private/User/Database.php
+++ b/lib/private/User/Database.php
@@ -45,7 +45,7 @@ declare(strict_types=1);
*/
namespace OC\User;
-use OC\Cache\CappedMemoryCache;
+use OCP\Cache\CappedMemoryCache;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IDBConnection;
use OCP\Security\Events\ValidatePasswordPolicyEvent;
diff --git a/lib/public/Cache/CappedMemoryCache.php b/lib/public/Cache/CappedMemoryCache.php
new file mode 100644
index 00000000000..6699600d42c
--- /dev/null
+++ b/lib/public/Cache/CappedMemoryCache.php
@@ -0,0 +1,160 @@
+<?php
+/**
+ * @copyright Copyright (c) 2016, ownCloud, Inc.
+ *
+ * @author Robin Appelman <robin@icewind.nl>
+ *
+ * @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 <http://www.gnu.org/licenses/>
+ *
+ */
+namespace OCP\Cache;
+
+use OCP\ICache;
+
+/**
+ * In-memory cache with a capacity limit to keep memory usage in check
+ *
+ * Uses a simple FIFO expiry mechanism
+ *
+ * @since 25.0.0
+ * @template T
+ */
+class CappedMemoryCache implements ICache, \ArrayAccess {
+ private int $capacity;
+ /** @var T[] */
+ private array $cache = [];
+
+ /**
+ * @inheritdoc
+ * @since 25.0.0
+ */
+ public function __construct(int $capacity = 512) {
+ $this->capacity = $capacity;
+ }
+
+ /**
+ * @inheritdoc
+ * @since 25.0.0
+ */
+ public function hasKey($key): bool {
+ return isset($this->cache[$key]);
+ }
+
+ /**
+ * @return ?T
+ * @since 25.0.0
+ */
+ public function get($key) {
+ return $this->cache[$key] ?? null;
+ }
+
+ /**
+ * @inheritdoc
+ * @param string $key
+ * @param T $value
+ * @param int $ttl
+ * @since 25.0.0
+ * @return bool
+ */
+ public function set($key, $value, $ttl = 0): bool {
+ if (is_null($key)) {
+ $this->cache[] = $value;
+ } else {
+ $this->cache[$key] = $value;
+ }
+ $this->garbageCollect();
+ return true;
+ }
+
+ /**
+ * @since 25.0.0
+ */
+ public function remove($key): bool {
+ unset($this->cache[$key]);
+ return true;
+ }
+
+ /**
+ * @inheritdoc
+ * @since 25.0.0
+ */
+ public function clear($prefix = ''): bool {
+ $this->cache = [];
+ return true;
+ }
+
+ /**
+ * @since 25.0.0
+ */
+ public function offsetExists($offset): bool {
+ return $this->hasKey($offset);
+ }
+
+ /**
+ * @inheritdoc
+ * @return T
+ * @since 25.0.0
+ */
+ #[\ReturnTypeWillChange]
+ public function &offsetGet($offset) {
+ return $this->cache[$offset];
+ }
+
+ /**
+ * @inheritdoc
+ * @param string $offset
+ * @param T $value
+ * @since 25.0.0
+ */
+ public function offsetSet($offset, $value): void {
+ $this->set($offset, $value);
+ }
+
+ /**
+ * @inheritdoc
+ * @since 25.0.0
+ */
+ public function offsetUnset($offset): void {
+ $this->remove($offset);
+ }
+
+ /**
+ * @return T[]
+ * @since 25.0.0
+ */
+ public function getData(): array {
+ return $this->cache;
+ }
+
+
+ /**
+ * @since 25.0.0
+ */
+ private function garbageCollect(): void {
+ while (count($this->cache) > $this->capacity) {
+ reset($this->cache);
+ $key = key($this->cache);
+ $this->remove($key);
+ }
+ }
+
+ /**
+ * @inheritdoc
+ * @since 25.0.0
+ */
+ public static function isAvailable(): bool {
+ return true;
+ }
+}