仓库初始化

This commit is contained in:
2025-08-13 10:43:56 +08:00
commit e8f9b46680
5180 changed files with 859303 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
<?php
/**
* Created by PhpStorm.
* User: Jenner
* Date: 2015/8/12
* Time: 14:59
*/
namespace Jenner\SimpleFork\Cache;
/**
* cache for processes shared variables
*
* @package Jenner\SimpleFork\Cache
*/
interface CacheInterface
{
/**
* get var
*
* @param $key
* @param null $default
* @return bool|mixed
*/
public function get($key, $default = null);
/**
* set var
*
* @param $key
* @param null $value
* @return
*/
public function set($key, $value);
/**
* has var ?
*
* @param $key
* @return bool
*/
public function has($key);
/**
* delete var
*
* @param $key
* @return bool
*/
public function delete($key);
}

View File

@@ -0,0 +1,263 @@
<?php
/**
* Created by PhpStorm.
* User: Jenner
* Date: 2016/6/22
* Time: 16:18
*/
namespace Jenner\SimpleFork\Cache;
class FileCache implements CacheInterface
{
/**
* 缓存目录
* @var
*/
private $cache_dir;
/**
* @param string $cache_dir
* @throws \Exception
*/
public function __construct($cache_dir)
{
$this->cache_dir = $cache_dir;
if (!is_dir($cache_dir)) {
$make_dir_result = mkdir($cache_dir, 0755, true);
if ($make_dir_result === false) throw new \Exception('Cannot create the cache directory');
}
}
/**
* get value by key, and check if it is expired
* @param string $key
* @param string $default
* @return mixed
*/
public function get($key, $default = null)
{
$cache_data = $this->getItem($key);
if ($cache_data === false || !is_array($cache_data)) return $default;
return $cache_data['data'];
}
/**
* 添加或覆盖一个key
* @param string $key
* @param mixed $value
* @param int $expire expire time in seconds
* @return mixed
*/
public function set($key, $value, $expire = 0)
{
return $this->setItem($key, $value, time(), $expire);
}
/**
* 设置包含元数据的信息
* @param $key
* @param $value
* @param $time
* @param $expire
* @return bool
*/
private function setItem($key, $value, $time, $expire)
{
$cache_file = $this->createCacheFile($key);
if ($cache_file === false) return false;
$cache_data = array('data' => $value, 'time' => $time, 'expire' => $expire);
$cache_data = serialize($cache_data);
$put_result = file_put_contents($cache_file, $cache_data);
if ($put_result === false) return false;
return true;
}
/**
* 创建缓存文件
* @param $key
* @return bool|string
*/
private function createCacheFile($key)
{
$cache_file = $this->path($key);
if (!file_exists($cache_file)) {
$directory = dirname($cache_file);
if (!is_dir($directory)) {
$make_dir_result = mkdir($directory, 0755, true);
if ($make_dir_result === false) return false;
}
$create_result = touch($cache_file);
if ($create_result === false) return false;
}
return $cache_file;
}
/**
* 判断Key是否存在
* @param $key
* @return mixed
*/
public function has($key)
{
$value = $this->get($key);
if ($value === false) return false;
return true;
}
/**
* 加法递增
* @param $key
* @param int $value
* @return mixed
*/
public function increment($key, $value = 1)
{
$item = $this->getItem($key);
if ($item === false) {
$set_result = $this->set($key, $value);
if ($set_result === false) return false;
return $value;
}
$check_expire = $this->checkExpire($item);
if ($check_expire === false) return false;
$item['data'] += $value;
$result = $this->setItem($key, $item['data'], $item['time'], $item['expire']);
if ($result === false) return false;
return $item['data'];
}
/**
* 减法递增
* @param $key
* @param int $value
* @return mixed
*/
public function decrement($key, $value = 1)
{
$item = $this->getItem($key);
if ($item === false) {
$value = 0 - $value;
$set_result = $this->set($key, $value);
if ($set_result === false) return false;
return $value;
}
$check_expire = $this->checkExpire($item);
if ($check_expire === false) return false;
$item['data'] -= $value;
$result = $this->setItem($key, $item['data'], $item['time'], $item['expire']);
if ($result === false) return false;
return $item['data'];
}
/**
* 删除一个key同事会删除缓存文件
* @param $key
* @return boolean
*/
public function delete($key)
{
$cache_file = $this->path($key);
if (file_exists($cache_file)) {
$unlink_result = unlink($cache_file);
if ($unlink_result === false) return false;
}
return true;
}
/**
* 清楚所有缓存
* @return mixed
*/
public function flush()
{
return $this->delTree($this->cache_dir);
}
/**
* 递归删除目录
* @param $dir
* @return bool
*/
function delTree($dir)
{
$files = array_diff(scandir($dir), array('.', '..'));
foreach ($files as $file) {
(is_dir("$dir/$file")) ? $this->delTree("$dir/$file") : unlink("$dir/$file");
}
return rmdir($dir);
}
/**
* 根据key获取缓存文件路径
*
* @param string $key
* @return string
*/
protected function path($key)
{
$parts = array_slice(str_split($hash = md5($key), 2), 0, 2);
return $this->cache_dir . '/' . implode('/', $parts) . '/' . $hash;
}
/**
* 获取含有元数据的信息
* @param $key
* @return bool|mixed|string
*/
protected function getItem($key)
{
$cache_file = $this->path($key);
if (!file_exists($cache_file) || !is_readable($cache_file)) {
return false;
}
$data = file_get_contents($cache_file);
if (empty($data)) return false;
$cache_data = unserialize($data);
if ($cache_data === false) {
return false;
}
$check_expire = $this->checkExpire($cache_data);
if ($check_expire === false) {
$this->delete($key);
return false;
}
return $cache_data;
}
/**
* 检查key是否过期
* @param $cache_data
* @return bool
*/
protected function checkExpire($cache_data)
{
$time = time();
$is_expire = intval($cache_data['expire']) !== 0 && (intval($cache_data['time']) + intval($cache_data['expire']) < $time);
if ($is_expire) return false;
return true;
}
}

