<?php
/**
 * Predis缓存驱动
 * 要求安装phpredis扩展:https://github.com/nicolasff/phpredis
 @date 2016-06-15
 @edit 2019-08-01
 */

namespace App\Common\Lib\Redis;

class PredisService{

    //predis对象
    private $handler;

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

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

    public function __construct() {

        $this->handler = self::getPredisObj();
    }

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

        $redis_conf = C('MSG');
        $options = array (
            'scheme' => 'tcp',
            'host'   => '127.0.0.1',
            'port'   => 6379,
//            'password' => '123456',
        );
        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'];
    }

    /**
     * 读取缓存
     * @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返回源数据
    }

    /**
     * 写入缓存
     * @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;
    }

    /**
     * 删除缓存
     * @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 );
    }
    //hash mget
    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);
    }

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

    /**
     * [get_status 获得redis服务器状态]
     * @return [type] [description]
     */
//    public function get_redis_status(){
//        return $this->redis_status;
//    }

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

}

记录一个laravel-excel导出表格值为0导出excel显示空的解决方法。

最近在使用laravel-excel导出表格的时候,发现假如字段值为0的情况下,导出的excel中直接显示为空,找到一个方法解决,如下. 在laravel-excel的config配置中...

阅读全文

php实现redis延时队列

写了一个简单的类实现 使用方法 $class = new RedisDelayQueue(); $class->execute(); <?php namespace app\common\service; class RedisDelay { ...

阅读全文

php获取包含10w个手机号的txt文件(逗号隔开)

txt文件格式如下 13000000000,13000000005,13000000403,13001010129,13001010688,13001010838,13001016389,13001019538,13001030005,13001042700,13001047495...

阅读全文

欢迎留言