分类 PHP 下的文章 - 空痕博客
首页
小记
php
python
uni
机器人
QQ机器人
APP
KHMD
KHMD-v3
其他页面
友情链接
用户留言
联系空痕
热门文章
企鹅音乐歌单链接获取教程
网易云音乐歌单链接获取教程
空痕音乐下载器
酷我音乐歌单链接获取教程
酷狗歌单码获取
标签搜索
KHMD
uniapp
APP
空痕音乐下载器
PHP
KongHen
机器人
QQ
音乐
音源
QQ音乐
UTS
ID3
python
pyinstaller
redis
发布
登录
找到
4
篇与
相关的结果
2025-02-20
php利用redis实现接口数据缓存
php利用redis实现服务端数据缓存。 要求php版本大于8.0 缓存类 文件RedisCache.php <?php /** * 请求缓存类 * 利用redis实现数据缓存 * * 作者:KongHen02 * */ class RedisCache { private string $key; private Redis $redis; public function __construct(int $db = 0, bool $device = false) { $config = [ 'host' => '127.0.0.1', 'port' => 6379, 'timeout' => 2, 'database' => $db ]; try { $this->redis = new Redis(); $this->redis->connect( $config['host'], $config['port'], $config['timeout'] ); $this->redis->select($config['database']); } catch (RedisException $e) { error_log("Redis连接失败: " . $e->getMessage()); throw new RuntimeException("缓存服务不可用"); } $this->key = $this->generateKey($device); } public function getCache(): mixed { try { header('X-Cache: ' . ($this->redis->exists($this->key) ? 'HIT' : 'MISS')); $data = $this->redis->get($this->key); return $data !== false ? unserialize($data) : null; } catch (RedisException $e) { error_log("读取缓存数据失败: " . $e->getMessage()); return null; } } public function setCache(mixed $data, int $ttl = 7200): bool { try { return $this->redis->setex( $this->key, $ttl, serialize($data) ); } catch (RedisException $e) { error_log("写入缓存数据失败: " . $e->getMessage()); return false; } } public function __destruct() { try { $this->redis?->close(); } catch (RedisException $e) { error_log("关闭Redis连接失败: " . $e->getMessage()); } } private function generateKey(bool $device): string { $url = $_SERVER['HTTP_HOST']; $urlArray = explode("?", $_SERVER['REQUEST_URI']); $url .= $urlArray[0]; $params = $_POST; ksort($params); $keyParts = [ $_SERVER['REQUEST_METHOD'], $device ? $this->detectDevice() : null, $url, substr(md5($urlArray[1]), 0, 8), substr(md5(http_build_query($params)), 0, 8) ]; return implode(':', array_filter($keyParts)); } private function detectDevice(): string { $ua = strtolower($_SERVER['HTTP_USER_AGENT'] ?? ''); return match(true) { (bool)preg_match('/mobile|android|iphone|ipod|windows phone/', $ua) => 'mobile', (bool)preg_match('/tablet|ipad|kindle/', $ua) => 'tablet', default => 'desktop' }; } } ?>使用说明 <?php // 导入缓存类 require_once($_SERVER['DOCUMENT_ROOT'] . "/RedisCache.php"); try { // 0:redis表序;0-15;必填 // true:是否区分客户端类型(手机/平板/电脑);true/false(默认);可空 $RedisCache = new RedisCache(0, true); if (($result = $RedisCache->getCache()) !== null) { exit($result); } } catch (RuntimeException $e) { // 缓存服务不可用时继续执行 header('X-Cache-Status: Down'); } // 业务逻辑处理 $result = "数据内容,不限制类型"; // 缓存成功结果 if (isset($result)) { // $result:需要缓存的数据;mixed;必填 // 7200:数据有效期;int(默认7200);可空 $RedisCache?->setCache($result, 7200); } echo($result); ?>
所有文章
PHP
# PHP
# redis
KongHen02
2天前
0
0
0
2024-06-05
PHP封装功能较全CURL函数
经常用到,之前一直重复写,今天写脚本的时候就封装了一个,方便重复利用 <?php /** * httpRequest * 发送curl请求 * * @param string $url 请求的URL * @param string $method 请求方法,默认是'GET' * @param string $back 返回类型,默认为空,可以为'all'或'cookie' * @param array $params 请求参数,默认为空数组 * @param array $headers 请求头,默认为空数组 * @return array 响应数据 * */ function sendCurlRequest($url, $method = 'GET', $back = "", $params = [], $headers = []) { // 初始化cURL会话 $ch = curl_init(); // 设置请求URL if ($method == 'GET' && !empty($params)) { $url .= '?' . http_build_query($params); } curl_setopt($ch, CURLOPT_URL, $url); // 设置请求方法 if($method == 'POST') { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params)); } // 返回而不是输出内容 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 允许跟随重定向 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // 设置返回类型 if ($back === "all") { // 设置为true以包含响应头 curl_setopt($ch, CURLOPT_HEADER, true); // 设置为false以包含响应体 curl_setopt($ch, CURLOPT_NOBODY, false); } elseif ($back === "cookie") { // 设置为true以包含响应头 curl_setopt($ch, CURLOPT_HEADER, true); } // 设置请求头 if (!empty($headers)) { curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); } // 设置超时(可选) curl_setopt($ch, CURLOPT_TIMEOUT, 30); // 执行cURL会话 $response = curl_exec($ch); // 获取HTTP状态码 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // 获取错误信息 $error = curl_error($ch); // 处理cookie $cookies = []; if ($back == "cookie" || $back == "all") { // 分离头部和主体 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $header = substr($response, 0, $header_size); if ($back == "all") { $body = substr($response, $header_size); } // 解析头部中的Cookie preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $header, $matches); foreach ($matches[1] as $item) { parse_str($item, $cookie); $cookies = array_merge($cookies, $cookie); } } // 关闭cURL会话 curl_close($ch); // 构建返回数据 if ($error) { $data = ['code' => 400, 'msg' => $error]; } else if ($httpCode >= 400) { $data = ['code' => 400, 'msg' => "HTTP error $httpCode"]; } else if ($back == "all") { $data = ['code' => 200, 'cookie' => $cookies, 'body' => $body]; } else if ($back == "cookie") { $data = ['code' => 200, 'cookie' => $cookies]; } else { $data = ['code' => 200, 'body' => $response]; } // 返回响应数据 return $data; }
所有文章
PHP
KongHen02
1年前
0
158
0
2024-03-24
网易云免费音乐解析
网易云免费音乐解析
所有文章
PHP
KongHen02
1年前
0
1,085
3
2024-01-10
QQ音乐搜索及解析接口
QQ音乐搜索及解析接口源码
所有文章
PHP
KongHen02
1年前
1
1,092
5
易航博客