{"id":244,"date":"2024-03-15T05:10:51","date_gmt":"2024-03-14T21:10:51","guid":{"rendered":"https:\/\/669082.xyz\/?p=244"},"modified":"2024-03-15T05:10:51","modified_gmt":"2024-03-14T21:10:51","slug":"php%e6%93%8d%e4%bd%9credis%e7%9a%84%e5%b0%81%e8%a3%85%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/669082.xyz\/index.php\/2024\/03\/15\/php%e6%93%8d%e4%bd%9credis%e7%9a%84%e5%b0%81%e8%a3%85%e7%b1%bb\/","title":{"rendered":"php\u64cd\u4f5credis\u7684\u5c01\u88c5\u7c7b"},"content":{"rendered":"<pre><code class=\"language-php\">&lt;?php\n\n\/**\n * redis\u64cd\u4f5c\u7c7b\n * \u8bf4\u660e\uff0c\u4efb\u4f55\u4e3afalse\u7684\u4e32\uff0c\u5b58\u5728redis\u4e2d\u90fd\u662f\u7a7a\u4e32\u3002\n * \u53ea\u6709\u5728key\u4e0d\u5b58\u5728\u65f6\uff0c\u624d\u4f1a\u8fd4\u56defalse\u3002\n * \u8fd9\u70b9\u53ef\u7528\u4e8e\u9632\u6b62\u7f13\u5b58\u7a7f\u900f\n *\n *\/\nclass RedisTool\n{\n    private $redis;\n\n    \/\/\u5f53\u524d\u6570\u636e\u5e93ID\u53f7\n    protected $dbId=0;\n\n    \/\/\u5f53\u524d\u6743\u9650\u8ba4\u8bc1\u7801\n    protected $auth;\n\n    \/**\n     * \u5b9e\u4f8b\u5316\u7684\u5bf9\u8c61,\u5355\u4f8b\u6a21\u5f0f.\n     * @var \\iphp\\db\\Redis\n     *\/\n    static private $_instance=array();\n\n    private  $k;\n\n    \/\/\u8fde\u63a5\u5c5e\u6027\u6570\u7ec4\n    protected $attr=array(\n        \/\/\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4\uff0credis\u914d\u7f6e\u6587\u4ef6\u4e2d\u9ed8\u8ba4\u4e3a300\u79d2\n        &#039;timeout&#039;=&gt;30,\n        \/\/\u9009\u62e9\u7684\u6570\u636e\u5e93\u3002\n        &#039;db_id&#039;=&gt;0,\n    );\n\n    \/\/\u4ec0\u4e48\u65f6\u5019\u91cd\u65b0\u5efa\u7acb\u8fde\u63a5\n    protected $expireTime;\n\n    protected $host;\n\n    protected $port;\n\n    private function __construct($config,$attr=array())\n    {\n        $this-&gt;attr        =    array_merge($this-&gt;attr,$attr);\n        $this-&gt;redis       =    new Redis();\n        $this-&gt;port        =    $config[&#039;port&#039;] ? $config[&#039;port&#039;] : 6379;\n        $this-&gt;host        =    $config[&#039;host&#039;];\n        $this-&gt;redis-&gt;connect($this-&gt;host, $this-&gt;port, $this-&gt;attr[&#039;timeout&#039;]);\n\n        if($config[&#039;auth&#039;])\n        {\n            $this-&gt;auth($config[&#039;auth&#039;]);\n            $this-&gt;auth      =    $config[&#039;auth&#039;];\n        }\n\n        $this-&gt;expireTime    =    time() + $this-&gt;attr[&#039;timeout&#039;];\n    }\n\n    \/**\n     * \u5f97\u5230\u5b9e\u4f8b\u5316\u7684\u5bf9\u8c61.\n     * \u4e3a\u6bcf\u4e2a\u6570\u636e\u5e93\u5efa\u7acb\u4e00\u4e2a\u8fde\u63a5\n     * \u5982\u679c\u8fde\u63a5\u8d85\u65f6\uff0c\u5c06\u4f1a\u91cd\u65b0\u5efa\u7acb\u4e00\u4e2a\u8fde\u63a5\n     * @param array $config\n     * @param int $dbId\n     * @return \\iphp\\db\\Redis\n     *\/\n    public static function getInstance($config, $attr = array())\n    {\n        \/\/\u5982\u679c\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5c06\u5176\u8ba4\u4e3a\u662f\u6570\u636e\u5e93\u7684ID\u53f7\u3002\u4ee5\u7b80\u5316\u5199\u6cd5\u3002\n        if(!is_array($attr))\n        {\n            $dbId    =    $attr;\n            $attr    =    array();\n            $attr[&#039;db_id&#039;]    =    $dbId;\n        }\n\n        $attr[&#039;db_id&#039;]    =    $attr[&#039;db_id&#039;] ? $attr[&#039;db_id&#039;] : 0;\n\n        \/\/\u8fd9\u91cc\u628a\u914d\u7f6e\u4fe1\u606f\u53ca\u6570\u636e\u5e93\u53f7 \u505a\u4e86md5 \n        $k    =    md5(implode(&#039;&#039;, $config).$attr[&#039;db_id&#039;]);\n\n        \/\/\u8fd9\u91cc\u5f88\u5de7\u5999 \u7528\u5224\u65ad\u662f\u5426\u662f\u672c\u7c7b\u7684\u5bf9\u8c61 \u6765\u5224\u65ad\u662f\u5426\u5b58\u5728\n        if(! (@static::$_instance[$k] instanceof self))\n        {\n\n            static::$_instance[$k] = new self($config,$attr);\n            static::$_instance[$k]-&gt;k        =    $k;\n            static::$_instance[$k]-&gt;dbId     =    $attr[&#039;db_id&#039;];\n\n            \/\/\u5982\u679c\u4e0d\u662f0\u53f7\u5e93\uff0c\u9009\u62e9\u4e00\u4e0b\u6570\u636e\u5e93\u3002\n            if($attr[&#039;db_id&#039;] != 0){\n                static::$_instance[$k]-&gt;select($attr[&#039;db_id&#039;]);\n            }\n        }\n        \/\/\u5982\u679c\u8fde\u63a5\u8fc7\u671f\n        elseif( time() &gt; static::$_instance[$k]-&gt;expireTime)\n        {\n            static::$_instance[$k]-&gt;close();\n            static::$_instance[$k]         =     new self($config,$attr);\n            static::$_instance[$k]-&gt;k      =    $k;\n            static::$_instance[$k]-&gt;dbId   =    $attr[&#039;db_id&#039;];\n\n            \/\/\u5982\u679c\u4e0d\u662f0\u53f7\u5e93\uff0c\u9009\u62e9\u4e00\u4e0b\u6570\u636e\u5e93\u3002\n            if($attr[&#039;db_id&#039;]!=0){\n                static::$_instance[$k]-&gt;select($attr[&#039;db_id&#039;]);\n            }\n        }\n        return static::$_instance[$k];\n    }\n\n    private function __clone(){}\n\n    \/**\n     * \u6267\u884c\u539f\u751f\u7684redis\u64cd\u4f5c\n     * @return \\Redis\n     *\/\n    public function getRedis()\n    {\n        return $this-&gt;redis;\n    }\n\n    \/*****************hash\u8868\u64cd\u4f5c\u51fd\u6570*******************\/\n\n    \/**\n     * \u5f97\u5230hash\u8868\u4e2d\u4e00\u4e2a\u5b57\u6bb5\u7684\u503c\n     * @param string $key \u7f13\u5b58key\n     * @param string  $field \u5b57\u6bb5\n     * @return string|false\n     *\/\n    public function hGet($key,$field)\n    {\n        return $this-&gt;redis-&gt;hGet($key,$field);\n    }\n\n    \/**\n     * \u4e3ahash\u8868\u8bbe\u5b9a\u4e00\u4e2a\u5b57\u6bb5\u7684\u503c\n     * @param string $key \u7f13\u5b58key\n     * @param string  $field \u5b57\u6bb5\n     * @param string $value \u503c\u3002\n     * @return bool \n     *\/\n    public function hSet($key,$field,$value)\n    {\n        return $this-&gt;redis-&gt;hSet($key,$field,$value);\n    }\n\n    \/**\n     * \u5224\u65adhash\u8868\u4e2d\uff0c\u6307\u5b9afield\u662f\u4e0d\u662f\u5b58\u5728\n     * @param string $key \u7f13\u5b58key\n     * @param string  $field \u5b57\u6bb5\n     * @return bool\n     *\/\n    public function hExists($key,$field)\n    {\n        return $this-&gt;redis-&gt;hExists($key,$field);\n    }\n\n    \/**\n     * \u5220\u9664hash\u8868\u4e2d\u6307\u5b9a\u5b57\u6bb5 ,\u652f\u6301\u6279\u91cf\u5220\u9664\n     * @param string $key \u7f13\u5b58key\n     * @param string  $field \u5b57\u6bb5\n     * @return int\n     *\/\n    \/\/\u8fd9\u91ccfield\u7684\u53c2\u6570\u683c\u5f0f\u662f  fied1 \u6216\u8005 field1, field2, field3\n    public function hdel($key,$field)\n    {\n        $fieldArr=explode(&#039;,&#039;,$field);\n        $delNum=0;\n\n        foreach($fieldArr as $row)\n        {\n            $row=trim($row);\n            $delNum+=$this-&gt;redis-&gt;hDel($key,$row);\n        }\n\n        return $delNum;\n    }\n\n    \/**\n     * \u8fd4\u56dehash\u8868\u5143\u7d20\u4e2a\u6570\n     * @param string $key \u7f13\u5b58key\n     * @return int|bool\n     *\/\n    public function hLen($key)\n    {\n        return $this-&gt;redis-&gt;hLen($key);\n    }\n\n    \/**\n     * \u4e3ahash\u8868\u8bbe\u5b9a\u4e00\u4e2a\u5b57\u6bb5\u7684\u503c,\u5982\u679c\u5b57\u6bb5\u5b58\u5728\uff0c\u8fd4\u56defalse\n     * @param string $key \u7f13\u5b58key\n     * @param string  $field \u5b57\u6bb5\n     * @param string $value \u503c\u3002\n     * @return bool\n     *\/\n    public function hSetNx($key,$field,$value)\n    {\n        return $this-&gt;redis-&gt;hSetNx($key,$field,$value);\n    }\n\n    \/**\n     * \u4e3ahash\u8868\u591a\u4e2a\u5b57\u6bb5\u8bbe\u5b9a\u503c\u3002\n     * @param string $key\n     * @param array $value\n     * @return array|bool\n     *\/\n    public function hMset($key,$value)\n    {\n        if(!is_array($value))\n            return false;\n        return $this-&gt;redis-&gt;hMset($key,$value); \n    }\n\n    \/**\n     * \u4e3ahash\u8868\u591a\u4e2a\u5b57\u6bb5\u8bbe\u5b9a\u503c\u3002\n     * @param string $key\n     * @param array|string $value string\u4ee5&#039;,&#039;\u53f7\u5206\u9694\u5b57\u6bb5\n     * @return array|bool\n     *\/\n    \/\/\u8fd9\u91cc\u539f\u535a\u4e3b\u5199\u9519\u4e86  \u8fd9\u91cc\u662f\u83b7\u53d6\u591a\u4e2a\u5b57\u6bb5\u7684\u503c\n    public function hMget($key,$field)\n    {\n        if(!is_array($field))\n            $field=explode(&#039;,&#039;, $field);\n        return $this-&gt;redis-&gt;hMget($key,$field);\n    }\n\n    \/**\n     * \u4e3ahash\u8868\u8bbe\u8fd9\u7d2f\u52a0\uff0c\u53ef\u4ee5\u8d1f\u6570\n     * @param string $key\n     * @param int $field\n     * @param string $value\n     * @return bool\n     *\/\n    public function hIncrBy($key,$field,$value)\n    {\n        $value=intval($value);\n        return $this-&gt;redis-&gt;hIncrBy($key,$field,$value);\n    }\n\n    \/**\n     * \u8fd4\u56de\u6240\u6709hash\u8868\u7684\u6240\u6709\u5b57\u6bb5\n     * @param string $key\n     * @return array|bool\n     *\/\n    \/\/\u8fd9\u91cc\u662f\u83b7\u53d6\u5f53\u524dkey\u4e0b \u6240\u6709field\u5b57\u6bb5\n    public function hKeys($key)\n    {\n        return $this-&gt;redis-&gt;hKeys($key);\n    }\n\n    \/**\n     * \u8fd4\u56de\u6240\u6709hash\u8868\u7684\u5b57\u6bb5\u503c\uff0c\u4e3a\u4e00\u4e2a\u7d22\u5f15\u6570\u7ec4\n     * @param string $key\n     * @return array|bool\n     *\/\n    \/\/\u83b7\u53d6\u5f53\u524dkey\u4e0b\u6240\u6709\u7684value\n    public function hVals($key)\n    {\n        return $this-&gt;redis-&gt;hVals($key);\n    }\n\n    \/**\n     * \u8fd4\u56de\u6240\u6709hash\u8868\u7684\u5b57\u6bb5\u503c\uff0c\u4e3a\u4e00\u4e2a\u5173\u8054\u6570\u7ec4\n     * @param string $key\n     * @return array|bool\n     *\/\n    \/\/\u83b7\u53d6\u5f53\u524dkey\u4e0b \u6240\u6709\u7684 field\u548c value\n    public function hGetAll($key)\n    {\n        return $this-&gt;redis-&gt;hGetAll($key);\n    }\n\n    \/*********************\u6709\u5e8f\u96c6\u5408\u64cd\u4f5c*********************\/\n\n    \/**\n     * \u7ed9\u5f53\u524d\u96c6\u5408\u6dfb\u52a0\u4e00\u4e2a\u5143\u7d20\n     * \u5982\u679cvalue\u5df2\u7ecf\u5b58\u5728\uff0c\u4f1a\u66f4\u65b0order\u7684\u503c\u3002\n     * @param string $key\n     * @param string $order \u5e8f\u53f7\n     * @param string $value \u503c\n     * @return bool\n     *\/\n    public function zAdd($key,$order,$value)\n    {\n        return $this-&gt;redis-&gt;zAdd($key,$order,$value);   \n    }\n\n    \/**\n     * \u7ed9$value\u6210\u5458\u7684order\u503c\uff0c\u589e\u52a0$num,\u53ef\u4ee5\u4e3a\u8d1f\u6570\n     * @param string $key\n     * @param string $num \u5e8f\u53f7\n     * @param string $value \u503c\n     * @return \u8fd4\u56de\u65b0\u7684order\n     *\/\n    public function zinCry($key,$num,$value)\n    {\n        return $this-&gt;redis-&gt;zinCry($key,$num,$value);\n    }\n\n    \/**\n     * \u5220\u9664\u503c\u4e3avalue\u7684\u5143\u7d20\n     * @param string $key\n     * @param stirng $value\n     * @return bool\n     *\/\n    public function zRem($key,$value)\n    {\n        return $this-&gt;redis-&gt;zRem($key,$value);\n    }\n\n    \/**\n     * \u96c6\u5408\u4ee5order\u9012\u589e\u6392\u5217\u540e\uff0c0\u8868\u793a\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c-1\u8868\u793a\u6700\u540e\u4e00\u4e2a\u5143\u7d20\n     * @param string $key\n     * @param int $start\n     * @param int $end\n     * @return array|bool\n     *\/\n    public function zRange($key,$start,$end)\n    {\n        return $this-&gt;redis-&gt;zRange($key,$start,$end);\n    }\n\n    \/**\n     * \u96c6\u5408\u4ee5order\u9012\u51cf\u6392\u5217\u540e\uff0c0\u8868\u793a\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c-1\u8868\u793a\u6700\u540e\u4e00\u4e2a\u5143\u7d20\n     * @param string $key\n     * @param int $start\n     * @param int $end\n     * @return array|bool\n     *\/\n    public function zRevRange($key,$start,$end)\n    {\n        return $this-&gt;redis-&gt;zRevRange($key,$start,$end);\n    }\n\n    \/**\n     * \u96c6\u5408\u4ee5order\u9012\u589e\u6392\u5217\u540e\uff0c\u8fd4\u56de\u6307\u5b9aorder\u4e4b\u95f4\u7684\u5143\u7d20\u3002\n     * min\u548cmax\u53ef\u4ee5\u662f-inf\u548c+inf\u3000\u8868\u793a\u6700\u5927\u503c\uff0c\u6700\u5c0f\u503c\n     * @param string $key\n     * @param int $start\n     * @param int $end\n     * @package array $option \u53c2\u6570\n     *     withscores=&gt;true\uff0c\u8868\u793a\u6570\u7ec4\u4e0b\u6807\u4e3aOrder\u503c\uff0c\u9ed8\u8ba4\u8fd4\u56de\u7d22\u5f15\u6570\u7ec4\n     *     limit=&gt;array(0,1) \u8868\u793a\u4ece0\u5f00\u59cb\uff0c\u53d6\u4e00\u6761\u8bb0\u5f55\u3002\n     * @return array|bool\n     *\/\n    public function zRangeByScore($key,$start=&#039;-inf&#039;,$end=&quot;+inf&quot;,$option=array())\n    {\n        return $this-&gt;redis-&gt;zRangeByScore($key,$start,$end,$option);\n    }\n\n    \/**\n     * \u96c6\u5408\u4ee5order\u9012\u51cf\u6392\u5217\u540e\uff0c\u8fd4\u56de\u6307\u5b9aorder\u4e4b\u95f4\u7684\u5143\u7d20\u3002\n     * min\u548cmax\u53ef\u4ee5\u662f-inf\u548c+inf\u3000\u8868\u793a\u6700\u5927\u503c\uff0c\u6700\u5c0f\u503c\n     * @param string $key\n     * @param int $start\n     * @param int $end\n     * @package array $option \u53c2\u6570\n     *     withscores=&gt;true\uff0c\u8868\u793a\u6570\u7ec4\u4e0b\u6807\u4e3aOrder\u503c\uff0c\u9ed8\u8ba4\u8fd4\u56de\u7d22\u5f15\u6570\u7ec4\n     *     limit=&gt;array(0,1) \u8868\u793a\u4ece0\u5f00\u59cb\uff0c\u53d6\u4e00\u6761\u8bb0\u5f55\u3002\n     * @return array|bool\n     *\/\n    public function zRevRangeByScore($key,$start=&#039;-inf&#039;,$end=&quot;+inf&quot;,$option=array())\n    {\n        return $this-&gt;redis-&gt;zRevRangeByScore($key,$start,$end,$option);\n    }\n\n    \/**\n     * \u8fd4\u56deorder\u503c\u5728start end\u4e4b\u95f4\u7684\u6570\u91cf\n     * @param unknown $key\n     * @param unknown $start\n     * @param unknown $end\n     *\/\n    public function zCount($key,$start,$end)\n    {\n        return $this-&gt;redis-&gt;zCount($key,$start,$end);\n    }\n\n    \/**\n     * \u8fd4\u56de\u503c\u4e3avalue\u7684order\u503c\n     * @param unknown $key\n     * @param unknown $value\n     *\/\n    public function zScore($key,$value)\n    {\n        return $this-&gt;redis-&gt;zScore($key,$value);\n    }\n\n    \/**\n     * \u8fd4\u56de\u96c6\u5408\u4ee5score\u9012\u589e\u52a0\u6392\u5e8f\u540e\uff0c\u6307\u5b9a\u6210\u5458\u7684\u6392\u5e8f\u53f7\uff0c\u4ece0\u5f00\u59cb\u3002\n     * @param unknown $key\n     * @param unknown $value\n     *\/\n    public function zRank($key,$value)\n    {\n        return $this-&gt;redis-&gt;zRank($key,$value);\n    }\n\n    \/**\n     * \u8fd4\u56de\u96c6\u5408\u4ee5score\u9012\u589e\u52a0\u6392\u5e8f\u540e\uff0c\u6307\u5b9a\u6210\u5458\u7684\u6392\u5e8f\u53f7\uff0c\u4ece0\u5f00\u59cb\u3002\n     * @param unknown $key\n     * @param unknown $value\n     *\/\n    public function zRevRank($key,$value)\n    {\n        return $this-&gt;redis-&gt;zRevRank($key,$value);\n    }\n\n    \/**\n     * \u5220\u9664\u96c6\u5408\u4e2d\uff0cscore\u503c\u5728start end\u4e4b\u95f4\u7684\u5143\u7d20\u3000\u5305\u62ecstart end\n     * min\u548cmax\u53ef\u4ee5\u662f-inf\u548c+inf\u3000\u8868\u793a\u6700\u5927\u503c\uff0c\u6700\u5c0f\u503c\n     * @param unknown $key\n     * @param unknown $start\n     * @param unknown $end\n     * @return \u5220\u9664\u6210\u5458\u7684\u6570\u91cf\u3002\n     *\/\n    public function zRemRangeByScore($key,$start,$end)\n    {\n        return $this-&gt;redis-&gt;zRemRangeByScore($key,$start,$end);\n    }\n\n    \/**\n     * \u8fd4\u56de\u96c6\u5408\u5143\u7d20\u4e2a\u6570\u3002\n     * @param unknown $key\n     *\/\n    public function zCard($key)\n    {\n        return $this-&gt;redis-&gt;zCard($key);\n    }\n    \/*********************\u961f\u5217\u64cd\u4f5c\u547d\u4ee4************************\/\n\n    \/**\n     * \u5728\u961f\u5217\u5c3e\u90e8\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\n     * @param unknown $key\n     * @param unknown $value\n     * \u8fd4\u56de\u961f\u5217\u957f\u5ea6\n     *\/\n    public function rPush($key,$value)\n    {\n        return $this-&gt;redis-&gt;rPush($key,$value); \n    }\n\n    \/**\n     * \u5728\u961f\u5217\u5c3e\u90e8\u63d2\u5165\u4e00\u4e2a\u5143\u7d20 \u5982\u679ckey\u4e0d\u5b58\u5728\uff0c\u4ec0\u4e48\u4e5f\u4e0d\u505a\n     * @param unknown $key\n     * @param unknown $value\n     * \u8fd4\u56de\u961f\u5217\u957f\u5ea6\n     *\/\n    public function rPushx($key,$value)\n    {\n        return $this-&gt;redis-&gt;rPushx($key,$value);\n    }\n\n    \/**\n     * \u5728\u961f\u5217\u5934\u90e8\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\n     * @param unknown $key\n     * @param unknown $value\n     * \u8fd4\u56de\u961f\u5217\u957f\u5ea6\n     *\/\n    public function lPush($key,$value)\n    {\n        return $this-&gt;redis-&gt;lPush($key,$value);\n    }\n\n    \/**\n     * \u5728\u961f\u5217\u5934\u63d2\u5165\u4e00\u4e2a\u5143\u7d20 \u5982\u679ckey\u4e0d\u5b58\u5728\uff0c\u4ec0\u4e48\u4e5f\u4e0d\u505a\n     * @param unknown $key\n     * @param unknown $value\n     * \u8fd4\u56de\u961f\u5217\u957f\u5ea6\n     *\/\n    public function lPushx($key,$value)\n    {\n        return $this-&gt;redis-&gt;lPushx($key,$value);\n    }\n\n    \/**\n     * \u8fd4\u56de\u961f\u5217\u957f\u5ea6\n     * @param unknown $key\n     *\/\n    public function lLen($key)\n    {\n        return $this-&gt;redis-&gt;lLen($key); \n    }\n\n    \/**\n     * \u8fd4\u56de\u961f\u5217\u6307\u5b9a\u533a\u95f4\u7684\u5143\u7d20\n     * @param unknown $key\n     * @param unknown $start\n     * @param unknown $end\n     *\/\n    public function lRange($key,$start,$end)\n    {\n        return $this-&gt;redis-&gt;lrange($key,$start,$end);\n    }\n\n    \/**\n     * \u8fd4\u56de\u961f\u5217\u4e2d\u6307\u5b9a\u7d22\u5f15\u7684\u5143\u7d20\n     * @param unknown $key\n     * @param unknown $index\n     *\/\n    public function lIndex($key,$index)\n    {\n        return $this-&gt;redis-&gt;lIndex($key,$index);\n    }\n\n    \/**\n     * \u8bbe\u5b9a\u961f\u5217\u4e2d\u6307\u5b9aindex\u7684\u503c\u3002\n     * @param unknown $key\n     * @param unknown $index\n     * @param unknown $value\n     *\/\n    public function lSet($key,$index,$value)\n    {\n        return $this-&gt;redis-&gt;lSet($key,$index,$value);\n    }\n\n    \/**\n     * \u5220\u9664\u503c\u4e3avaule\u7684count\u4e2a\u5143\u7d20\n     * PHP-REDIS\u6269\u5c55\u7684\u6570\u636e\u987a\u5e8f\u4e0e\u547d\u4ee4\u7684\u987a\u5e8f\u4e0d\u592a\u4e00\u6837\uff0c\u4e0d\u77e5\u9053\u662f\u4e0d\u662fbug\n     * count&gt;0 \u4ece\u5c3e\u90e8\u5f00\u59cb\n     *  &gt;0\u3000\u4ece\u5934\u90e8\u5f00\u59cb\n     *  =0\u3000\u5220\u9664\u5168\u90e8\n     * @param unknown $key\n     * @param unknown $count\n     * @param unknown $value\n     *\/\n    public function lRem($key,$count,$value)\n    {\n        return $this-&gt;redis-&gt;lRem($key,$value,$count);\n    }\n\n    \/**\n     * \u5220\u9664\u5e76\u8fd4\u56de\u961f\u5217\u4e2d\u7684\u5934\u5143\u7d20\u3002\n     * @param unknown $key\n     *\/\n    public function lPop($key)\n    {\n        return $this-&gt;redis-&gt;lPop($key);\n    }\n\n    \/**\n     * \u5220\u9664\u5e76\u8fd4\u56de\u961f\u5217\u4e2d\u7684\u5c3e\u5143\u7d20\n     * @param unknown $key\n     *\/\n    public function rPop($key)\n    {\n        return $this-&gt;redis-&gt;rPop($key);\n    }\n\n    \/*************redis\u5b57\u7b26\u4e32\u64cd\u4f5c\u547d\u4ee4*****************\/\n\n    \/**\n     * \u8bbe\u7f6e\u4e00\u4e2akey\n     * @param unknown $key\n     * @param unknown $value\n     *\/\n    public function set($key,$value)\n    {\n        return $this-&gt;redis-&gt;set($key,$value);\n    }\n\n    \/**\n     * \u5f97\u5230\u4e00\u4e2akey\n     * @param unknown $key\n     *\/\n    public function get($key)\n    {\n        return $this-&gt;redis-&gt;get($key);\n    }\n\n    \/**\n     * \u8bbe\u7f6e\u4e00\u4e2a\u6709\u8fc7\u671f\u65f6\u95f4\u7684key\n     * @param unknown $key\n     * @param unknown $expire\n     * @param unknown $value\n     *\/\n    public function setex($key,$expire,$value)\n    {\n        return $this-&gt;redis-&gt;setex($key,$expire,$value);\n    }\n\n    \/**\n     * \u8bbe\u7f6e\u4e00\u4e2akey,\u5982\u679ckey\u5b58\u5728,\u4e0d\u505a\u4efb\u4f55\u64cd\u4f5c.\n     * @param unknown $key\n     * @param unknown $value\n     *\/\n    public function setnx($key,$value)\n    {\n        return $this-&gt;redis-&gt;setnx($key,$value);\n    }\n\n    \/**\n     * \u6279\u91cf\u8bbe\u7f6ekey\n     * @param unknown $arr\n     *\/\n    public function mset($arr)\n    {\n        return $this-&gt;redis-&gt;mset($arr);\n    }\n\n    \/*************redis\u3000\u65e0\u5e8f\u96c6\u5408\u64cd\u4f5c\u547d\u4ee4*****************\/\n\n    \/**\n     * \u8fd4\u56de\u96c6\u5408\u4e2d\u6240\u6709\u5143\u7d20\n     * @param unknown $key\n     *\/\n    public function sMembers($key)\n    {\n        return $this-&gt;redis-&gt;sMembers($key);\n    }\n\n    \/**\n     * \u6c422\u4e2a\u96c6\u5408\u7684\u5dee\u96c6\n     * @param unknown $key1\n     * @param unknown $key2\n     *\/\n    public function sDiff($key1,$key2)\n    {\n        return $this-&gt;redis-&gt;sDiff($key1,$key2);\n    }\n\n    \/**\n     * \u6dfb\u52a0\u96c6\u5408\u3002\u7531\u4e8e\u7248\u672c\u95ee\u9898\uff0c\u6269\u5c55\u4e0d\u652f\u6301\u6279\u91cf\u6dfb\u52a0\u3002\u8fd9\u91cc\u505a\u4e86\u5c01\u88c5\n     * @param unknown $key\n     * @param string|array $value\n     *\/\n    public function sAdd($key,$value)\n    {\n        if(!is_array($value))\n            $arr=array($value);\n        else\n            $arr=$value;\n        foreach($arr as $row)\n            $this-&gt;redis-&gt;sAdd($key,$row);\n    }\n\n    \/**\n     * \u8fd4\u56de\u65e0\u5e8f\u96c6\u5408\u7684\u5143\u7d20\u4e2a\u6570\n     * @param unknown $key\n     *\/\n    public function scard($key)\n    {\n        return $this-&gt;redis-&gt;scard($key);\n    }\n\n    \/**\n     * \u4ece\u96c6\u5408\u4e2d\u5220\u9664\u4e00\u4e2a\u5143\u7d20\n     * @param unknown $key\n     * @param unknown $value\n     *\/\n    public function srem($key,$value)\n    {\n        return $this-&gt;redis-&gt;srem($key,$value);\n    }\n\n    \/*************redis\u7ba1\u7406\u64cd\u4f5c\u547d\u4ee4*****************\/\n\n    \/**\n     * \u9009\u62e9\u6570\u636e\u5e93\n     * @param int $dbId \u6570\u636e\u5e93ID\u53f7\n     * @return bool\n     *\/\n    public function select($dbId)\n    {\n        $this-&gt;dbId=$dbId;\n        return $this-&gt;redis-&gt;select($dbId);\n    }\n\n    \/**\n     * \u6e05\u7a7a\u5f53\u524d\u6570\u636e\u5e93\n     * @return bool\n     *\/\n    public function flushDB()\n    {\n        return $this-&gt;redis-&gt;flushDB();\n    }\n\n    \/**\n     * \u8fd4\u56de\u5f53\u524d\u5e93\u72b6\u6001\n     * @return array\n     *\/\n    public function info()\n    {\n        return $this-&gt;redis-&gt;info();\n    }\n\n    \/**\n     * \u540c\u6b65\u4fdd\u5b58\u6570\u636e\u5230\u78c1\u76d8\n     *\/\n    public function save()\n    {\n        return $this-&gt;redis-&gt;save();\n    }\n\n    \/**\n     * \u5f02\u6b65\u4fdd\u5b58\u6570\u636e\u5230\u78c1\u76d8\n     *\/\n    public function bgSave()\n    {\n        return $this-&gt;redis-&gt;bgSave();\n    }\n\n    \/**\n     * \u8fd4\u56de\u6700\u540e\u4fdd\u5b58\u5230\u78c1\u76d8\u7684\u65f6\u95f4\n     *\/\n    public function lastSave()\n    {\n        return $this-&gt;redis-&gt;lastSave();\n    }\n\n    \/**\n     * \u8fd4\u56dekey,\u652f\u6301*\u591a\u4e2a\u5b57\u7b26\uff0c?\u4e00\u4e2a\u5b57\u7b26\n     * \u53ea\u6709*\u3000\u8868\u793a\u5168\u90e8\n     * @param string $key\n     * @return array\n     *\/\n    public function keys($key)\n    {\n        return $this-&gt;redis-&gt;keys($key);\n    }\n\n    \/**\n     * \u5220\u9664\u6307\u5b9akey\n     * @param unknown $key\n     *\/\n    public function del($key)\n    {\n        return $this-&gt;redis-&gt;del($key);\n    }\n\n    \/**\n     * \u5224\u65ad\u4e00\u4e2akey\u503c\u662f\u4e0d\u662f\u5b58\u5728\n     * @param unknown $key\n     *\/\n    public function exists($key)\n    {\n        return $this-&gt;redis-&gt;exists($key);\n    }\n\n    \/**\n     * \u4e3a\u4e00\u4e2akey\u8bbe\u5b9a\u8fc7\u671f\u65f6\u95f4 \u5355\u4f4d\u4e3a\u79d2\n     * @param unknown $key\n     * @param unknown $expire\n     *\/\n    public function expire($key,$expire)\n    {\n        return $this-&gt;redis-&gt;expire($key,$expire);\n    }\n\n    \/**\n     * \u8fd4\u56de\u4e00\u4e2akey\u8fd8\u6709\u591a\u4e45\u8fc7\u671f\uff0c\u5355\u4f4d\u79d2\n     * @param unknown $key\n     *\/\n    public function ttl($key)\n    {\n        return $this-&gt;redis-&gt;ttl($key);\n    }\n\n    \/**\n     * \u8bbe\u5b9a\u4e00\u4e2akey\u4ec0\u4e48\u65f6\u5019\u8fc7\u671f\uff0ctime\u4e3a\u4e00\u4e2a\u65f6\u95f4\u6233\n     * @param unknown $key\n     * @param unknown $time\n     *\/\n    public function exprieAt($key,$time)\n    {\n        return $this-&gt;redis-&gt;expireAt($key,$time);\n    }\n\n    \/**\n     * \u5173\u95ed\u670d\u52a1\u5668\u94fe\u63a5\n     *\/\n    public function close()\n    {\n        return $this-&gt;redis-&gt;close();\n    }\n\n    \/**\n     * \u5173\u95ed\u6240\u6709\u8fde\u63a5\n     *\/\n    public static function closeAll()\n    {\n        foreach(static::$_instance as $o)\n        {\n            if($o instanceof self)\n                $o-&gt;close();\n        }\n    }\n\n    \/** \u8fd9\u91cc\u4e0d\u5173\u95ed\u8fde\u63a5\uff0c\u56e0\u4e3asession\u5199\u5165\u4f1a\u5728\u6240\u6709\u5bf9\u8c61\u9500\u6bc1\u4e4b\u540e\u3002\n    public function __destruct()\n    {\n        return $this-&gt;redis-&gt;close();\n    }\n    **\/\n    \/**\n     * \u8fd4\u56de\u5f53\u524d\u6570\u636e\u5e93key\u6570\u91cf\n     *\/\n    public function dbSize()\n    {\n        return $this-&gt;redis-&gt;dbSize();\n    }\n\n    \/**\n     * \u8fd4\u56de\u4e00\u4e2a\u968f\u673akey\n     *\/\n    public function randomKey()\n    {\n        return $this-&gt;redis-&gt;randomKey();\n    }\n\n    \/**\n     * \u5f97\u5230\u5f53\u524d\u6570\u636e\u5e93ID\n     * @return int\n     *\/\n    public function getDbId()\n    {\n        return $this-&gt;dbId;\n    }\n\n    \/**\n     * \u8fd4\u56de\u5f53\u524d\u5bc6\u7801\n     *\/\n    public function getAuth()\n    {\n        return $this-&gt;auth;\n    }\n\n    public function getHost()\n    {\n        return $this-&gt;host;\n    }\n\n    public function getPort()\n    {\n        return $this-&gt;port;\n    }\n\n    public function getConnInfo()\n    {\n        return array(\n            &#039;host&#039;=&gt;$this-&gt;host,\n            &#039;port&#039;=&gt;$this-&gt;port,\n            &#039;auth&#039;=&gt;$this-&gt;auth\n        );\n    }\n    \/*********************\u4e8b\u52a1\u7684\u76f8\u5173\u65b9\u6cd5************************\/\n\n    \/**\n     * \u76d1\u63a7key,\u5c31\u662f\u4e00\u4e2a\u6216\u591a\u4e2akey\u6dfb\u52a0\u4e00\u4e2a\u4e50\u89c2\u9501\n     * \u5728\u6b64\u671f\u95f4\u5982\u679ckey\u7684\u503c\u5982\u679c\u53d1\u751f\u7684\u6539\u53d8\uff0c\u521a\u4e0d\u80fd\u4e3akey\u8bbe\u5b9a\u503c\n     * \u53ef\u4ee5\u91cd\u65b0\u53d6\u5f97Key\u7684\u503c\u3002\n     * @param unknown $key\n     *\/\n    public function watch($key)\n    {\n        return $this-&gt;redis-&gt;watch($key);\n    }\n\n    \/**\n     * \u53d6\u6d88\u5f53\u524d\u94fe\u63a5\u5bf9\u6240\u6709key\u7684watch\n     *  EXEC \u547d\u4ee4\u6216 DISCARD \u547d\u4ee4\u5148\u88ab\u6267\u884c\u4e86\u7684\u8bdd\uff0c\u90a3\u4e48\u5c31\u4e0d\u9700\u8981\u518d\u6267\u884c UNWATCH \u4e86\n     *\/\n    public function unwatch()\n    {\n        return $this-&gt;redis-&gt;unwatch();\n    }\n\n    \/**\n     * \u5f00\u542f\u4e00\u4e2a\u4e8b\u52a1\n     * \u4e8b\u52a1\u7684\u8c03\u7528\u6709\u4e24\u79cd\u6a21\u5f0fRedis::MULTI\u548cRedis::PIPELINE\uff0c\n     * \u9ed8\u8ba4\u662fRedis::MULTI\u6a21\u5f0f\uff0c\n     * Redis::PIPELINE\u7ba1\u9053\u6a21\u5f0f\u901f\u5ea6\u66f4\u5feb\uff0c\u4f46\u6ca1\u6709\u4efb\u4f55\u4fdd\u8bc1\u539f\u5b50\u6027\u6709\u53ef\u80fd\u9020\u6210\u6570\u636e\u7684\u4e22\u5931\n     *\/\n    public function multi($type=\\Redis::MULTI)\n    {\n        return $this-&gt;redis-&gt;multi($type);\n    }\n\n    \/**\n     * \u6267\u884c\u4e00\u4e2a\u4e8b\u52a1\n     * \u6536\u5230 EXEC \u547d\u4ee4\u540e\u8fdb\u5165\u4e8b\u52a1\u6267\u884c\uff0c\u4e8b\u52a1\u4e2d\u4efb\u610f\u547d\u4ee4\u6267\u884c\u5931\u8d25\uff0c\u5176\u4f59\u7684\u547d\u4ee4\u4f9d\u7136\u88ab\u6267\u884c\n     *\/\n    public function exec()\n    {\n        return $this-&gt;redis-&gt;exec();\n    }\n\n    \/**\n     * \u56de\u6eda\u4e00\u4e2a\u4e8b\u52a1\n     *\/\n    public function discard()\n    {\n        return $this-&gt;redis-&gt;discard();\n    }\n\n    \/**\n     * \u6d4b\u8bd5\u5f53\u524d\u94fe\u63a5\u662f\u4e0d\u662f\u5df2\u7ecf\u5931\u6548\n     * \u6ca1\u6709\u5931\u6548\u8fd4\u56de+PONG\n     * \u5931\u6548\u8fd4\u56defalse\n     *\/\n    public function ping()\n    {\n        return $this-&gt;redis-&gt;ping();\n    }\n\n    public function auth($auth)\n    {\n        return $this-&gt;redis-&gt;auth($auth);\n    }\n    \/*********************\u81ea\u5b9a\u4e49\u7684\u65b9\u6cd5,\u7528\u4e8e\u7b80\u5316\u64cd\u4f5c************************\/\n\n    \/**\n     * \u5f97\u5230\u4e00\u7ec4\u7684ID\u53f7\n     * @param unknown $prefix\n     * @param unknown $ids\n     *\/\n    public function hashAll($prefix,$ids)\n    {\n        if($ids==false)\n            return false;\n        if(is_string($ids))\n            $ids=explode(&#039;,&#039;, $ids);\n        $arr=array();\n        foreach($ids as $id)\n        {\n            $key=$prefix.&#039;.&#039;.$id;\n            $res=$this-&gt;hGetAll($key);\n            if($res!=false)\n                $arr[]=$res;\n        }\n\n        return $arr;\n    }\n\n    \/**\n     * \u751f\u6210\u4e00\u6761\u6d88\u606f\uff0c\u653e\u5728redis\u6570\u636e\u5e93\u4e2d\u3002\u4f7f\u75280\u53f7\u5e93\u3002\n     * @param string|array $msg\n     *\/\n    public function pushMessage($lkey,$msg)\n    {\n        if(is_array($msg)){\n            $msg    =    json_encode($msg);\n        }\n        $key    =    md5($msg);\n\n        \/\/\u5982\u679c\u6d88\u606f\u5df2\u7ecf\u5b58\u5728\uff0c\u5220\u9664\u65e7\u6d88\u606f\uff0c\u5df2\u5f53\u524d\u6d88\u606f\u4e3a\u51c6\n        \/\/echo $n=$this-&gt;lRem($lkey, 0, $key).&quot;\\n&quot;;\n        \/\/\u91cd\u65b0\u8bbe\u7f6e\u65b0\u6d88\u606f\n        $this-&gt;lPush($lkey, $key);\n        $this-&gt;setex($key, 3600, $msg);\n        return $key;\n    }\n\n    \/**\n     * \u5f97\u5230\u6761\u6279\u91cf\u5220\u9664key\u7684\u547d\u4ee4\n     * @param unknown $keys\n     * @param unknown $dbId\n     *\/\n    public function delKeys($keys,$dbId)\n    {\n        $redisInfo=$this-&gt;getConnInfo();\n        $cmdArr=array(\n            &#039;redis-cli&#039;,\n            &#039;-a&#039;,\n            $redisInfo[&#039;auth&#039;],\n            &#039;-h&#039;,\n            $redisInfo[&#039;host&#039;],\n            &#039;-p&#039;,\n            $redisInfo[&#039;port&#039;],\n            &#039;-n&#039;,\n            $dbId,\n        );\n        $redisStr=implode(&#039; &#039;, $cmdArr);\n        $cmd=&quot;{$redisStr} KEYS \\&quot;{$keys}\\&quot; | xargs {$redisStr} del&quot;;\n        return $cmd;\n    }\n}<\/code><\/pre>\n<h5>\u4f7f\u7528\u65b9\u6cd5<\/h5>\n<pre><code class=\"language-php\">require_once(&#039;.\/RedisTool.class.php&#039;);\n\n\/\/\u914d\u7f6e\u4fe1\u606f\n$redisConfig = [\n            &#039;port&#039; =&gt; &#039;6379&#039;,    \n            &#039;host&#039; =&gt; &#039;127.0.0.1&#039;,   \n            &#039;auth&#039; =&gt; &#039;123456&#039;,  \n               ];\n\n\/\/\u83b7\u53d6\u5bf9\u8c61\n$rt = RedisTool :: getInstance($redisConfig, 0);\n\n$user1 = [\n        &#039;id&#039;          =&gt; 1,\n        &#039;username&#039;        =&gt; &#039;alice&#039;,\n        &#039;age&#039;             =&gt; &#039;18&#039;,\n        ];\n\n$user2 = [\n        &#039;id&#039;          =&gt; 2,\n        &#039;username&#039;        =&gt; &#039;bob&#039;,\n        &#039;age&#039;             =&gt; &#039;19&#039;,\n        ];\n\n\/\/\u6dfb\u52a0\u6570\u636e\u5e76\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4       hMset \u4e5f\u53ef\u4ee5\u7528\u6765\u4fee\u6539\n$rt -&gt; hMset(&#039;user1&#039;, $user1);\n$rt -&gt; expire(&#039;user1&#039;, 1800);\n\n$rt -&gt; hMset(&#039;user2&#039;, $user2);\n$rt -&gt; expire(&#039;user2&#039;, 1800);\n\n\/\/\u83b7\u53d6\u6240\u6709key \u4e5f\u53ef\u4ee5\u7528\u4e8e\u6a21\u7cca\u5339\u914d\n$res = $rt -&gt; keys(&#039;*&#039;);\nvar_dump($res);\n\n\/\/\u5220\u9664\u6307\u5b9akey \u8fd9\u6761\u6570\u636e\n$rt -&gt; del(&#039;user1&#039;);\n\n\/\/\u6307\u5b9a key field \u6765\u4fee\u6539value  \u4e00\u6b21\u53ea\u4fee\u6539\u4e00\u4e2avalue  \u4e5f\u53ef\u4ee5\u7528\u4e8e\u6dfb\u52a0\n$rt -&gt; hSet(&#039;user2&#039;, &#039;age&#039;, 21);\n$rt -&gt; expire(&#039;user2&#039;, 1800);\n\n\/\/\u4f7f\u7528key field \u6765\u67e5\u8be2value\n$rt -&gt; hGet(&#039;user2&#039;, &#039;age&#039;);<\/code><\/pre>\n<p>\u53c2\u8003\u8d44\u6599 <a href=\"https:\/\/www.kancloud.cn\/a173512\/php_note\/1352142\">https:\/\/www.kancloud.cn\/a173512\/php_note\/1352142<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&lt;?php \/** * redis\u64cd\u4f5c\u7c7b * \u8bf4\u660e\uff0c\u4efb\u4f55\u4e3afalse\u7684\u4e32\uff0c\u5b58\u5728redis\u4e2d\u90fd\u662f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-244","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/posts\/244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=244"}],"version-history":[{"count":1,"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/posts\/244\/revisions"}],"predecessor-version":[{"id":245,"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/posts\/244\/revisions\/245"}],"wp:attachment":[{"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}