ASCWH - PHP https://www.ascwh.com/category/php/ zh-CN 只是一个默认分类 Mon, 19 Jun 2023 00:25:33 +0800 Mon, 19 Jun 2023 00:25:33 +0800 轻量级队列 Beanstalkd https://www.ascwh.com/404.html https://www.ascwh.com/404.html Mon, 19 Jun 2023 00:25:33 +0800 ASCWH 一:介绍

Beanstalkd 是一个个简单、快速、轻量级的内存型队列。它是典型的类Memcached设计,协议和使用方式都是同样风格。
github:https://github.com/beanstalkd
官网:https://beanstalkd.github.io/
安装
在 Centos7 上通过命令 yum -y install beanstalkd --enablerepo=epel;
其他系统的安装在 官网 上查看
查看当前版本号

beanstalkd -v

启动
常见启动如下:
beanstalkd -l 0.0.0.0 -p 11300 -b /home/software/binstalkd/binlogs

启动后对 beanstalkd 的操作可以使用 telnet,比如 telnet 127.0.0.1 11300。然后便可以执行 beanstalkd 的各命令,如 stats 查看信息,use, put, watch 等等。

二:功能特性

2.1 优先级
任务job可以有0~2^32 个优先级, 0 代表最高优先级,默认优先级为1024。

2.2 延迟 delay
比如说多长时间后执行某个任务

2.3 持久化
可以通过binlog将job及其状态记录到文件里面,在Beanstalkd下次启动时可以通过读取binlog来恢复之前的job及状态。

2.4 超时控制
为了防止某个consumer长时间占用任务但不能处理的情况,Beanstalkd为reserve操作设置了timeout时间,如果该consumer不能在指定时间内完成job,job将被迁移回READY状态,供其他consumer执行。

2.5 分布式容错
因为它是类Memcached设计,beanstalkd各个server之间并不知道彼此的存在,都是通过client来实现分布式以及根据tube名称去特定server获取job。

三:使用场景

用作延时队列:比如可以用于如果用户30分钟内不操作,任务关闭。
用作定时任务:比如可以用于专门的后台任务。
用作异步操作:这是所有消息队列都最常用的,先将任务仍进去,顺序执行。
用作循环队列:用release命令可以循环执行任务,比如可以做负载均衡任务分发。
用作兜底机制:比如一个请求有失败的概率,可以用Beanstalk不断重试,设定超时时间,时间内尝试到成功为止。
其实我们最重要的使用场景是把它作为延迟队列类使用,比如:

下订单后多长时间没有付款,要取消订单,并退库存
用户注册成功后,发送一封邮件
定期检查退款状态的订单是否退款成功

四:Beanstalkd设计基本概念

4.1 核心概念#
job:一个需要异步处理的任务,是Beanstalkd中的基本单元,需要放在一个tube中
tube:一个有名的队列,用来存储统一类型的job,是producer和consumer操作的对象。tube可以称为管道
producer:Job 的生产者,通过 put 命令来将一个 job 放到一个 tube 中
consumer:Job的消费者,通过 reserve/release/bury/delete 命令来获取 job 或改变 job 的状态

4.2 job的生命周期#
生产者生成任务,并根据业务需求将任务放到不同的管道中。比如与注册有关的任务放到注册管道中,和订单有关的任务放到订单管道中。

任务进入管道到离开管道一共有5个状态 :
(ready,delayed,reserved,buried,delete)

READY - 需要立即处理的任务,当延时 (DELAYED) 任务到期后会自动成为当前任务;
DELAYED - 延迟执行的任务, 当消费者处理任务后, 可以用将消息再次放回 DELAYED 队列延迟执行;
RESERVED - 已经被消费者获取, 正在执行的任务。Beanstalkd 负责检查任务是否在 TTR(time-to-run) 内完成;
BURIED - 保留的任务: 任务不会被执行,也不会消失,除非有人把它 "踢" 回队列;
DELETED - 消息被彻底删除。Beanstalkd 不再维持这些消息。
状态流程#
1.生产任务:

当producer生产者put一个job到tube时,这个job就处于 ready 状态,等待consumer来消费处理;

  producer生产者也可以选择延迟put一个job,这时job就先达到 delayed 状态(比如设置一个5秒延迟的job,那么5秒之后,这个job才会变成 ready 状态,才可以被consumer消费)
  1. 消费任务:

consumer获取了当前 ready 的job后,该job就会迁移到 reserved 状态,这样其他的consumer就不能在操作该job

  1. 消费完任务后:

当consumer消费完该job后,可以选择delete, release 或者 bury 3种操作。

delete操作:job从系统消亡,之后不能在获取;
release操作:可以重新把该job状态迁移回 ready (也可以延迟状态 delayed 操作),使其他的consumer可以继 续获取和执行该job;
bury操作: 把job置为 buried 状态,及是把该job休眠,等到需要的时候,还可以将休眠的 job 重新置为 ready 状态, 也可以delete掉 buried 状态的job。
也就是说:当消费者处理完任务后,任务的状态可能是delete(删除,处理成功),可能是buried(预留,意味着先把任务放一边,等待条件成熟还要用),可能是ready,也可能是delayed,需要根据具体业务场景自己进行判断定义

五:例子

使用的PHP库:https://github.com/pheanstalk/pheanstalk
直接用composer进行安装

composer require pda/pheanstalk

5.1 生产者和消费者例子**
生产者: producer.php**

require_once('./vendor/autoload.php');

use Pheanstalk\Pheanstalk;

$pheanstalk = Pheanstalk::create('192.168.1.100', 11301);

$tubeName = 'use_email_list';
$jobData = array(
    'uid' => time(),
    'email' => '12917@qq.com',
    'message' => 'hello beanstalkd',
    'dtime' => date('Y-m-d H:i:s'),
);

$pheanstalk->useTube($tubeName)->put(json_encode($jobData));

echo json_encode($jobData);

运行程序: php ./producer.php

消费者:consumer.php

<?php
require_once('./vendor/autoload.php');

use Pheanstalk\Pheanstalk;

$pheanstalk = Pheanstalk::create('192.168.1.109', 11301);
$tubeName = 'use_email_list';

