php workerman 使用predis 在命令运行一段时间后就断开了

发布于 2022-09-30 23:11:26 字数 5279 浏览 18 评论 0


这是我运行一段时间之后就报错,刚开始是没问题的,能连接上,后面就不行了,然后进程就断开了

<?php
namespace pcl\service;

class RedisService {

//predis对象
public $handler;

public static $expire_time = 0;

//redis服务器状态默认开始
// private $redis_status = 1;

// 存储对象
private static $moduleObj = array();

public function __construct() {
    $this->handler = $this->getPredisObj();
}

/**
 * [get_servers 获得redis服务器配置]
 * @return [type] [description]
 */
public static function get_servers(){

    $options = array (
        'scheme' => 'tcp',
        'host'   => '127.0.0.1',
        'port'   => '6379',
        #'password' => '',
    );
    return $options;
}

/**
 * 获取predis 单例
 * @return object
 */
public static function getPredisObj(){
    if (empty(self::$moduleObj['predisObj']) || is_null(self::$moduleObj['predisObj'])){
        self::$moduleObj['predisObj'] = new \Predis\Client(self::get_servers());

    }
    return self::$moduleObj['predisObj'];
}

public function connect(){
    $this->handler->connect();
}

/**
 * 读取缓存
 * @access public
 * @param string $name 缓存变量名
 * @return mixed
 */
public function get($name) {

    $value = $this->handler->get($name);
    $jsonData  = json_decode( $value, true );
    return ($jsonData === NULL) ? $value : $jsonData;   //检测是否为JSON数据 true 返回JSON解析数组, false返回源数据
}
//静态get
public static function sGet($name) {

    $value = self::getPredisObj()->get($name);
    $jsonData  = json_decode( $value, true );
    return ($jsonData === NULL) ? $value : $jsonData;   //检测是否为JSON数据 true 返回JSON解析数组, false返回源数据
}

/**
 * 写入缓存
 * @access public
 * @param string $name 缓存变量名
 * @param mixed $value  存储数据
 * @param integer $expire  有效时间(秒)
 * @return boolean
 */
public function set($name, $value, $expire = null) {

    //对数组/对象数据进行缓存处理,保证数据完整性
    $value  =  (is_object($value) || is_array($value)) ? json_encode($value) : $value;
    if(is_int($expire) && $expire) {
        $result = $this->handler->setex($name, $expire, $value);
    }else{
        $result = $this->handler->set($name, $value);
    }

    return $result;
}
#静态set
public static function sSet($name, $value, $expire = null) {

    //对数组/对象数据进行缓存处理,保证数据完整性
    $value  =  (is_object($value) || is_array($value)) ? json_encode($value) : $value;
    if(is_int($expire) && $expire) {
        $result = self::getPredisObj()->setex($name, $expire, $value);
    }else{
        $result = self::getPredisObj()->set($name, $value);
    }

    return $result;
}
/**
 * 删除缓存
 * @access public
 * @param string $name 缓存变量名
 * @return boolean
 */
public function del($name) {
    return $this->handler->del($name);
}

/**
 * 检查key值是否存在
 * @access public
 * @param string $name 缓存变量名
 * @return boolean
 */
public function exists($name) {
    return $this->handler->exists($name);
}

public function hset($hash_key = '',$key = '',$val = ''){
    return $this->handler->hset($hash_key,$key,$val);
}

public function hget($hash_key = '',$key = ''){
    return $this->handler->hget($hash_key,$key);
}

public function hdel($hash_key = '',$key = ''){
    return $this->handler->hdel($hash_key,$key);
}

//hmset/hmget 存取多个元素到hash表
//$redis -> hmset ( 'hash1' , array ( 'key3' => 'v3' , 'key4' => 'v4' ) ) ;
//$redis -> hmget ( 'hash1' , array ( 'key3' , 'key4' ) ) ;  //返回相应的值 array('v3','v4')
public function hmset($hash_key = '', $array = []){
    return $this->handler->hmset($hash_key, $array);
}
////hsetnx 增加一个元素,但不能重复
public function hsetnx($hash_key = '', $key , $val = ''){
    return $this->handler->hsetnx($hash_key, $key, $val );
}

public function hmget($hash_key = '', $array = []){
    return $this->handler->hmget($hash_key, $array);
}
//hgetall 返回整个hash表元素
public function hgetall($hash_key = ''){
    return $this->handler->hgetall($hash_key);
}

public function hexists($hash_key = '',$key = ''){
    return $this->handler->hexists($hash_key, $key);
}


/**
 * [set_status 设置redis服务器方法]
 */
public function set_status(){
    $this->redis_status = 0;
}

public function incr($key){
    return $this->handler->incr($key);
}

/**
 * 创建集合
 */
public function sAdd($key,$array = []){
    $this->handler->sadd($key,$array);
}

/**
 * 查看集合中的成员
 */
public function sMembers($key){
    return $this->handler->smembers($key);
}

/**
 * 移除集合中的一个或者多个元素
 */
public function sRem($key,$array = []){
    if(!empty($array)){
        foreach($array as $value){
            $this->handler->srem($key,$value);
        }
    }
}

/**
 * 开启事务
 */
public function multi(){
    $this->handler->multi();
}

/**
 * 提交事务
 */
public function exec(){
    $this->handler->exec();
}

/**
 * 取消事务
 */
public function discard(){
    $this->handler->discard();
}

public function hkeys($key){
    return $this->handler->hkeys($key);
}

/**
 * 查看键的剩余时间
 */
public function ttl($key){
    return $this->handler->ttl($key);
}

public function siSmember($key,$member){
    return $this->handler->sismember($key,$member);
}

}

这是我写的一个redis类 不知道是哪里有问题了 运行一段时间后就报错 Error while reading line from the server. [tcp://127.0.0.1:6379] 进程也终止了 求各位大佬帮忙解决一下

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扬花落满肩 2022-10-07 23:11:26

加个超时限制为 0 或者 -1 试试

$redis = new Predis\Client('tcp://10.0.0.1:6379?read_write_timeout=0');

来源:php - Predis is giving 'Error while reading line from server' - Stack Overflow

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文