redis – 苏demo的别样人生 https://www.libaocai.com 行走于凡尘俗世,活得别样人生 Sat, 26 Sep 2020 06:39:05 +0000 zh-CN hourly 1 https://wordpress.org/?v=6.2.4 mac下简易安装redis server https://www.libaocai.com/7117.html Sat, 26 Sep 2020 06:36:00 +0000 http://www.libaocai.com/?p=7117 下载安装:

wget http://download.redis.io/releases/redis-6.0.8.tar.gz
tar xzf redis-6.0.8.tar.gz
cp redis-6.0.8 /usr/local
cd redis-6.0.8
sudo make test
sudo make install

启动redis:

redis-sever

测试:

redis-cli
set testkey 123
get testkey

推荐Redis客户端 几款开源的图形化Redis客户端管理软件

转载请注明:苏demo的别样人生 » mac下简易安装redis server

]]>
php采用redis有序集合实现延迟队列 https://www.libaocai.com/7067.html Thu, 09 Jul 2020 12:33:02 +0000 http://www.libaocai.com/?p=7067 最近在实践小项目的时候,采用redis的有序集合,例如有序集合分数的概念实现了延迟队列的功能。以下是实践练习的代码,仅供学习使用:

示例代码:

<?php
$queueName = 'delay:queue:nts';

while (true)
{
try
{
$queueData = RedisHelper::zRangeByScore($queueName, 0, time(), ['limit' => [0, 1]]);
if (!$queueData)
{
cronOutlog("INFO: no data");
usleep(100000);
}
$task = $queueData['0'];
if (RedisHelper::zRem($queueName, $task))
{
$data = json_decode($task, true);
cronOutlog("INFO:" . $data['task_name'] . " 运行时间:" . formatLongDate().' 内容:'.json_encode($data['body']));
//todo 执行逻辑
}
usleep(100000);
} catch (\Exception $ex)
{
cronOutlog("INFO:异常操作,再次放回队列");
RedisHelper::zAdd($queueName, time() + 5 * 60, json_encode($task, JSON_UNESCAPED_UNICODE));
}
}

推送数据加入延迟队列示例代码:

$queueName = 'delay:queue:nts';
$data = [
'task_name' => time(),
'body' => [
'id' => time(),
'name' => NeoString::getRandString(10)
],
];
RedisHelper::zAdd($queueName, time() + 30, json_encode($data, JSON_UNESCAPED_UNICODE));

转载请注明:苏demo的别样人生 » php采用redis有序集合实现延迟队列

]]>