while(true) {
    //获取队列信息,reserve 阻塞获取
    $job = $pheanstalk->watch($tubeName)->ignore('default')->reserve();
    $data = $job->getData();

    //执行相关逻辑
    $result = file_put_contents('./send_email.log', $data, FILE_APPEND | LOCK_EX);
    if ($result) {
        echo 'success :'.$data.PHP_EOF;
        $pheanstalk->delete($job);
    }

    //暂停(不可能是百分百的准确,跟系统的调度、CPU时钟周期等有一定关系)
    usleep(500000);
}

echo 'Success !';

运行程序:php ./consumer.php

5.2 监控例子
monitor.php

//监控服务状态
require_once('./vendor/autoload.php');

use Pheanstalk\Pheanstalk;

$pheanstalk = Pheanstalk::create('192.168.1.109', 11301);

//可以开发监控面板,监控数据的,有多少tube,多少队列,多少延迟等等


//查看beanstalkd状态
var_export($pheanstalk->stats());

//查看有多少个tube
var_export($pheanstalk->listTubes());

//设置要监听的tube
$pheanstalk->watch('use_email_list');

//取消对默认tube的监听,可以省略
$pheanstalk->ignore('default');

//查看监听的tube列表
var_export($pheanstalk->listTubesWatched());

//查看use_email_list的tube当前的状态
var_export($pheanstalk->statsTube('use_email_list'));

//单个job信息
// var_export($pheanstalk->statsJob($job));

六: pheanstalk一些方法说明

6.1 整个 beanstalkd 当前状态信息#

var_export($pheanstalk->stats())  //beanstalkd 当前状态信息

//output:
Pheanstalk\Response\ArrayResponse::__set_state(array(
  'current-jobs-urgent' => '0', // 优先级小于1024状态为ready的job数量
  'current-jobs-ready' => '0', // 状态为ready的job数量
  'current-jobs-reserved' => '0', // 状态为reserved的job数量
  'current-jobs-delayed' => '0', // 状态为delayed的job数量
  'current-jobs-buried' => '0', // 状态为buried的job数量
  'cmd-put' => '0', // 总共执行put指令的次数
  'cmd-peek' => '0', // 总共执行peek指令的次数
  'cmd-peek-ready' => '0', // 总共执行peek-ready指令的次数
  'cmd-peek-delayed' => '0', // 总共执行peek-delayed指令的次数
  'cmd-peek-buried' => '0', // 总共执行peek-buried指令的次数
  'cmd-reserve' => '0', // 总共执行reserve指令的次数
  'cmd-reserve-with-timeout' => '0',
  'cmd-delete' => '0',
  'cmd-release' => '0',
  'cmd-use' => '0', // 总共执行use指令的次数
  'cmd-watch' => '0', // 总共执行watch指令的次数
  'cmd-ignore' => '0',
  'cmd-bury' => '0',
  'cmd-kick' => '0',
  'cmd-touch' => '0',
  'cmd-stats' => '2',
  'cmd-stats-job' => '0',
  'cmd-stats-tube' => '0',
  'cmd-list-tubes' => '0',
  'cmd-list-tube-used' => '0',
  'cmd-list-tubes-watched' => '0',
  'cmd-pause-tube' => '0',
  'job-timeouts' => '0', // 所有超时的job的总共数量
  'total-jobs' => '0', // 创建的所有job数量
  'max-job-size' => '65535', // job的数据部分最大长度
  'current-tubes' => '1', // 当前存在的tube数量
  'current-connections' => '1', // 当前打开的连接数
  'current-producers' => '0', // 当前所有的打开的连接中至少执行一次put指令的连接数量
  'current-workers' => '0', // 当前所有的打开的连接中至少执行一次reserve指令的连接数量
  'current-waiting' => '0', // 当前所有的打开的连接中执行reserve指令但是未响应的连接数量
  'total-connections' => '11', // 总共处理的连接数
  'pid' => '4839', // 服务器进程的id
  'version' => '1.10', // 服务器版本号
  'rusage-utime' => '0.000000', // 进程总共占用的用户CPU时间
  'rusage-stime' => '0.001478', // 进程总共占用的系统CPU时间
  'uptime' => '12031', // 服务器进程运行的秒数
  'binlog-oldest-index' => '2', // 开始储存jobs的binlog索引号
  'binlog-current-index' => '2', // 当前储存jobs的binlog索引号
  'binlog-records-migrated' => '0',
  'binlog-records-written' => '0', // 累积写入的记录数
  'binlog-max-size' => '10485760', // binlog的最大容量
  'id' => '4b005307e8af5b37', // 一个随机字符串,在beanstalkd进程启动时产生
  'hostname' => 'xing',
))

6.2 单个job任务的信息:#

//单个job信息
var_export($pheanstalk->statsJob($job1));

'id' => '1', // job id
'tube' => 'use_email_list', // job 所在的管道
'state' => 'reserved', // job 当前的状态
'pri' => '1024', // job 的优先级
'age' => '5222', // 自 job 创建时间为止 单位:秒
'delay' => '0',
'ttr' => '60', // time to run
'time-left' => '58', // 仅在job状态为reserved或者delayed时有意义,当job状态为reserved时表示剩余的超时时间
'file' => '2', // 表示包含此job的binlog序号,如果没有开启它将为0
'reserves' => '10', // 表示job被reserved的次数
'timeouts' => '0', // 表示job处理的超时时间
'releases' => '1', // 表示job被released的次数
'buries' => '0', // 表示job被buried的次数
'kicks' => '0', // 表示job被kiced的次数

6.3 tube 管道的信息:#

//查看有多少个tube
var_export($pheanstalk->listTubes());

//设置要监听的tube
$pheanstalk->watch('use_email_list');

//取消对默认tube的监听,可以省略
$pheanstalk->ignore('default');

//查看监听的tube列表
var_export($pheanstalk->listTubesWatched());

//查看use_email_list的tube当前的状态
var_export($pheanstalk->statsTube('use_email_list'));

6.4 生产者调用方法#

// put 任务 方式一; 返回新 job 的任务标识,整型值;
$pheanstalk->useTube('use_email_list')->put(
    'hello, world', // 任务内容
    23, // 任务的优先级, 默认为 1024
    0, // 不等待直接放到ready队列中.
    60 // 处理任务的时间(单位为秒)
);

