改名
This commit is contained in:
+21
-18
@@ -6,10 +6,12 @@
|
||||
* Time: 14:51
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Snowflake\Cache;
|
||||
|
||||
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Snowflake\Abstracts\Component;
|
||||
use Swoole\Coroutine\System;
|
||||
|
||||
@@ -32,8 +34,9 @@ class File extends Component implements ICache
|
||||
/**
|
||||
* @param $key
|
||||
* @param $val
|
||||
* @return string|int
|
||||
*/
|
||||
public function set($key, $val)
|
||||
public function set($key, $val): string|int
|
||||
{
|
||||
if (is_array($val) || is_object($val)) {
|
||||
$val = serialize($val);
|
||||
@@ -42,15 +45,15 @@ class File extends Component implements ICache
|
||||
if (!$this->exists($tmpFile)) {
|
||||
touch($tmpFile);
|
||||
}
|
||||
System::writeFile($tmpFile, $val, LOCK_EX);
|
||||
return System::writeFile($tmpFile, $val, LOCK_EX);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param array $hashKeys
|
||||
* @return mixed|void
|
||||
* @return array|bool
|
||||
*/
|
||||
public function hMget($key, array $hashKeys)
|
||||
public function hMGet($key, array $hashKeys): array|bool
|
||||
{
|
||||
$hash = $this->get($key);
|
||||
if (!is_array($hash)) {
|
||||
@@ -67,9 +70,9 @@ class File extends Component implements ICache
|
||||
/**
|
||||
* @param $key
|
||||
* @param array $val
|
||||
* @return mixed|void
|
||||
* @return mixed
|
||||
*/
|
||||
public function hMset($key, array $val)
|
||||
public function hMSet($key, array $val): mixed
|
||||
{
|
||||
$hash = $this->get($key);
|
||||
if (!is_array($hash)) {
|
||||
@@ -81,11 +84,11 @@ class File extends Component implements ICache
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $hashKey
|
||||
* @return mixed|void
|
||||
* @param string $key
|
||||
* @param string $hashKey
|
||||
* @return string|int|bool
|
||||
*/
|
||||
public function hget($key, $hashKey)
|
||||
public function hGet(string $key, string $hashKey): string|int|bool|null
|
||||
{
|
||||
$hash = $this->get($key);
|
||||
if (!is_array($hash)) {
|
||||
@@ -98,9 +101,9 @@ class File extends Component implements ICache
|
||||
* @param $key
|
||||
* @param $hashKey
|
||||
* @param $hashValue
|
||||
* @return mixed|void
|
||||
* @return mixed
|
||||
*/
|
||||
public function hset($key, $hashKey, $hashValue)
|
||||
public function hSet($key, $hashKey, $hashValue): mixed
|
||||
{
|
||||
$hash = $this->get($key);
|
||||
if (!is_array($hash)) {
|
||||
@@ -116,20 +119,20 @@ class File extends Component implements ICache
|
||||
* @param $key
|
||||
* @return bool
|
||||
*/
|
||||
public function exists($key)
|
||||
#[Pure] public function exists($key): bool
|
||||
{
|
||||
return file_exists($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @return mixed|null
|
||||
* @return mixed|bool
|
||||
*/
|
||||
public function get($key)
|
||||
public function get($key): string|bool
|
||||
{
|
||||
$tmpFile = $this->getCacheKey($key);
|
||||
if (!$this->exists($tmpFile)) {
|
||||
return NULL;
|
||||
return false;
|
||||
}
|
||||
$content = file_get_contents($tmpFile);
|
||||
return unserialize($content);
|
||||
@@ -140,8 +143,8 @@ class File extends Component implements ICache
|
||||
* @return string
|
||||
* @throws
|
||||
*/
|
||||
private function getCacheKey($key)
|
||||
private function getCacheKey($key): string
|
||||
{
|
||||
return storage($key,'cache');
|
||||
return storage($key, 'cache');
|
||||
}
|
||||
}
|
||||
|
||||
+15
-14
@@ -6,6 +6,7 @@
|
||||
* Time: 16:35
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Snowflake\Cache;
|
||||
|
||||
/**
|
||||
@@ -17,36 +18,36 @@ interface ICache
|
||||
/**
|
||||
* @param $key
|
||||
* @param $val
|
||||
* @return mixed
|
||||
* @return string|int
|
||||
*/
|
||||
public function set($key, $val);
|
||||
public function set($key, $val): string|int;
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @return mixed
|
||||
* @return string|int|bool
|
||||
*/
|
||||
public function get($key);
|
||||
public function get($key): string|int|bool;
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $hashKeys
|
||||
* @return mixed
|
||||
* @param array $hashKeys
|
||||
* @return array|bool|null
|
||||
*/
|
||||
public function hMget($key, array $hashKeys);
|
||||
public function hMGet($key, array $hashKeys): array|bool|null;
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param array $val
|
||||
* @return mixed
|
||||
*/
|
||||
public function hMset($key, array $val);
|
||||
public function hMSet($key, array $val): mixed;
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $hashKey
|
||||
* @return mixed
|
||||
* @param string $key
|
||||
* @param string $hashKey
|
||||
* @return string|int|bool
|
||||
*/
|
||||
public function hget($key, $hashKey);
|
||||
public function hGet(string $key, string $hashKey): string|int|bool;
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
@@ -54,11 +55,11 @@ interface ICache
|
||||
* @param $hashValue
|
||||
* @return mixed
|
||||
*/
|
||||
public function hset($key, $hashKey, $hashValue);
|
||||
public function hSet($key, $hashKey, $hashValue): mixed;
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @return bool
|
||||
*/
|
||||
public function exists($key);
|
||||
public function exists($key): bool;
|
||||
}
|
||||
|
||||
@@ -1,154 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
namespace Snowflake\Cache;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Snowflake\Abstracts\Component;
|
||||
use Snowflake\Abstracts\Config;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Exception\ConfigException;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
/**
|
||||
* Class Memcached
|
||||
* @package Yoc\cache
|
||||
*/
|
||||
class Memcached extends Component implements ICache
|
||||
{
|
||||
|
||||
/** @var \Memcached */
|
||||
private \Memcached $_memcached;
|
||||
|
||||
public string $host = '127.0.0.1';
|
||||
|
||||
public int $port = 11211;
|
||||
|
||||
public int $timeout = 60;
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$event = Snowflake::app()->event;
|
||||
$event->on(Event::RELEASE_ALL, [$this, 'destroy']);
|
||||
$event->on(Event::EVENT_AFTER_REQUEST, [$this, 'release']);
|
||||
|
||||
$id = Config::get('id', false, 'system');
|
||||
$this->_memcached = new \Memcached($id);
|
||||
$this->addServer();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Memcached
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getConnect()
|
||||
{
|
||||
return $this->_memcached;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
private function addServer()
|
||||
{
|
||||
$array = [];
|
||||
$memcached = Config::get('cache.memcached');
|
||||
if (isset($memcached[0])) {
|
||||
foreach ($memcached as $value) {
|
||||
$array[] = [$value['host'], $value['port'], $value['weight']];
|
||||
}
|
||||
$isConnected = $this->_memcached->addServers($array);
|
||||
} else {
|
||||
$array[] = Config::get('cache.memcached.host', true);
|
||||
$array[] = Config::get('cache.memcached.port', true);
|
||||
$array[] = Config::get('cache.memcached.weight', true);
|
||||
$isConnected = $this->_memcached->addServer(...$array);
|
||||
}
|
||||
if (!$isConnected) {
|
||||
throw new Exception('Cache Memcache Host 127.0.0.1 Connect Fail.');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $val
|
||||
* @return mixed|void
|
||||
*/
|
||||
public function set($key, $val)
|
||||
{
|
||||
// TODO: Implement set() method.
|
||||
if (is_array($val) || is_object($val)) {
|
||||
$val = serialize($val);
|
||||
}
|
||||
|
||||
$this->_memcached->set($key, $val);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @return mixed|void
|
||||
*/
|
||||
public function get($key)
|
||||
{
|
||||
// TODO: Implement get() method.
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param array $hashKeys
|
||||
* @return mixed|void
|
||||
*/
|
||||
public function hMget($key, array $hashKeys)
|
||||
{
|
||||
// TODO: Implement hMget() method.
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param array $val
|
||||
* @return mixed|void
|
||||
*/
|
||||
public function hMset($key, array $val)
|
||||
{
|
||||
// TODO: Implement hMset() method.
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $hashKey
|
||||
* @return mixed|void
|
||||
*/
|
||||
public function hget($key, $hashKey)
|
||||
{
|
||||
// TODO: Implement hget() method.
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $hashKey
|
||||
* @param $hashValue
|
||||
* @return mixed|void
|
||||
*/
|
||||
public function hset($key, $hashKey, $hashValue)
|
||||
{
|
||||
// TODO: Implement hset() method.
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @return bool|void
|
||||
*/
|
||||
public function exists($key)
|
||||
{
|
||||
// TODO: Implement exists() method.
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -46,7 +46,7 @@ class Redis extends Component
|
||||
* @return mixed
|
||||
* @throws
|
||||
*/
|
||||
public function __call($name, $arguments)
|
||||
public function __call($name, $arguments): mixed
|
||||
{
|
||||
if (method_exists($this, $name)) {
|
||||
$data = $this->{$name}(...$arguments);
|
||||
@@ -82,7 +82,7 @@ SCRIPT;
|
||||
* @return int
|
||||
* @throws Exception
|
||||
*/
|
||||
public function unlock($key)
|
||||
public function unlock($key): int
|
||||
{
|
||||
$redis = $this->proxy();
|
||||
return $redis->del($key);
|
||||
@@ -113,7 +113,7 @@ SCRIPT;
|
||||
* @return \Redis
|
||||
* @throws Exception
|
||||
*/
|
||||
public function proxy()
|
||||
public function proxy(): \Redis
|
||||
{
|
||||
$connections = Snowflake::app()->pool->redis;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user