2016-01-12 07:14:04 -05:00
|
|
|
<?php
|
|
|
|
|
/**
|
2016-07-21 11:07:57 -04:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
|
*
|
2016-07-21 12:13:36 -04:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-01-12 07:14:04 -05:00
|
|
|
*
|
|
|
|
|
* @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,
|
2019-12-03 13:57:53 -05:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2016-01-12 07:14:04 -05:00
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
namespace OC\Cache;
|
|
|
|
|
|
|
|
|
|
use OCP\ICache;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* In-memory cache with a capacity limit to keep memory usage in check
|
|
|
|
|
*
|
|
|
|
|
* Uses a simple FIFO expiry mechanism
|
2022-03-02 13:24:26 -05:00
|
|
|
* @template T
|
2022-06-29 09:34:06 -04:00
|
|
|
* @deprecated use OCP\Cache\CappedMemoryCache instead
|
2016-01-12 07:14:04 -05:00
|
|
|
*/
|
|
|
|
|
class CappedMemoryCache implements ICache, \ArrayAccess {
|
|
|
|
|
private $capacity;
|
2022-03-02 13:24:26 -05:00
|
|
|
/** @var T[] */
|
2016-01-12 07:14:04 -05:00
|
|
|
private $cache = [];
|
|
|
|
|
|
|
|
|
|
public function __construct($capacity = 512) {
|
|
|
|
|
$this->capacity = $capacity;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-02 13:24:26 -05:00
|
|
|
public function hasKey($key): bool {
|
2016-01-12 07:14:04 -05:00
|
|
|
return isset($this->cache[$key]);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-02 13:24:26 -05:00
|
|
|
/**
|
|
|
|
|
* @return ?T
|
|
|
|
|
*/
|
2016-01-12 07:14:04 -05:00
|
|
|
public function get($key) {
|
2022-03-02 13:24:26 -05:00
|
|
|
return $this->cache[$key] ?? null;
|
2016-01-12 07:14:04 -05:00
|
|
|
}
|
|
|
|
|
|
2022-03-02 13:24:26 -05:00
|
|
|
/**
|
|
|
|
|
* @param string $key
|
2022-03-03 11:15:02 -05:00
|
|
|
* @param T $value
|
2022-03-02 13:24:26 -05:00
|
|
|
* @param int $ttl
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function set($key, $value, $ttl = 0): bool {
|
2017-03-29 13:16:12 -04:00
|
|
|
if (is_null($key)) {
|
|
|
|
|
$this->cache[] = $value;
|
|
|
|
|
} else {
|
|
|
|
|
$this->cache[$key] = $value;
|
|
|
|
|
}
|
2016-01-12 07:14:04 -05:00
|
|
|
$this->garbageCollect();
|
2022-03-02 13:24:26 -05:00
|
|
|
return true;
|
2016-01-12 07:14:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function remove($key) {
|
|
|
|
|
unset($this->cache[$key]);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function clear($prefix = '') {
|
|
|
|
|
$this->cache = [];
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-19 11:11:53 -04:00
|
|
|
public function offsetExists($offset): bool {
|
2016-01-12 07:14:04 -05:00
|
|
|
return $this->hasKey($offset);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-19 11:11:53 -04:00
|
|
|
/**
|
2022-03-02 13:24:26 -05:00
|
|
|
* @return T
|
2021-10-19 11:11:53 -04:00
|
|
|
*/
|
|
|
|
|
#[\ReturnTypeWillChange]
|
2016-01-14 09:00:41 -05:00
|
|
|
public function &offsetGet($offset) {
|
|
|
|
|
return $this->cache[$offset];
|
2016-01-12 07:14:04 -05:00
|
|
|
}
|
|
|
|
|
|
2022-03-02 13:24:26 -05:00
|
|
|
/**
|
2022-05-03 05:15:24 -04:00
|
|
|
* @param string $offset
|
2022-03-03 11:15:02 -05:00
|
|
|
* @param T $value
|
2022-03-02 13:24:26 -05:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2021-10-19 11:11:53 -04:00
|
|
|
public function offsetSet($offset, $value): void {
|
2016-01-12 07:14:04 -05:00
|
|
|
$this->set($offset, $value);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-19 11:11:53 -04:00
|
|
|
public function offsetUnset($offset): void {
|
2016-01-12 07:14:04 -05:00
|
|
|
$this->remove($offset);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-02 13:24:26 -05:00
|
|
|
/**
|
|
|
|
|
* @return T[]
|
|
|
|
|
*/
|
2016-11-03 09:35:44 -04:00
|
|
|
public function getData() {
|
|
|
|
|
return $this->cache;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-12 07:14:04 -05:00
|
|
|
|
|
|
|
|
private function garbageCollect() {
|
|
|
|
|
while (count($this->cache) > $this->capacity) {
|
|
|
|
|
reset($this->cache);
|
|
|
|
|
$key = key($this->cache);
|
|
|
|
|
$this->remove($key);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-19 17:30:34 -05:00
|
|
|
|
|
|
|
|
public static function isAvailable(): bool {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2016-01-12 07:14:04 -05:00
|
|
|
}
|