微语

记录生活碎片,分享点滴烟火

头像
老张博客 2024-11-01 13:10
#折腾 犯神经病了,前天才把Claw的4刀给卖了,今天就想回收回来了。原因就是听说阿里老的300卷也不能开香港轻量了,也就是说应该找不到便宜的前端了,所以就不如直接不要前端,把网站放Claw上拉倒!
头像
老张博客 2024-10-31 15:08
#博客 之前找飞牛士给的Wordpress插件Redis Object Cache Pro,真的是非常好用的,他的博客有介绍。
插件在仪表板中没有任何配置选项,所有的参数配置均在wp-config.php配置文件中完成。它需要定义一个名称为’WP_REDIS_CONFIG’的常量,以下示例适用于大多数网站:
```php
define('WP_REDIS_CONFIG', [
'token' => '这里填写随机60个字符ewadawd21312都可以',
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0, // 指定Redis数据库
'maxttl' => 3600 * 24 * 7, // 7 天
'timeout' => 1.0,
'read_timeout' => 1.0,
'split_alloptions' => true,
'debug' => false,
]);

define('WP_REDIS_DISABLED', false);
```
在示例中可以看到,前面是参数后面是值。以下介绍一些常用的参数:

‘host’ => 这里指定 Redis服务器地址,如果和WEB服务器在同一个主机,则设定为’127.0.0.1’即可。
‘port’ => 这个是Redis的连接端口,默认为6379,如果在Redis中变更了默认端口,那么这里也要跟着变更。
‘database’ => 这个参数是指定Redis数据库编号,如果有多个网站使用同一个Redis服务器,则需要为每个网站指定一个不同的数据库编号。默认为0,最多支持的编号数量取决于Redis服务器支持的数据库数量,一般默认支持16个。
‘username’ => 指定连接的用户名,如Redis未设置登录验证,则无需该参数。用法示例:'username' => 'abc',
‘password’ => 指定连接的密码,用法同上。
‘prefix’ => 类似于 WP 的$table_prefix变量,它为所有数据库表名添加前缀。示例:'prefix' => 'mysitename',
‘maxttl’ => 以秒为单位对所有新的缓存键强制执行“最大生存时间”。示例:'maxttl' => 3600 * 24, // 24小时
‘timeout’ => 与 Redis 的连接超时时间,以秒为单位,支持小数。示例:'timeout' => 1.5,
‘read_timeout’ => 读取超时时间,以秒为单位,支持小数,用法同上。
‘retry_interval’ => 重试间隔,以毫秒为单位,支持整数。示例:'retry_interval' => 300,
‘retries’ => 设置连接失败的重试次数,整数型。
‘backoff’ => ‘default’, 使用重试配置时,将使用默认回退配置,该配置包括:去相关抖动算法、补偿计算的基数是500毫秒、补偿时间上限为750毫秒
‘shared’ => true, 设定Redis 服务器/集群是共享的还是专用的。这会影响内存和键计数的显示方式。
‘async_flush’ => true, 在Redis 4.0中引入了异步刷新,可以使用该async_flush选项启用。它允许在后台删除大量Key而不会阻塞服务器。
‘prefetch’ => true, 启用预取后,请求的缓存键将基于每个请求进行存储,并在连续请求的早期按组进行批量加载。可预取键值列表在每次请求时都会更新,以保持其最新状态。
以上是一些常用参数,基本满足绝大部分网站需求,如需要了解全部参数,可以查阅详细文档。
头像
老张博客 2024-10-31 09:55
#日常/随记 每天早上五点起床,跑步机20分钟后给孩子做饭、饭后上上网,这样上午还是有些困,迷迷糊糊的。现在形成规则就是早饭后上床小眯二三十分钟,一上午精神爽爽!!
头像
老张博客 2024-10-30 10:51
#折腾 前几天因为把memos成功调用到Wordpress后,便把老张随笔的程序换成了memos。不过,后来折腾又能成功的在wordpress调用typecho了,所以今天又把老张随笔程序再换回typecho。因为什么?因为我越看memos越不像一个工具,迟早会弃用memos,早做决定早省心!!
头像
老张博客 2024-10-29 09:57
#吐槽 楼下食客扰民,还可以报警?!我住的那栋楼是小门口,楼下有几家饭店。昨天一烧烤店有一桌食客在外面吃,那几个人说话的声音真TM的大,比吵架的声音都大,吵的人根本睡不着!一直吵到夜里一点多才结束。而晚上太晚我又睡不着,一直迷迷糊糊到三点半才入睡!以后再有这样的,就直接110!
头像
老张博客 2024-10-28 21:57
#折腾 claude.ai真是个好东西,让写个typecho的API接口,秒出!
```php
<?php
/**
* Typecho REST API Implementation
* 此文件需放在 Typecho 根目录下的 api.php
*/

// 设置允许跨域访问
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json; charset=UTF-8');

// 引入 Typecho 配置文件
require_once 'config.inc.php';

// 引入 Typecho 公共方法
require_once 'var/Typecho/Common.php';

// 初始化数据库连接
Typecho_Db::set($db);
$options = Typecho_Widget::widget('Widget_Options');

// 路由处理
$route = isset($_GET['route']) ? $_GET['route'] : '';
$method = $_SERVER['REQUEST_METHOD'];

// API 响应类
class ApiResponse {
public static function success($data) {
echo json_encode([
'status' => 'success',
'data' => $data
]);
}

public static function error($message, $code = 400) {
http_response_code($code);
echo json_encode([
'status' => 'error',
'message' => $message
]);
}
}

// API 路由处理
switch ($route) {
case 'posts':
if ($method === 'GET') {
getPosts();
}
break;
case 'post':
if ($method === 'GET' && isset($_GET['id'])) {
getPost($_GET['id']);
}
break;
case 'categories':
if ($method === 'GET') {
getCategories();
}
break;
case 'tags':
if ($method === 'GET') {
getTags();
}
break;
case 'comments':
if ($method === 'GET') {
getComments();
}
break;
default:
ApiResponse::error('Invalid route', 404);
break;
}

// 获取文章列表
function getPosts() {
$pageSize = isset($_GET['pageSize']) ? (int)$_GET['pageSize'] : 10;
$currentPage = isset($_GET['page']) ? (int)$_GET['page'] : 1;

$db = Typecho_Db::get();
$select = $db->select('cid', 'title', 'slug', 'created', 'modified', 'text', 'authorId')
->from('table.contents')
->where('type = ?', 'post')
->where('status = ?', 'publish')
->order('created', Typecho_Db::SORT_DESC)
->page($currentPage, $pageSize);

$posts = $db->fetchAll($select);
$result = [];

foreach ($posts as $post) {
$result[] = [
'id' => $post['cid'],
'title' => $post['title'],
'slug' => $post['slug'],
'created' => date('Y-m-d H:i:s', $post['created']),
'modified' => date('Y-m-d H:i:s', $post['modified']),
'content' => $post['text'],
'author' => getAuthor($post['authorId'])
];
}

ApiResponse::success($result);
}

// 获取单篇文章
function getPost($id) {
$db = Typecho_Db::get();
$post = $db->fetchRow($db->select()
->from('table.contents')
->where('cid = ?', $id)
->where('type = ?', 'post')
->where('status = ?', 'publish')
->limit(1));

if (!$post) {
ApiResponse::error('Post not found', 404);
return;
}

$result = [
'id' => $post['cid'],
'title' => $post['title'],
'slug' => $post['slug'],
'created' => date('Y-m-d H:i:s', $post['created']),
'modified' => date('Y-m-d H:i:s', $post['modified']),
'content' => $post['text'],
'author' => getAuthor($post['authorId']),
'categories' => getPostCategories($post['cid']),
'tags' => getPostTags($post['cid'])
];

ApiResponse::success($result);
}

// 获取分类列表
function getCategories() {
$db = Typecho_Db::get();
$categories = $db->fetchAll($db->select()
->from('table.metas')
->where('type = ?', 'category'));

$result = [];
foreach ($categories as $category) {
$result[] = [
'id' => $category['mid'],
'name' => $category['name'],
'slug' => $category['slug'],
'description' => $category['description']
];
}

ApiResponse::success($result);
}

// 获取标签列表
function getTags() {
$db = Typecho_Db::get();
$tags = $db->fetchAll($db->select()
->from('table.metas')
->where('type = ?', 'tag'));

$result = [];
foreach ($tags as $tag) {
$result[] = [
'id' => $tag['mid'],
'name' => $tag['name'],
'slug' => $tag['slug']
];
}

ApiResponse::success($result);
}

// 获取评论列表
function getComments() {
$pageSize = isset($_GET['pageSize']) ? (int)$_GET['pageSize'] : 10;
$currentPage = isset($_GET['page']) ? (int)$_GET['page'] : 1;

$db = Typecho_Db::get();
$comments = $db->fetchAll($db->select()
->from('table.comments')
->where('status = ?', 'approved')
->order('created', Typecho_Db::SORT_DESC)
->page($currentPage, $pageSize));

$result = [];
foreach ($comments as $comment) {
$result[] = [
'id' => $comment['coid'],
'postId' => $comment['cid'],
'author' => $comment['author'],
'email' => $comment['mail'],
'url' => $comment['url'],
'content' => $comment['text'],
'created' => date('Y-m-d H:i:s', $comment['created'])
];
}

ApiResponse::success($result);
}

// 辅助函数:获取作者信息
function getAuthor($authorId) {
$db = Typecho_Db::get();
$author = $db->fetchRow($db->select()
->from('table.users')
->where('uid = ?', $authorId));

return [
'id' => $author['uid'],
'name' => $author['name'],
'screenName' => $author['screenName'],
'mail' => $author['mail']
];
}

// 辅助函数:获取文章分类
function getPostCategories($postId) {
$db = Typecho_Db::get();
$categories = $db->fetchAll($db->select('table.metas.*')
->from('table.metas')
->join('table.relationships', 'table.relationships.mid = table.metas.mid')
->where('table.relationships.cid = ?', $postId)
->where('table.metas.type = ?', 'category'));

$result = [];
foreach ($categories as $category) {
$result[] = [
'id' => $category['mid'],
'name' => $category['name'],
'slug' => $category['slug']
];
}
return $result;
}

// 辅助函数:获取文章标签
function getPostTags($postId) {
$db = Typecho_Db::get();
$tags = $db->fetchAll($db->select('table.metas.*')
->from('table.metas')
->join('table.relationships', 'table.relationships.mid = table.metas.mid')
->where('table.relationships.cid = ?', $postId)
->where('table.metas.type = ?', 'tag'));

$result = [];
foreach ($tags as $tag) {
$result[] = [
'id' => $tag['mid'],
'name' => $tag['name'],
'slug' => $tag['slug']
];
}
return $result;
}
```
这个API实现提供了以下主要功能:

