1. 程式人生 > >【redis 封裝】

【redis 封裝】

<?php
/**
 * Created by PhpStorm.
 * User: andy
 * Date: 18/3/26
 */
namespace app\common\lib\redis;
class Predis {
    public $redis = "";
    /**
     * 定義單例模式的變數
     * @var null
     */
    private static $_instance = null;

    public static function getInstance() {
        if(empty(self::$_instance
)) { self::$_instance = new self(); } return self::$_instance; } private function __construct() { $this->redis = new \Redis(); $result = $this->redis->connect(config('redis.host'), config('redis.port'), config('redis.timeOut')); if
($result === false) { throw new \Exception('redis connect error'); } } /** * set * @param $key * @param $value * @param int $time * @return bool|string */ public function set($key, $value, $time = 0 ) { if(!$key) { return
''; } if(is_array($value)) { $value = json_encode($value); } if(!$time) { return $this->redis->set($key, $value); } return $this->redis->setex($key, $time, $value); } /** * get * @param $key * @return bool|string */ public function get($key) { if(!$key) { return ''; } return $this->redis->get($key); } /** * @param $key * @return array */ public function sMembers($key) { return $this->redis->sMembers($key); } /** * @param $name * @param $arguments * @return array */ public function __call($name, $arguments) { //echo $name.PHP_EOL; //print_r($arguments); if(count($arguments) != 2) { return ''; } $this->redis->$name($arguments[0], $arguments[1]); } }

總結:採用單例模式:減少資源不必要開銷