FastMovieAI AI短剧创作平台 二开基线
- 源码: xhadmincn/FastMovieAI (Apache 2.0) - 二开: Docker部署, Swoole改Select, route.php修复, 补update/VERSION - vendor/示例资源已gitignore
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace plugin\control\utils;
|
||||
|
||||
class LRUCache
|
||||
{
|
||||
private int $capacity;
|
||||
private array $cache = [];
|
||||
private array $order = [];
|
||||
|
||||
public function __construct($capacity = 2000)
|
||||
{
|
||||
$this->capacity = $capacity;
|
||||
}
|
||||
|
||||
public function get($key)
|
||||
{
|
||||
if (!isset($this->cache[$key])) {
|
||||
return null;
|
||||
}
|
||||
unset($this->order[$key]);
|
||||
$this->order[$key] = true;
|
||||
return $this->cache[$key];
|
||||
}
|
||||
|
||||
public function set($key, $value)
|
||||
{
|
||||
if (isset($this->cache[$key])) {
|
||||
$this->cache[$key] = $value;
|
||||
unset($this->order[$key]);
|
||||
$this->order[$key] = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (count($this->cache) >= $this->capacity) {
|
||||
$oldestKey = array_key_first($this->order);
|
||||
unset($this->cache[$oldestKey], $this->order[$oldestKey]);
|
||||
}
|
||||
|
||||
$this->cache[$key] = $value;
|
||||
$this->order[$key] = true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user