This commit is contained in:
me
2026-07-23 15:50:56 +07:00
parent 9ec08a3e44
commit 7448b5ea2a
9 changed files with 172 additions and 705 deletions
-21
View File
@@ -1,21 +0,0 @@
<?php
namespace Nightmare\Cache\Driver;
use Nightmare\Fs;
class File
{
private $opt;
public function __construct() {
}
public function has($key) {}
public function get($key, $default = null) {}
public function set($key, $value, $ttl) {}
public function unset($key) {}
public function clear() {}
public function clear_prefix($prefix) {}
}
-41
View File
@@ -136,47 +136,6 @@ class Fs
throw new Exception('remove error, not match file type');
}
/**
* @param string $path
* @param array $excludes
* @return RecursiveIteratorIterator
*/
public static function read_full_dir($path, $excludes = [])
{
$directory = new RecursiveDirectoryIterator(
$path,
FilesystemIterator::UNIX_PATHS
| FilesystemIterator::SKIP_DOTS
);
$filter = new RecursiveCallbackFilterIterator($directory, function ($current, $key, $iterator) use ($path, $excludes) {
$relativePath = Str::replace_first($path, '', $current->getPathname());
foreach ($excludes as $exclude) {
if (empty($exclude)) {
continue;
}
//var_dump($relativePath);
//var_dump($exclude);
$exclude = trim($exclude);
$exclude = trim($exclude, '/');
$relativePath = trim($relativePath, '/');
if (str_ends_with($relativePath, $exclude)) {
return false;
}
}
return true;
});
return new RecursiveIteratorIterator(
$filter,
RecursiveIteratorIterator::SELF_FIRST
);
}
/**
* @param string ...$paths
* @return string
-57
View File
@@ -1,57 +0,0 @@
<?php
namespace Nightmare\Session\Storage;
use SessionHandlerInterface;
use ReturnTypeWillChange;
class Apcu implements SessionHandlerInterface
{
private string $prefix;
private int $ttl;
public function __construct(string $prefix = 'sess_', int $ttl = 86400)
{
$this->prefix = $prefix;
$this->ttl = $ttl;
}
#[ReturnTypeWillChange]
public function close()
{
return true;
}
#[ReturnTypeWillChange]
public function destroy($id)
{
$key = $this->prefix . $id;
return apcu_delete($key);
}
#[ReturnTypeWillChange]
public function gc($max_lifetime)
{
return 1;
}
#[ReturnTypeWillChange]
public function open($path, $name)
{
return true;
}
#[ReturnTypeWillChange]
public function read($id)
{
$key = $this->prefix . $id;
return apcu_exists($key) ? apcu_fetch($key) : '';
}
#[ReturnTypeWillChange]
public function write($id, $data)
{
$key = $this->prefix . $id;
return apcu_store($key, $data, $this->ttl);
}
}
View File
-207
View File
@@ -1,207 +0,0 @@
<?php
namespace nightmare;
class cache
{
/**
* @var bool
*/
private static $debug = false;
/**
* @var int|null
*/
private static $expire = null;
/**
* @var string
*/
private static $prefix = '';
/**
* @var mixed
*/
private static $adapter;
/**
* @var array
*/
private static $adapters = [];
// common
/**
* @param bool $debug
* @return void
*/
public static function set_debug($debug)
{
self::$debug = $debug;
}
/**
* @param string $prefix
* @return void
*/
public static function set_prefix($prefix)
{
self::$prefix = $prefix;
}
/**
* @param int|null $ttl
* @return void
*/
public static function set_expire($ttl = null)
{
self::$expire = $ttl;
}
// adapter
/**
* @param string $key
* @return void
*/
public static function set_adapter($key)
{
self::$adapter = self::$adapters[$key];
}
/**
* @param string $key
* @return mixed
*/
public static function get_adapter($key)
{
return self::$adapters[$key];
}
/**
* @param string $key
* @param mixed $adapter
* @return void
*/
public static function add_adapter($key, $adapter)
{
self::$adapters = array_merge(self::$adapters, [$key => $adapter]);
}
/**
* @param string $key
* @return void
*/
public function remove_adapter($key)
{
unset(self::$adapters[$key]);
}
/**
* @return array
*/
public static function get_adapters()
{
return self::$adapters;
}
// cache
/**
* @param string $key
* @return bool
*/
public static function has($key)
{
return self::$adapter->hasItem(self::$prefix . $key);
}
// truyen 1 tham so - lay binh thuong
// truyen 2 tham so tro len - luu cache cho lan sau
/**
* @param string $key
* @param mixed $default
* @param array $opt
* @return mixed
*/
public static function get($key, $default = null, $opt = [])
{
$opt += [
'expire' => self::$expire,
'debug' => false,
'save' => true,
'save_if' => null, // ?callable
];
if (self::$debug || $opt['debug']) {
self::unset(self::$prefix . $key);
}
// get cache
$item = self::$adapter->getItem(self::$prefix . $key);
if ($item->isHit()) {
return $item->get();
} else {
if (is_callable($default)) {
$default = call_user_func($default, $opt);
}
if ($opt['save']) {
$save = false;
if (is_callable($opt['save_if'])) {
if (call_user_func($opt['save_if'], $default)) {
$save = true;
}
} else {
$save = true;
}
if ($save) {
self::set($key, $default, $opt['expire']);
}
}
return $default;
}
}
/**
* @param string $key
* @param mixed $value
* @param int|null $expire
* @return bool
*/
public static function set($key, $value, $expire = null)
{
$item = self::$adapter->getItem(self::$prefix . $key);
if ($expire !== null) {
$item->expiresAfter($expire);
} elseif (self::$expire !== null) {
$item->expiresAfter(self::$expire);
}
$item->set($value);
return self::$adapter->save($item);
}
/**
* @param string $key
* @return bool
*/
public static function unset($key)
{
return self::$adapter->deleteItem(self::$prefix . $key);
}
/**
* @param string $prefix
* @return bool
*/
public static function clear($prefix = '')
{
return self::$adapter->clear($prefix);
}
}
View File
-207
View File
@@ -1,207 +0,0 @@
<?php
namespace nightmare;
class cache
{
/**
* @var bool
*/
private $debug = false;
/**
* @var int|null
*/
private $expire = null;
/**
* @var string
*/
private $prefix = '';
/**
* @var mixed
*/
private $adapter;
/**
* @var array
*/
private $adapters = [];
// common
/**
* @param bool $debug
* @return void
*/
public function set_debug($debug)
{
self::$debug = $debug;
}
/**
* @param string $prefix
* @return void
*/
public function set_prefix($prefix)
{
self::$prefix = $prefix;
}
/**
* @param int|null $ttl
* @return void
*/
public function set_expire($ttl = null)
{
self::$expire = $ttl;
}
// adapter
/**
* @param string $key
* @return void
*/
public function set_adapter($key)
{
self::$adapter = self::$adapters[$key];
}
/**
* @param string $key
* @return mixed
*/
public function get_adapter($key)
{
return self::$adapters[$key];
}
/**
* @param string $key
* @param mixed $adapter
* @return void
*/
public function add_adapter($key, $adapter)
{
self::$adapters = array_merge(self::$adapters, [$key => $adapter]);
}
/**
* @param string $key
* @return void
*/
public function remove_adapter($key)
{
unset(self::$adapters[$key]);
}
/**
* @return array
*/
public function get_adapters()
{
return self::$adapters;
}
// cache
/**
* @param string $key
* @return bool
*/
public function has($key)
{
return self::$adapter->hasItem(self::$prefix . $key);
}
// truyen 1 tham so - lay binh thuong
// truyen 2 tham so tro len - luu cache cho lan sau
/**
* @param string $key
* @param mixed $default
* @param array $opt
* @return mixed
*/
public function get($key, $default = null, $opt = [])
{
$opt += [
'expire' => self::$expire,
'debug' => false,
'save' => true,
'save_if' => null, // ?callable
];
if (self::$debug || $opt['debug']) {
self::unset(self::$prefix . $key);
}
// get cache
$item = self::$adapter->getItem(self::$prefix . $key);
if ($item->isHit()) {
return $item->get();
} else {
if (is_callable($default)) {
$default = call_user_func($default, $opt);
}
if ($opt['save']) {
$save = false;
if (is_callable($opt['save_if'])) {
if (call_user_func($opt['save_if'], $default)) {
$save = true;
}
} else {
$save = true;
}
if ($save) {
self::set($key, $default, $opt['expire']);
}
}
return $default;
}
}
/**
* @param string $key
* @param mixed $value
* @param int|null $expire
* @return bool
*/
public function set($key, $value, $expire = null)
{
$item = self::$adapter->getItem(self::$prefix . $key);
if ($expire !== null) {
$item->expiresAfter($expire);
} elseif (self::$expire !== null) {
$item->expiresAfter(self::$expire);
}
$item->set($value);
return self::$adapter->save($item);
}
/**
* @param string $key
* @return bool
*/
public function unset($key)
{
return self::$adapter->deleteItem(self::$prefix . $key);
}
/**
* @param string $prefix
* @return bool
*/
public function clear($prefix = '')
{
return self::$adapter->clear($prefix);
}
}
View File
View File