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

Move CappedMemoryCache to OCP #33064

Merged
merged 1 commit into from Jul 18, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/files_external/lib/Lib/Storage/AmazonS3.php
Expand Up @@ -44,7 +44,7 @@
use Aws\S3\S3Client;
use Icewind\Streams\CallbackWrapper;
use Icewind\Streams\IteratorDirectory;
use OC\Cache\CappedMemoryCache;
use OCP\Cache\CappedMemoryCache;
use OC\Files\Cache\CacheEntry;
use OC\Files\ObjectStore\S3ConnectionTrait;
use OC\Files\ObjectStore\S3ObjectTrait;
Expand Down
2 changes: 1 addition & 1 deletion apps/files_external/lib/Lib/Storage/SMB.php
Expand Up @@ -54,7 +54,7 @@
use Icewind\SMB\System;
use Icewind\Streams\CallbackWrapper;
use Icewind\Streams\IteratorDirectory;
use OC\Cache\CappedMemoryCache;
use OCP\Cache\CappedMemoryCache;
use OC\Files\Filesystem;
use OC\Files\Storage\Common;
use OCA\Files_External\Lib\Notify\SMBNotifyHandler;
Expand Down
2 changes: 1 addition & 1 deletion apps/files_external/lib/Lib/Storage/Swift.php
Expand Up @@ -200,7 +200,7 @@ public function __construct($params) {

$this->params = $params;
// FIXME: private class...
$this->objectCache = new \OC\Cache\CappedMemoryCache();
$this->objectCache = new \OCP\Cache\CappedMemoryCache();
Fixed Show fixed Hide fixed
$this->connectionFactory = new SwiftFactory(
\OC::$server->getMemCacheFactory()->createDistributed('swift/'),
$this->params,
Expand Down
2 changes: 1 addition & 1 deletion apps/files_sharing/lib/MountProvider.php
Expand Up @@ -28,7 +28,7 @@
*/
namespace OCA\Files_Sharing;

use OC\Cache\CappedMemoryCache;
use OCP\Cache\CappedMemoryCache;
use OC\Files\View;
use OCA\Files_Sharing\Event\ShareMountedEvent;
use OCP\EventDispatcher\IEventDispatcher;
Expand Down
2 changes: 1 addition & 1 deletion apps/files_sharing/lib/SharedMount.php
Expand Up @@ -29,7 +29,7 @@

namespace OCA\Files_Sharing;

use OC\Cache\CappedMemoryCache;
use OCP\Cache\CappedMemoryCache;
use OC\Files\Filesystem;
use OC\Files\Mount\MountPoint;
use OC\Files\Mount\MoveableMount;
Expand Down
2 changes: 1 addition & 1 deletion apps/user_ldap/lib/Group_LDAP.php
Expand Up @@ -45,7 +45,7 @@
use Closure;
use Exception;
use OC;
use OC\Cache\CappedMemoryCache;
use OCP\Cache\CappedMemoryCache;
use OC\ServerNotAvailableException;
use OCP\Group\Backend\IGetDisplayNameBackend;
use OCP\Group\Backend\IDeleteGroupBackend;
Expand Down
2 changes: 1 addition & 1 deletion apps/user_ldap/lib/Helper.php
Expand Up @@ -29,7 +29,7 @@
*/
namespace OCA\User_LDAP;

use OC\Cache\CappedMemoryCache;
use OCP\Cache\CappedMemoryCache;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IConfig;
use OCP\IDBConnection;
Expand Down
2 changes: 1 addition & 1 deletion apps/user_ldap/lib/User/Manager.php
Expand Up @@ -28,7 +28,7 @@
*/
namespace OCA\User_LDAP\User;

use OC\Cache\CappedMemoryCache;
use OCP\Cache\CappedMemoryCache;
use OCA\User_LDAP\Access;
use OCA\User_LDAP\FilesystemHelper;
use OCP\IAvatarManager;
Expand Down
2 changes: 1 addition & 1 deletion apps/workflowengine/lib/Manager.php
Expand Up @@ -30,7 +30,7 @@
namespace OCA\WorkflowEngine;

use Doctrine\DBAL\Exception;
use OC\Cache\CappedMemoryCache;
use OCP\Cache\CappedMemoryCache;
use OCA\WorkflowEngine\AppInfo\Application;
use OCA\WorkflowEngine\Check\FileMimeType;
use OCA\WorkflowEngine\Check\FileName;
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Expand Up @@ -116,6 +116,7 @@
'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',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Accounts/AccountManager.php
Expand Up @@ -42,7 +42,7 @@
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;
Expand Down
2 changes: 1 addition & 1 deletion lib/private/AllConfig.php
Expand Up @@ -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;
Expand Down
Expand Up @@ -34,7 +34,7 @@
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;
Expand Down
1 change: 1 addition & 0 deletions lib/private/Cache/CappedMemoryCache.php
Expand Up @@ -28,6 +28,7 @@
*
* Uses a simple FIFO expiry mechanism
* @template T
* @deprecated use OCP\Cache\CappedMemoryCache instead
*/
class CappedMemoryCache implements ICache, \ArrayAccess {
private $capacity;
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Diagnostics/QueryLogger.php
Expand Up @@ -24,7 +24,7 @@
*/
namespace OC\Diagnostics;

use OC\Cache\CappedMemoryCache;
use OCP\Cache\CappedMemoryCache;
use OCP\Diagnostics\IQueryLogger;

class QueryLogger implements IQueryLogger {
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Encryption/File.php
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Files/AppData/AppData.php
Expand Up @@ -26,7 +26,7 @@
*/
namespace OC\Files\AppData;

use OC\Cache\CappedMemoryCache;
use OCP\Cache\CappedMemoryCache;
use OC\Files\SimpleFS\SimpleFolder;
use OC\SystemConfig;
use OCP\Files\Folder;
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Files/Config/UserMountCache.php
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Files/Filesystem.php
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Files/Mount/Manager.php
Expand Up @@ -29,7 +29,7 @@

namespace OC\Files\Mount;

use OC\Cache\CappedMemoryCache;
use OCP\Cache\CappedMemoryCache;
use OC\Files\Filesystem;
use OC\Files\SetupManager;
use OC\Files\SetupManagerFactory;
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Files/Node/Root.php
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Files/Storage/Wrapper/Encoding.php
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Share20/Manager.php
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/private/User/Database.php
Expand Up @@ -45,7 +45,7 @@
*/
namespace OC\User;

use OC\Cache\CappedMemoryCache;
use OCP\Cache\CappedMemoryCache;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IDBConnection;
use OCP\Security\Events\ValidatePasswordPolicyEvent;
Expand Down
160 changes: 160 additions & 0 deletions 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;
}
}