<?php
/**
* Created by PhpStorm.
* User:
* Date: 2021/6/23
* Time: 15:42
*/
class Rediscurd
{
//
private static $redis;
//redis连接
private $conn;
//私有构造函数
private function __construct()
{
//连接本地redis
$host = "127.0.0.1";
$port = "6379";
$this->conn = new Redis();
$this->conn -> connect($host,$port);
}
//私有克隆方式
private function __clone()
{
// TODO: Implement __clone() method.
}
//单一获取对象入口
public static function get_instance(){
//若部位该类对象则实例化对象,若不是则返回已实例化对象
if (!self::$redis instanceof self){
self::$redis = new self;
}
return self::$redis;
}
/*
* 设置redis
* value redis设置的值
* expire redis过期时间
*
*/
public function set_redis($key,$value,$expire=0){
if (is_array($value)){
$value = json_encode($value);
}
return $expire ? $this->conn->set($key,$value,$expire): $this->conn->set($key,$value);
}
/*
* 获取redis
* key redis的键
* return array|int|string
*/
public function get_redis($key){
$data = $this->conn->get($key);
return json_decode($data) ? json_decode($data) : $data;
}
/*
* 移除redis
* key redis的键
*/
public function del_redis($key){
return $this->conn->del($key);
}
/*
* php list操作
* lpush操作
*/
public function lpush($list,$value){
$value = is_array($value) ? json_encode($value) : $value;
return $this->conn->lPush($list,$value);
}
/*
* rpush操作
*/
public function rpush($list,$value){
$value = is_array($value) ? json_encode($value) : $value;
return $this->conn->rPush($list,$value);
}
/*
* lrange操作
*/
public function lrange($key,$start,$end){
$res = $this->conn->lRange($key,$start,$end);
foreach ($res as & $k){
if (json_decode($k)){
$k = json_decode($k);
}
}
return $res;
}
}
php单例模式简易封装redis
最新推荐文章于 2023-09-07 11:55:01 发布