// put 任务 方式二; 返回新 job 的任务标识,整型值;
$pheanstalk->putInTube(
    'use_email_list', // 管道名称
    'hello, world', // 任务内容
    23, // 任务的优先级, 默认为 1024
    0, // 不等待直接放到ready队列中. 如值为 60 表示 60秒;
    60 // 处理任务的时间(单位为秒)
);

// 给管道里所有新任务设置延迟
$pheanstalk->pauseTube('use_email_list', 30);

// 取消管道延迟
$pheanstalk->resumeTube('use_email_list');

概念说明:

任务优先级
任务 (job) 可以有 0~2^32 个优先级, 0 代表最高优先级。 beanstalkd 采用最大最小堆 (Min-max heap) 处理任务优先级排序, 任何时刻调用 reserve 命令的消费者总是能拿到当前优先级最高的任务, 时间复杂度为 O(logn).
ttr(time to run, 预设的执行时间)
消费者必须在预设的 TTR (time-to-run) 时间内发送 delete / release / bury 改变任务状态;否则 Beanstalkd 会认为消息处理失败,状态改为 ready,然后把任务交给另外的消费者节点执行。如果消费者预计在 TTR (time-to-run) 时间内无法完成任务, 也可以发送 touch 命令, 它的作用是让 Beanstalkd 重置该任务的 time-left 剩余执行时间.

6.5 消费者调用方法

  1. 正常获取和执行job流程
// 获取 use_email_list 管道的 job
$job = $pheanstalk->watch('use_email_list')->ignore('default')->reserve();
$job_2 = $pheanstalk->reserveFromTube('use_email_list');
$job_3 = $pheanstalk->peekReady('use_email_list');

// 如果知道 job 的 id, 也可以
$job_4 = $pheanstalk->peek($id);
// var_export($pheanstalk->statsJob($job_4));

// 获取下一个延迟时间最短 的 job
$job_5 = $pheanstalk->peekDelayed('use_email_list');

// do job .... 这里省略异常的考虑

// 释放任务 让别人执行
$pheanstalk->release($job);
// 或成功执行完,则删除任务
//$pheanstalk->delete($job);
// 将任务埋起来,预留
//$pheanstalk->bury($job);
  1. 处理 buried 状态的 Job
// 获取下一个被埋藏的 job
$job = $pheanstalk->peekBuried('use_email_list');

// 将任务状态从 buried 改为 ready
//$pheanstalk->kickJob($job);

// 批量将指定数目的任务从 buried 改为 ready
$pheanstalk->kick(10);
  1. 处理 buried 状态的 Job
// 获取下一个被埋藏的 job
$job = $pheanstalk->peekBuried('use_email_list');

// 将任务状态从 buried 改为 ready
//$pheanstalk->kickJob($job);

// 批量将指定数目的任务从 buried 改为 ready
$pheanstalk->kick(10);
]]>
0 https://www.ascwh.com/404.html#comments https://www.ascwh.com/feed/category/php/
supervisor unix:///tmp/supervisor.sock no such file 问题 https://www.ascwh.com/400.html https://www.ascwh.com/400.html Thu, 21 Jul 2022 15:29:34 +0800 ASCWH 1、打开配置文件

vim /etc/supervisord.conf
这里把所有的 /tmp 路径改掉

/tmp/supervisor.sock 改成 /var/run/supervisor.sock
/tmp/supervisord.log 改成/var/log/supervisor.log
/tmp/supervisord.pid 改成 /var/run/supervisor.pid
要不容易被 linux 自动清掉

2、修改权限

sudo chmod 777 /run
sudo chmod 777 /var/log
如果没改,启动报错 IOError: [Errno 13] Permission denied: '/var/log/supervisord.log'

3、创建 supervisor.sock

sudo touch /var/run/supervisor.sock
sudo chmod 777 /var/run/supervisor.sock

4、启动supervisord,启动之前的实例或杀死进程

]]>
0 https://www.ascwh.com/400.html#comments https://www.ascwh.com/feed/category/php/
CentOS7 安装 、配置 Supervisor 基础操作 https://www.ascwh.com/399.html https://www.ascwh.com/399.html Sat, 21 May 2022 00:50:00 +0800 ASCWH Supervisor是什么

Supervisor是比较常用的进程管理工具,支持 Linux/MacOS平台,可以用来控制一组Linux/Unix进程(启动、重启、kill)等,使用Supervisor管理的进程,可以做到以守护进程的方式运行,服务异常关闭后可以自动重启。

Supervisor是一个 Client/Server模式的系统,允许用户在类unix操作系统上监视和控制多个进程,或者可以说是多个程序。

Supervisor 有下面几个组件:

supervisord: Supervisor的服务端程序,使用前,需要先启动该组件;
supervisorctl: Supervisor的客户端程序,用来实际控制子进程(自定义的服务、程序);
Supervisor 通过配置文件,还可以启动Web控制台,通过Web页面来管理子进程;

Supervisor 与系统自带 init 进程管理比较

方便:有些编译运行的程序,在安装完成后,需要为他们编写启动停止管理脚本,写入和维护可能很痛苦,而且进程在异常崩溃结束时,许多程序都不会正确重新启动的。Supervisord启动管理的程序进程是作为其子进程来运行的,并且可以配置为在进程崩溃停止时自动重新启动它们。

准确:在Unix上的进程通常很难获得准确的up/down状态。Pidfiles经常说谎。Supervisord 将进程作为子进程启动,所以它总是知道其子进程的正确的up/down状态,可以方便的对这些数据进行查询。

进程分组:进程支持分组启动和停止,也支持启动顺序,即‘优先级’,supervisor允许为进程分配优先级,并允许用户通过supervisorctl客户端发出命令,如“全部启动”和”重新启动所有“,它们以预先分配的优先级顺序启动。还可以将进程分为”进程组“,一组逻辑关联的进程可以作为一个单元停止或启动。

Supervisor 特点

简单:supervisor通过简单的INI风格的配置文件进行配置管理,易于学习,并提供了许多每个进程选项,如重新启动失败的进程和日志的自动切割。

集中:supervisor提供一个start、stop和监控进程的地方,进程可以单独或分组进行控制。可以通过supervisor的本地或远程命令行管理和web管理(一般为了安全,web通常需要禁用)

高效:supervisor通过fork/exec启动子进程,子进程需要前台运行,操作系统进程终止时去通知supervisor,而不像一些我们需要写脚本去定期轮询PID文件来重新启动失败的进程。

