首页 > 编程知识 正文

php实现消息推送,redis实现消息队列

时间:2023-05-03 20:57:44 阅读:108545 作者:530

我最近在工作中遇到了一个需求。 要向所有用户发送公告,用户可以删除或设置为已读。 这是数据无法实现的。 我通过在redis模拟中合并数据来实现这个功能。 说了这么多胡话,还是给我晾衣服吧。

直接粘贴代码

//*

*消息列表类

* redis key类型为set

* yxt_notice_read_已读列表(表后缀uid除以1000 ) ) ) ) ) )。

*保存方式:密钥消息id用户id (以逗号分隔) ) ) ) ) ) ) ) ) ) ) ) )。

* yxt_notice_del已读列表

*保存方式:密钥值用户id消息id (逗号分隔) ) )。

* author fong

*/

class messagebox扩展redis

{

公共$ uid;

private $ readlist key=' yxt _ notice _ read _ ';

private $ dellistkey=' yxt _ notice _ del ';

公共$ mid; //消息id

私有$ list size=1000;

权限$ init=false;

公共函数_ _ construct (

{

$ this-connect (config (redis.host )、config )、redis.port );

$ this-auth (config (redis.password ) );

}

//添加已读信息

publicfunctionread($uid,$mid ) {

$this-uid=$uid;

$this-mid=$mid;

$this-buildKey (;

$ RES=$ this-hget ($ this-readlist key,$this-mid );

if(isset($res ) in_array ) $this-uid,explode )、'、$RES ) )返回;

if(empty($RES ) ) $res=';

$this-hset($this-readlistkey,$this-mid,$res.','.$this-uid );

}

public function getIndexKey (

返回ceil ($ this-uid/$ this-list size );

}

//删除一个已读信息

publicfunctiondel($uid,$mid ) {

$this-uid=$uid;

$this-mid=$mid;

$this-buildKey (;

$ RES=$ this-hget ($ this-dellistkey,$this-uid );

if(isset($res ) in_array ) $this-mid,explode )、'、$RES ) )返回;

if(empty($RES ) ) $res=';

$this-hset($this-Dellistkey,$this-uid,$res.','.$this-mid );

}

//连接已读列表的键值

公共功能构建密钥

if($this-init )返回; //防止多次重复时,改变键值

$ this-readlist key=$ this-readlist key.$ this-getindexkey (;

$this-init=true;

}

//获取用户已读列表

publicfunctiongetreadlist($uid ) {

$this-uid=$uid;

$this-buildKey (;

$ RES=$ this-Hg etall ($ this-readlist key );

Foreach($RESas$k=$v ) {

if(in_array($uid,explode )、'、$v ) ) )。

$lists[]=$k;

}

}

返回$ lists;

}

//获取用户删除的列表

publicfunctiongetdellist($uid ) {

$this-uid=$uid;

$this-buildKey (;

//var_dump($this-hget ) $this-Dellistkey,$this-uid ); die;

returntrim($this-hget ) $this-Dellistkey,$this-uid ),',';

}

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。