<?php
namespace app\API\server;
class RedisInstance
{
private static $_instance = null;
private function __construct(){
self::$_instance = new \Redis();
self::$_instance->connect('127.0.0.1','6379','5');
}
private function __clone(){}
public static function getInstance(){
if(!self::$_instance){
new self;
}
return self::$_instance;
}
public static function setKeyValueArray($redisKey, $obj)
{
if (empty($obj) || count($obj) == 0) {
return false;
}
return self::getInstance()->hMSet($redisKey, $obj);
}
public static function setString($key, $value)
{
return self::getInstance()->set($key,$value);
}
public static function getString($key)
{
return self::getInstance()->get($key);
}
public static function getKeyValueArray($key)
{
$obj = array();
if(empty($key)){
return $obj;
}
if(empty($key)){$key="";}
$fields = self::getInstance()->hkeys($key);
if (!empty($fields) && count($fields) > 0)
{
$obj = self::getInstance()->hmget($key, $fields);
}
return $obj;
}
public static function getHmget($key,$fields)
{
$obj = self::getInstance()->hmget($key, $fields);
return $obj;
}
public static function setRedisSet($setName, $obj)
{
if (empty($obj) || count($obj) == 0)
r
Redis 单例模式 封装
最新推荐文章于 2024-08-06 16:08:28 发布
本文探讨如何在 Java 和 PHP 中实现 Redis 连接的单例模式封装,旨在优化资源利用,提高应用效率。通过单例模式确保 Redis 客户端在应用程序中只实例化一次,避免了重复连接和资源浪费。
摘要由CSDN通过智能技术生成