可扩展:supervisor有一个简单的事件(event)通知协议,还有一个用于控制的XML-RPC接口,可以用Python开发人员来扩展构建。

兼容:supervisor由Python编写,在除Windows操作系统以外基本都支持,如linux,Mac OS x,solaris,FreeBSD系统。

Supervisor 组建构成

supervisord:supervisor服务器的进程名是supervisord。它主要负责在自己的调用中启动子程序,响应客户端的命令,重新启动崩溃或退出的进程,记录其子进程stdout和stderr的输出,以及生成和处理对应于子进程生命周期中的”event“。

服务器进程使用的配置文件,通常路径存放在 /etc/supervisord.conf 中。此配置文件是INI格式的配置文件。

supervisorctl:supervisor命令行的客户端名称是supervisorctl。它为supervisord提供了一个类似于shell的交互界面。使用supervisorctl,用户可以查看不同的supervisord进程列表,获取控制子进程的状态,如停止和启动子进程。

web服务器:一个可以通过Web界面来查看和控制进程的状态,默认监听在9001上。

1、安装以及服务启动

yum install epel-release     # 安装企业版 Linux 附加软件包(EPEL)
yum install -y supervisor    # yum 安装 supervisor
systemctl enable supervisord # 开机自启动
systemctl start supervisord  # 启动supervisord服务(服务端)
systemctl status supervisord # 查看supervisord服务状态(服务端)
ps -ef|grep supervisord      # 查看是否存在supervisord进程

生成配置文件

用supervisor自带命令生成一个标准的配置文件

/usr/bin/echo_supervisord_conf > /etc/supervisord.conf
~]# cat /etc/supervisord.conf
; Sample supervisor config file.
;
; For more information on the config file, please see:
; http://supervisord.org/configuration.html
;
; Notes:
;  - Shell expansion ("~" or "$HOME") is not supported.  Environment
;    variables can be expanded using this syntax: "%(ENV_HOME)s".
;  - Comments must have a leading space: "a=b ;comment" not "a=b;comment".

[unix_http_server]
file=/tmp/supervisor.sock   ; (socket 文件的路径)
;chmod=0700                 ; socket 文件权限 (default 0700)
;chown=nobody:nogroup       ; socket 文件属主:属组
;username=user              ; (启动http的用户 (open server))
;password=123               ; (默认的密码 (open server))

;[inet_http_server]         ; 默认禁用tcp监听的http 服务
;port=127.0.0.1:9001        ; (指定监听在本机ip地址和端口)
;username=user              ; (默认启动http服务的用户)
;password=123               ; (默认的密码)

[supervisord]
logfile=/tmp/supervisord.log ; (主日志文件的存放位置,默认在程序的工作启动目录)
logfile_maxbytes=50MB        ; (主日志文件的最大值,之后进行切割;默认 50MB)
logfile_backups=10           ; (主日志文件备份的数目;默认 10)
loglevel=info                ; (日志级别;默认是info; 其它: debug,warn,trace)
pidfile=/tmp/supervisord.pid ; (supervisord 运行时的pidfile路径;默认 supervisord.pid)
nodaemon=false               ; (如果为true,程序就以前台运行;默认是 false)
minfds=1024                  ; (min. 启动有效的文件描述符数目;默认 1024)
minprocs=200                 ; (min. 有效进程描述符;默认 200)
;umask=022                   ; (进程文件创建的默认权限;默认 022)
;user=chrism                 ; (默认是当前启动的用户)
;identifier=supervisor       ; (supervisord 标识符, 默认是'supervisor')
;directory=/tmp              ; (默认启动时间不会切换)
;nocleanup=true              ; (在启动时不清理临时文件;默认值为false)
;childlogdir=/tmp            ; ('AUTO' 子进程日志目录, 默认 $TEMP)
;environment=KEY="value"     ; (增加一个环境变量键值对:key=”value“)
;strip_ansi=false            ; (在log日志里去掉ansi转义编码; 默认是 false)

; 下面的部分选项必须保留在RPC的配置文件中
; (supervisorctl/web 接口) 使用以下配置来管理
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket
;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
;username=chris              ; should be same as http_username if set
;password=123                ; should be same as http_password if set
;prompt=mysupervisor         ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history  ; use readline history if available

; 以下是被管理的示例程序显示所有可能用到的配置。
; 创建一个或“多个”程序: 要遵循以下的键值对规则。
; supervisor.