文章相关接口:


GET /api.php?route=posts - 获取文章列表
GET /api.php?route=post&id={id} - 获取单篇文章详情


分类相关接口:


GET /api.php?route=categories - 获取所有分类


标签相关接口:


GET /api.php?route=tags - 获取所有标签


评论相关接口:


GET /api.php?route=comments - 获取评论列表

每个接口都支持以下特性:

分页支持(通过 page 和 pageSize 参数)
JSON 格式的响应
错误处理
跨域支持

使用方法:

将此文件保存为 api.php 并放在 Typecho 根目录
通过 HTTP 请求访问对应的接口

例如获取文章列表:
CopyGET http://your-blog-url/api.php?route=posts&page=1&pageSize=10
头像
老张博客 2024-10-28 13:44
#VPS 不再见优刻得!三个月前买了他家的一款活动机,拿来做备用的。这两天想着把他给用起来,结果线路真的是太拉了根本没办法用!问了售后客服,意思是这是非精品线路,只能这样。只能这样的线路你拿出来坑什么人呀!语气强硬要求退款!还好按剩余给我退了!
头像
老张博客 2024-10-26 10:33
#折腾 昨天下午,两个小时,实现Wordpress调用typecho的功能,这让我对GPT有了重新的认识。上次写调用memos时认为豆包最“聪明”,结果这次才知道,不管是哪个GPT,你多次提同样的一个问题,他们的回答都不一样。最终还是认为官方的GPT最为好用了!
头像
老张博客 2024-10-25 10:34
#折腾 用memos的人真的是越来越少,木大的广场也是寥寥几人,我也在思考着是否再使用下去!原因有二,一是memos作者更新太随意,只能保持在0.18.1版本不变,另外就是若以后弃用了数据不好往其他程序上迁移;另外一点就是之前老张随笔一直使用typecho并使用商业主题,typecho上写有一千二百多篇随笔,弃用typecho真的是可惜,而且用typecho把数据迁到Wordpress等也方便!考虑几天,准备再折腾!!
头像
老张博客 2024-10-24 10:02
#memos 昨天对Wordpress首页调用memos进行了优化,增加若字符数过多只显示前200个字符的判断。因为全半角不同,最后一个字符乱码。又让AI修改,结果网站出现了502错误,原因就是“正则表达式的匹配过程在处理大量数据或者频繁调用时可能会消耗较多的计算资源。”,正是这个原因导致服务器负载过高,后来经过多次修改,最终完美解决。
动态图片
头像
老张博客 2024-10-23 09:13
#折腾 有些时间折腾一样东西越折腾越上头、越上头也就越钻牛角尖,我也在之前多篇博客里提到我这属于“无脑折腾”。有些时候真的需要停下来、静下来、想一想。比如这次我折腾Wodpress首页调用memos,前前后后花了十几个小时,如果在折腾前静下心来把思路理好,估计也就是个把小时就可以搞定!所以,以后还是不能“无脑折腾”!
头像
老张博客 2024-10-22 13:09
#折腾 以生命不息、折腾不止的原则,终于把wordpress首页调用memos首条memo搞定了,其实难度最大的也就是图片的显示。经历了数十小时,也让我学会了与AI对话!(6张图片分别是外链、本地、三方OS方式)