View File

@@ -0,0 +1,126 @@
<?php
/**
* Created by PhpStorm.
* User: Jenner
* Date: 2015/8/20
* Time: 15:14
*/
namespace Jenner\SimpleFork\Cache;
/**
* redis cache
*
* @package Jenner\SimpleFork\Cache
*/
class RedisCache implements CacheInterface
{
/**
* @var \Redis
*/
protected $redis;
protected $prefix;
/**
* @param string $host
* @param int $port
* @param int $database
* @param string $prefix
*/
public function __construct(
$host = '127.0.0.1',
$port = 6379,
$database = 0,
$prefix = 'simple-fork'
)
{
$this->redis = new \Redis();
$connection_result = $this->redis->connect($host, $port);
if (!$connection_result) {
throw new \RuntimeException('can not connect to the redis server');
}
if ($database != 0) {
$select_result = $this->redis->select($database);
if (!$select_result) {
throw new \RuntimeException('can not select the database');
}
}
if (empty($prefix)) {
throw new \InvalidArgumentException('prefix can not be empty');
}
$this->prefix = $prefix;
}
/**
* close redis connection
*/
public function __destruct()
{
$this->close();
}
/**
* close the connection
*/
public function close()
{
$this->redis->close();
}
/**
* get var
*
* @param $key
* @param null $default
* @return bool|string|null
*/
public function get($key, $default = null)
{
$result = $this->redis->hGet($this->prefix, $key);
if ($result !== false) return $result;
return $default;
}
/**
* set var
*
* @param $key
* @param null $value
* @return boolean
*/
public function set($key, $value)
{
return $this->redis->hSet($this->prefix, $key, $value);
}
/**
* has var ?
*
* @param $key
* @return bool
*/
public function has($key)
{
return $this->redis->hExists($this->prefix, $key);
}
/**
* delete var
*
* @param $key
* @return bool
*/
public function delete($key)
{
if ($this->redis->hDel($this->prefix, $key) > 0) {
return true;
}
return false;
}
}

View File

@@ -0,0 +1,176 @@
<?php
/**
* Created by PhpStorm.
* User: Jenner
* Date: 2015/8/12
* Time: 15:00
*/
namespace Jenner\SimpleFork\Cache;
/**
* shared memory cache
*
* @package Jenner\SimpleFork\Cache
*/
class SharedMemory implements CacheInterface
{
/**
* holds shared memory resource
* @var resource
*/
protected $shm;
/**
* shared memory ipc key
* @var string
*/
protected $client_count_key = 'system_client_count';
/**
* memory size
* @var int
*/
protected $size;
/**
* @param int $size memory size
* @param string $file
*/
public function __construct($size = 33554432, $file = __FILE__)
{
$this->size = $size;
if (function_exists("shm_attach") === false) {
$message = "\nYour PHP configuration needs adjustment. " .
"See: http://us2.php.net/manual/en/shmop.setup.php. " .
"To enable the System V shared memory support compile " .
" PHP with the option --enable-sysvshm.";
throw new \RuntimeException($message);
}
$this->attach($file); //create resources (shared memory)
}
/**
* connect shared memory
*
* @param string $file
*/
public function attach($file = __FILE__)
{
if (!file_exists($file)) {
$touch = touch($file);
if (!$touch) {
throw new \RuntimeException("file is not exists and it can not be created. file: {$file}");
}
}
$key = ftok($file, 'a');
$this->shm = shm_attach($key, $this->size); //allocate shared memory
}
/**
* remove shared memory.
* you should know that it maybe does not work.
*
* @return bool
*/
public function remove()
{
//dallocate shared memory
if (!shm_remove($this->shm)) {
return false;
}
$this->dettach();
// shm_remove maybe not working. it likes a php bug.
unset($this->shm);
return true;
}
/**
* @return bool
*/
public function dettach()
{
return shm_detach($this->shm); //allocate shared memory
}
/**
* set var
*
* @param $key
* @param $value
* @return bool
*/
public function set($key, $value)
{
return shm_put_var($this->shm, $this->shm_key($key), $value); //store var
}
/**
* generate shm key
*
* @param $val
* @return mixed
*/
public function shm_key($val)
{ // enable all world langs and chars !
// text to number system.
return preg_replace("/[^0-9]/", "", (preg_replace("/[^0-9]/", "", md5($val)) / 35676248) / 619876);
}
/**
* get var
*
* @param $key
* @param null $default
* @return bool|mixed
*/
public function get($key, $default = null)
{
if ($this->has($key)) {
return shm_get_var($this->shm, $this->shm_key($key));
} else {
return $default;
}
}
/**
* has var ?
*
* @param $key
* @return bool
*/
public function has($key)
{
if (shm_has_var($this->shm, $this->shm_key($key))) { // check is isset
return true;
} else {
return false;
}
}
/**
* delete var
*
* @param $key
* @return bool
*/
public function delete($key)
{
if ($this->has($key)) {
return shm_remove_var($this->shm, $this->shm_key($key));
} else {
return false;
}
}
/**
* init when wakeup
*/
public function __wakeup()
{
$this->attach();
}
}