;[program:theprogramname]
;command=/bin/cat              ; 程序的启动命令 (使用绝对路径)
;process_name=%(program_name)s ; process_name 表示 (默认是 %(program_name)s)
;numprocs=1                    ; 启动时的进程数 (默认 1)
;directory=/tmp                ; 执行时切换到的目录 (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=999                  ; 相对启动优先级(default 999)
;autostart=true                ; 是否跟随supervisord程序启动该监控程序 (default: true)
;startsecs=1                   ; # 在设定时间内,程序必须保持运行 (def. 1)
;startretries=3                ; 当启动失败时尝试的最大次数(default 3)
;autorestart=unexpected        ; 如果退出后,什么状态退出的去重启,默认非意外的(def: unexpected)
;exitcodes=0,2                 ; 'expected' 符合退出代码之后去重启 (default 0,2)
;stopsignal=QUIT               ; 用于杀死进程的信号 (default TERM)
;stopwaitsecs=10               ; 最大等待秒数 SIGKILL (default 10)
;stopasgroup=false             ; 发送停止信号到Unix进程组 (default false)
;killasgroup=false             ; SIGKILL UNIX进程组 (def false)
;user=chrism                   ; setuid to this UNIX account to run the program
;redirect_stderr=true          ; 是否开启程序标准错误输出的重定向 (default false)
;stdout_logfile=/a/path        ; 标准输出路径; default AUTO
;stdout_logfile_maxbytes=1MB   ; 文件最大大小 # 日志文件进行切割 (default 50MB)
;stdout_logfile_backups=10     ; # 日志文件备份数目 (default 10)
;stdout_capture_maxbytes=1MB   ; ‘捕获模式’中的字节数 (default 0)
;stdout_events_enabled=false   ; 在标准输出写入文件时发出事件 (default false)
;stderr_logfile=/a/path        ; 标准错误输出, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB   ; 文件最大大小 # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10     ; # of stderr logfile backups (default 10)
;stderr_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;environment=A="1",B="2"       ; 添加进程环境变量 (def no adds)
;serverurl=AUTO                ; 覆盖serverurl计算 (childutils)

;下面是event事件部分所有可能设置的值,大部分同上面一样。
; eventlistener subsection values, create one or more 'real'
; eventlistener: sections to be able to handle event notifications
; sent by supervisor.

;[eventlistener:theeventlistenername]
;command=/bin/eventlistener    ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1                    ; number of processes copies to start (def 1)
;events=EVENT                  ; event notif. types to subscribe to (req'd)
;buffer_size=10                ; event buffer queue size (default 10)
;directory=/tmp                ; directory to cwd to before exec (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=-1                   ; the relative start priority (default -1)
;autostart=true                ; start at supervisord start (default: true)
;startsecs=1                   ; # of secs prog must stay up to be running (def. 1)
;startretries=3                ; max # of serial start failures when starting (default 3)
;autorestart=unexpected        ; autorestart if exited after running (def: unexpected)
;exitcodes=0,2                 ; 'expected' exit codes used with autorestart (default 0,2)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false             ; send stop signal to the UNIX process group (default false)
;killasgroup=false             ; SIGKILL the UNIX process group (def false)
;user=chrism                   ; setuid to this UNIX account to run the program
;redirect_stderr=false         ; redirect_stderr=true is not allowed for eventlisteners
;stdout_logfile=/a/path        ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10     ; # of stdout logfile backups (default 10)
;stdout_events_enabled=false   ; emit events on stdout writes (default false)
;stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10     ; # of stderr logfile backups (default 10)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;environment=A="1",B="2"       ; process environment additions
;serverurl=AUTO                ; override serverurl computation (childutils)

; The below sample group section shows all possible group values,
; create one or more 'real' group: sections to create "heterogeneous"
; process groups.

;[group:thegroupname]
;programs=progname1,progname2  ; 这里的progname1,progname2就是定义的监控管理程序的名字,如[program:x]这里就是x
;priority=999                  ; the relative start priority (default 999)

;  下面的 [include] 选项只能包含一个files 设置,功能是定义supervisor管理程序的配置文件,可以单独的移除去,和主配置文件分开,方便。; setting can list multiple files (separated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*
; include files themselves.

;[include]
;files = relative/directory/*.ini         ;定义管理监控程序的配置文件的路径

管理命令

supervisorctl status # 查看所有进程的状态
supervisorctl stop program_name    # 停止
supervisorctl start program_name   # 启动
supervisorctl restart program_name # 重启某个程序
supervisorctl stop all             # 停止所有
supervisorctl reload  # 重新启动配置中的所有程序
supervisorctl update  # 更新新的配置到supervisord
supervisord -c /etc/supervisord.conf # yum 安装后,启动默认方式
supervisorctl start/stop/restart/status group       # 管理所有属于名为 group 这个分组的进程
supervisorctl start/stop/restart/status group:name1 # 管理分组里指定的进程

添加配置实例

; 设置进程的名称,使用 supervisorctl 来管理进程时需要使用该进程名 我这里就叫做nginx了!
[program:nginx] 
; 需要执行的命令
command=/usr/sbin/nginx -g 'daemon off;' 
; 命令执行的目录或者说执行 command 之前,先切换到工作目录 可以理解为在执行命令前会切换到这个目录 在我这基本没啥用
directory=/etc/nginx
; 是否自动启动
autostart=true
; 程序意外退出是否自动重启
autorestart=true
; 如果为true,则stderr的日志会被写入stdout日志文件中,  理解为重定向输出的日志
redirect_stderr=true
; 启动优先级 越大优先级越高
priority=10
; 子进程的stdout的日志路径 输出日志文件,如果日志路径不存在,会导致无法启动错误
stdout_logfile=/data/logs/supervisord/nginx.log
; 错误日志文件 当 redirect_stderr=true 时,stderr_logfile就不用
stderr_logfile=/data/logs/supervisord/nginx.err.log 

修改后,更新 supervisor


supervisorctl reread # 重新读取配置
supervisorctl update # 更新配置
supervisorctl restart nginx  # 重启 nginx
killall nginx  # 杀掉所有的 nginx 进程. 已经杀不死了 说明守护进程配置成功

宝塔Supervisor管理器生成实例

[program:test-call-serve-excel]
command=redis xxxxxxx
directory=/www/wwwroot/test-call
autorestart=true
startsecs=3
startretries=3
stdout_logfile=/www/server/panel/plugin/supervisor/log/test-serve.out.log
stderr_logfile=/www/server/panel/plugin/supervisor/log/test-serve.err.log
stdout_logfile_maxbytes=2MB
stderr_logfile_maxbytes=2MB
user=www
priority=999
numprocs=4
process_name=%(program_name)s_%(process_num)02d
]]>
0 https://www.ascwh.com/399.html#comments https://www.ascwh.com/feed/category/php/
Mac 配置 WireGuard 客户端 https://www.ascwh.com/397.html https://www.ascwh.com/397.html Thu, 19 May 2022 21:14:00 +0800 ASCWH WireGuard 是一个易于配置、快速且安全的开源组网,它利用了最新的加密技术。目的是提供一种更快、更简单、更精简的通用 VPN,它可以轻松地在树莓派这类低端设备到高端服务器上部署。
IPsec 和 OpenVPN 等大多数其他解决方案是几十年前开发的。安全研究人员和内核开发人员 Jason Donenfeld 意识到它们速度慢且难以正确配置和管理。
这让他创建了一个新的开源 VPN 协议和解决方案,它更加快速、安全、易于部署和管理。
WireGuard 最初是为 Linux 开发的,但现在可用于 Windows、macOS、BSD、iOS 和 Android。它仍在活跃开发中

安装 WireGuard 客户端

brew install wireguard-tools

配置 WireGurad 客户端

sudo mkdir /usr/local/etc/wireguard
sudo touch /usr/local/etc/wireguard/wg0.conf

wg0.conf 文件如下:

[Interface]
Address = 10.200.200.2/32
PrivateKey = <client_private_key>
MTU = 1420

[Peer]
PublicKey = <server_public_key>
Endpoint = <SERVER_IP:SERVER_PORT>
AllowedIPs = 10.200.0.0/16
PersistentKeepalive = 25

启动 WireGuard

sudo wg-quick up wg0  # 启动Wireguard
sudo wg-quick down wg0 #关闭WIreguard
sudo wg # 查看Wireguard状态
]]>
0 https://www.ascwh.com/397.html#comments https://www.ascwh.com/feed/category/php/
PHP获取今天、明天、昨天、上周、本周、上月、本月 的基础方法 https://www.ascwh.com/396.html https://www.ascwh.com/396.html Tue, 17 May 2022 23:28:30 +0800 ASCWH <?php declare (strict_types=1); namespace app\librarys; /** * Class DateRange 日期范围处理 * @package app\librarys */ class DateRange { /** * @var */ private static $instance; /** * @var */ public $timestamp; /** * @var */ public $currentDate; /** * */ private function __construct() { $this->currentDate = time(); } /** * @title __clone */ private function __clone() { } /** * @title getInstance * @return \app\librarys\DateRange */ public static function getInstance() { if (!self::$instance) { self::$instance = new self(); } return self::$instance; } /** * @title setTimestamp 设置时间格式 默认为时间戳, 可传入 date 方法的 时间参数 * * @param $timestamp * * @return */ public function setTimestamp($timestamp) { $this->timestamp = $timestamp; return $this; } /** * @title setCurrentDate * * @param $date * * @return \app\librarys\DateRange */ public function setCurrentDate($date) { $this->currentDate = $date; return $this; } /** * @title execute 传入时间格式 并返回开始时间和结束时间 * * @param $dateType * * @return array|false[]|float[]|int[]|string[] */ public final function execute($dateType) { switch ($dateType) { case 'today': // 今天 $range = $this->getDateRangeByToday(); break; case 'tomorrow': // 明天 $range = $this->getDateRangeByTomorrowday(); break; case 'yesterday': // 昨天 $range = $this->getDateRangeByYesterday(); break; case 'week': // 本周 $range = $this->getDateRangeByWeek(); break; case 'lastWeek': // 上周 $range = $this->getDateRangeByLastWeek(); break; case 'month': // 本月 $range = $this->getDateRangeByMonth(); break; case 'lastMonth': // 上月 $range = $this->getDateRangeByLastMonth(); break; default: $range = []; } return $range; } /** * @title getDateRangeByToday 返回今天的时间范围 * @return false[]|float[]|int[]|string[] */ protected function getDateRangeByToday() { return [ $this->getTime(strtotime(date('Y-m-d', $this->currentDate))), $this->getTime(strtotime(date('Y-m-d', $this->currentDate)) + 86400 - 1) ]; } /** * @title getDateRangeByYesterday 返回昨日的时间范围 * @return false[]|float[]|int[]|string[] */ protected function getDateRangeByYesterday() { $yesterday = strtotime('-1 day', $this->currentDate); return [ $this->getTime(strtotime(date('Y-m-d', $yesterday))), $this->getTime(strtotime(date('Y-m-d', $yesterday)) + 86400 - 1) ]; } /** * @title getDateRangeByTomorrowday 返回明天的时间范围 * @return array */ protected function getDateRangeByTomorrowday() { $yesterday = strtotime('+1 day', $this->currentDate); return [ $this->getTime(strtotime(date('Y-m-d', $yesterday))), $this->getTime(strtotime(date('Y-m-d', $yesterday)) + 86400 - 1) ]; } /** * @title getDateRangeByWeek 获取本周的时间范围 * @return array */ protected function getDateRangeByWeek() { return [ $this->getTime(strtotime(date('Y-m-d', strtotime('this week Monday', $this->currentDate)))), $this->getTime(strtotime(date('Y-m-d', strtotime('this week Sunday', $this->currentDate))) + 86400 - 1) ]; } /** * @title getDateRangeByLastWeek 获取上周的时间范围 * @return array */ protected function getDateRangeByLastWeek() { return [ $this->getTime(strtotime(date('Y-m-d', strtotime('last week Monday', $this->currentDate)))), $this->getTime(strtotime(date('Y-m-d', strtotime('last week Sunday', $this->currentDate))) + 86400 - 1) ]; } /** * @title getDateRangeByMonth 获取本月的时间范围 * @return array */ protected function getDateRangeByMonth() { return [ $this->getTime(strtotime(date('Y-m-1', $this->currentDate))), $this->getTime(strtotime(date('Y-m-t', $this->currentDate)) + 86400 - 1) ]; } /** * @title getDateRangeByLastMonth 获取上月的时间范围 * @return false[]|float[]|int[]|string[] */ protected function getDateRangeByLastMonth() { return [ $this->getTime(strtotime(date('Y-m-d', strtotime('first day of previous month', $this->currentDate)))), $this->getTime(strtotime(date('Y-m-d', strtotime('last day of previous month', $this->currentDate))) + 86400 - 1) ]; } /** * @title getTime 按 $this->timestamp 返回时间格式 * * @param string $time * * @return false|float|int|string */ public function getTime($time = '') { switch (true) { case (empty($time)): $timeInt = time(); break; case (is_numeric($time)): $timeInt = $time; break; case (is_string($time)): $timeInt = strtotime($time); if (!$timeInt) { $timeInt = time(); } break; default: $timeInt = time(); } return empty($this->timestamp) ? $timeInt : date($this->timestamp, (int)$timeInt); } /** * @title getTimeRangeByDateRange 获取给定的开始时间和结束时间的每天时间 * * @param int $start * @param int $end * * @return array|false */ public function getTimeRangeByDateRange(int $start, int $end) { if ($start > $end) { return false; } $diff = ($end - $start) / 86400; $day = []; for ($i = 0; $i <= $diff; $i++) { $day[] = $this->getTime($start + $i * 86400); } return $day; } } ]]> 0 https://www.ascwh.com/396.html#comments https://www.ascwh.com/feed/category/php/ PhpSpreadsheet 导入导出基础应用 https://www.ascwh.com/392.html https://www.ascwh.com/392.html Thu, 02 Sep 2021 16:56:00 +0800 ASCWH 由于PHPExcel已经不再维护,PhpSpreadsheet是PHPExcel的下一个版本。PhpSpreadsheet是一个用纯PHP编写的库,并引入了命名空间,PSR规范等。这里简单介绍下PhpSpreadsheet的导入导出功能。

1、安装

使用composer安装:
composer require phpoffice/phpspreadsheet

GitHub下载:https://github.com/PHPOffice/PhpSpreadsheet

2、excel文件导出

    // 命名空间引入
    use PhpOffice\PhpSpreadsheet\IOFactory;
    use PhpOffice\PhpSpreadsheet\Spreadsheet;

    /**
     * @title export 导出
     */
    public function export() {
        $data  = [
            ['title1' => '111', 'title2' => '222'],
            ['title1' => '111', 'title2' => '222'],
            ['title1' => '111', 'title2' => '222']
        ];
        $title = ['标题1', '标题2'];
        array_unshift($data, $title);

        // Create new Spreadsheet object
        $spreadsheet = new Spreadsheet();
        $sheet       = $spreadsheet->getActiveSheet()->setTitle('工作表标题');

        foreach ($data as $k => $item) {
            $column = 1;
            foreach ($item as $value) {
            // 因不想定义 A…  一大串大写字母 所以使用数字单元格坐标设置单元格值。
                $sheet->setCellValueByColumnAndRow($column, $k + 1, $value);
                $column++;
            }
        }

        // Redirect output to a client’s web browser (Xlsx)
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="01simple.xlsx"');
        header('Cache-Control: max-age=0');
        // If you're serving to IE 9, then the following may be needed
        header('Cache-Control: max-age=1');
        // If you're serving to IE over SSL, then the following may be needed
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');              // Date in the past
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
        header('Cache-Control: cache, must-revalidate');               // HTTP/1.1
        header('Pragma: public');                                      // HTTP/1.0
        $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');
        exit;
    }

3、读取excel文件内容

/**
    // 命名空间引入
    use PhpOffice\PhpSpreadsheet\IOFactory;
    
    
     * @title  readExcel 读取excel文件内容
     * @return array
     */
    public function readExcel() {
        $inputFileName = './test.xlsx';
        $spreadsheet   = IOFactory::load($inputFileName);
        //  toArray() 具体参数参考官方文档
        $sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
        return $sheetData;
    }
]]>
0 https://www.ascwh.com/392.html#comments https://www.ascwh.com/feed/category/php/
Redis String 字符串操作 https://www.ascwh.com/391.html https://www.ascwh.com/391.html Sat, 05 Jun 2021 21:26:36 +0800 ASCWH /** * * String操作 字符串操作 */ //设置键值:成功返回true,否则返回false,键值不存在则新建,否则覆盖 $redis->set('string', 'hello world!'); //从左往右第五个字符开始替换为另一指定字符串,成功返回替换后新字符串的长度。 $redis->setRange('string',6, '1111'); //截取字符串里指定key对应的value里的第一个到第七个字符。 $redis->getRange('string', 0, 6); //添加键,返回旧键值:若key不存在则创建键值,返回false $redis->getSet('ad', 'hi man'); //一次设置多个键值对:成功返回true $redis->mset(['name' => 'jet', 'age' => 18]); //一次获取多个key的值:返回一个键值对数组,其中不存在的key值为false。 $redis->mget(['name', 'age']); //创建一个具有时间限制的键值,过期则删除,秒为单位,成功返回true $redis->setex('name', 10, 'jetwu'); //创建一个具有时间限制的键值,过期则删除,毫秒为单位,成功返回true $redis->psetex('name', 10, 'jetwu'); //key的值不存在时,添加key并返回true,key存在返回false。 $redis->setnx('name', 'boby'); //setnx命令的批量操作。只有在给定所有key都不存在的时候才能设置成功,只要其中一个key存在,所有key都无法设置成功。 $redis->msetnx(['name' => '11', 'name1' => '22']); //获取指定key存储的字符串的长度,key不存在返回0,不为字符串返回false。 $redis->strlen('name'); //将指定key存储的数字值增加1。若key不存在会先初始化为0再增加1,若key存储的不是整数值则返回false。成功返回key新值。 $redis->incr('name'); //给指定key存储的数字值增加指定增量值。 $redis->incrBy('age', 10); //给指定key存储的数字值增加指定浮点数增量。 $redis->incrByFloat('age', 1.5); //将指定key存储的数字值减一。 $redis->decr('age'); //将指定key存储的数字值减去指定减量值。 $redis->decrBy('age', 10); //为指定key值尾部添加字符,返回值得长度,若key不存在则创建 $redis->append('name', 'haha'); //获取键值:成功返回String类型键值,若key不存在或不是String类型则返回false $redis->get('name'); ]]> 0 https://www.ascwh.com/391.html#comments https://www.ascwh.com/feed/category/php/ Redis Hash 哈希操作 https://www.ascwh.com/390.html https://www.ascwh.com/390.html Sat, 05 Jun 2021 21:25:26 +0800 ASCWH /** * * Hash 哈希操作 可理解为数据库操作 */ //为user表中的字段赋值。成功返回1,失败返回0。若user表不存在会先创建表再赋值,若字段已存在会覆盖旧值。 $redis->hSet('user', 'name', '222'); //获取user表中指定字段的值。若user表不存在则返回false。 $redis->hGet('user', 'realname'); //查看user表的某个字段是否存在,存在返回true,否则返回false。 $redis->hExists('user', 'realname'); //删除user表的一个字段,不支持删除多个字段。成功返回1,否则返回0。 $redis->hDel('user', '222'); //同时设置某个user表的多个字段值。成功返回true。 $redis->hMset('user', ['name' => 'jet', 'age' => 18]); //同时获取某个user表的多个字段值。其中不存在的字段值为false。 $redis->hMget('user', ['name', 'age']); //获取某个user表所有的字段和值。 $redis->hGetAll('user'); //获取某个user表所有字段名。user表不存在时返回空数组,key不为user表时返回false。 $redis->hKeys('user'); //获取某个user表所有字段值。 $redis->hVals('user'); //为user表中不存在的字段赋值。若user表不存在则先创建,若字段已存在则不做任何操作。设置成功返回true,否则返回false。 $redis->hSetNx('user', 'realname', 'jetwu'); //获取某个user表的字段数量。若user表不存在返回0,若user不是hash表则返回false。 $redis->hLen('user'); //为user表中的指定字段加上指定的数值,若user表不存在则先创建,若字段不存在则先初始化值为0再进行操作,若字段值为字符串则返回false。设置成功返回字段新值。 $redis->hIncrBy('user', 'age', 10); //为user表中的指定字段加上指定浮点数值。 $redis->hIncrBy('user', 'age', 1.5); ]]> 0 https://www.ascwh.com/390.html#comments https://www.ascwh.com/feed/category/php/ Redis Zset 有序集合操作 https://www.ascwh.com/389.html https://www.ascwh.com/389.html Sat, 05 Jun 2021 21:23:30 +0800 ASCWH /** * * Zset操作 sorted set操作 * 有序集合 * sorted set 它在set的基础上增加了一个顺序属性,这一属性在修改添加元素的时候可以指定,每次指定后,zset会自动从新按新的值调整顺序 * */ // //将一个或多个元素插入到集合里面,默认从尾部开始插入 // //如果要在头部插入,则找一个元素,在元素后面添加一个你需要插入的元素即可 $redis->zAdd('sorted1',100,'坑啊',98.999,99,90,90,80,80,60,60,70,70); $redis->zAdd //('集合',浮点数(元素),'key',(插入头部的数据),key); // //返回有序集中指定区间内的成员。成员按分数值递增排序,分数值相同的则按字典序来排序。 // //参数:第四个参数表示是否返回各个元素的分数值,默认为false。 $redis->zRange('sorted', 0, -1, true); // //返回有序集中指定区间内的成员。成员按分数值递减排序,分数值相同的则按字典序的倒序来排序。 $redis->zReverseRange('sorted', 0, -1, true); // //返回有序集中指定分数区间的成员列表,按分数值递增排序 $redis->zRangeByScore('sorted', 10, 99); // //自定义返回的序集返回起始位置及条数 $redis->zRangeByScore('sorted', 0,90,['limit' =>[0,2]]); //返回有序集中指定分数区间的成员列表,按分数值递减排序,分数值相同的则按字典序的倒序来排序。注意,区间表示的时候大值在前,小值在后。 $redis->zRevRangeByScore('sorted', 100, 90); // //迭代有序集合中的元素。 // //可理解为查找指定的值,将元素修改为float类型 // //返回值:[元素名=>分数值,,..] $redis->zscan('sorted', $it, 100, 10); // //返回指定有序集的元素数量,序集的长度。 $redis->zCard('sorted'); // //返回有序集中指定分数区间的成员数量。 $redis->zCount('sorted', 90, 100); // //返回有序集中指定成员的分数值。若成员不存在则返回false。 $redis->zScore('sorted', 'math'); // //返回有序集中指定成员元素的大小排名,按分数值递增排序。分数值最小者排名为0。 $redis->zRank('sorted', 60); // //返回有序集中指定成员元素的排名,按分数值递减排序。分数值最大者排名为0。 $redis->zRevRank('sorted', 70); // //删除有序集中的一个或多个成员,忽略不存在的成员。返回删除的元素个数。 $redis->zRem('sorted', 'chemistry', 'English'); // //删除有序集中指定排名区间的所有成员,返回删除元素个数 $redis->zRemRangeByRank('sorted', 0, 2); // //删除有序集中指定分数值区间的所有成员,返回删除元素的个数 $redis->zRemRangeByScore('sorted', 80, 90); // //对有序集中指定成员的分数值增加指定增量值。若为负数则做减法,若有序集不存在则先创建,若有序集中没有对应成员则先添加,最后再操作。 $redis->zIncrBy('sorted', 2, 'Chinese'); // //计算给定一个或多个有序集的交集,元素相加,并将其存储到目的有序集中 $redis->zinterstore('zset3',['sorted','sorted1']); // //计算给定一个或多个有序集的并集,元素相加,并将其存储到目的有序集中 $redis->zunionstore('zset3',['sorted', 'sorted1']); ]]> 0 https://www.ascwh.com/389.html#comments https://www.ascwh.com/feed/category/php/ Redis Set 操作 https://www.ascwh.com/388.html https://www.ascwh.com/388.html Sat, 05 Jun 2021 21:21:14 +0800 ASCWH //将一个元素加入集合,已经存在集合中的元素则忽略。若集合不存在则先创建,若key不是集合类型则返回false,若元素已存在返回0,插入成功返回1。 $redis->sAdd('set3', '11'); //返回集合中所有成员。 $redis->sMembers('set3'); //判断集合里是否存在指定元素,是返回true,否则返回false。 $redis->sismember('set', 'hello'); //返回集合中元素的数量。 $redis->scard('set'); //随机删除并返回集合里的一个元素。 $redis->sPop('set'); //随机返回(n)个集合内的元素,由第二个参数决定返回多少个 //如果 n 大于集合内元素的个数则返回整个集合 //如果 n 是负数时随机返回 n 的绝对值,数组内的元素会重复出现 $redis->sRandMember('set', -20); //删除集合中指定的一个元素,元素不存在返回0。删除成功返回1,否则返回0。 $redis->srem('set', 'hello'); //模糊搜索相对的元素, //参数:key,迭代器变量,匹配值,每次返回元素数量(默认为10个) $redis->sscan('set', $it, 's*', 5); //将指定一个源集合里的值移动到一个目的集合。成功返回true,失败或者源集合值不存在时返回false。 //参数:源集合,目标集合,移动的元素 $redis->sMove('set', 'set2', 'sdf4'); //以第一个集合为标准,后面的集合对比,返回差集 $redis->sDiff('set', 'set2','set3'); //参数:第一个参数为目标集合,存储缺少的值(三个集合相加,同样字段覆盖,组合成一个新的集合)返回第一个参数所增加的值的个数。 $redis->sDiffStore('set', 'set3', 'set2'); //返回所有集合的相同值,必须所有集合都有,不存在的集合视为空集。 $redis->sInter('set', 'set3', 'set2'); //参数:第一个参数为目标集合,存储后面集合的交集 //若目的集合已存在则覆盖它。返回交集元素个数,否则返回储存的交集 $redis->sInterStore('set4', 'set', 'set3'); //把所有集合合并在一起并返回 $redis->sUnion('set', 'set2', 'set3'); //以第一个集合为目标,把后面的集合合并在一起,存储到第一个集合里面,如果已经存在则覆盖掉,并返回并集的个数 $redis->sUnionStore('set4', 'set', 'set2', 'set3'); ]]> 0 https://www.ascwh.com/388.html#comments https://www.ascwh.com/feed/category/php/