封装一个php+redis的操作类

为什么要封装redis方法?

封装Redis方法有以下几个原因:

  1. 简化代码:封装Redis方法可以将复杂的Redis操作封装成一个简洁的方法,提高代码的可读性和可维护性。

  2. 提高复用性:通过封装Redis方法,可以将相同的Redis操作封装成一个函数或类,方便在不同的项目中复用,提高开发效率。

  3. 隐藏实现细节:封装Redis方法可以隐藏Redis的具体实现细节,只暴露需要的方法,降低和Redis的耦合度,方便后续更换或升级Redis。

  4. 安全性:通过封装Redis方法,可以对访问Redis的权限进行控制,只允许通过封装的方法进行操作,提高数据的安全性。

  5. 性能优化:封装Redis方法可以对一些常用的操作进行优化,例如添加缓存处理、批量操作等,提高系统的性能和响应速度。

总之,封装Redis方法可以提供更好的抽象和封装,简化代码,提高复用性和安全性,优化性能,使得开发更加方便和高效。

以下就是我自己封装php操作redis的类(方法还是比较全的,可以根据自己的需求调用来封装)

REDIS封装
<?php

class RedisWrapper {
    private $redis;

    public function __construct($host = '127.0.0.1', $port = 6379, $password = '', $database = 0) {
        $this->redis = new Redis();
        try {
            $this->redis->connect($host, $port);
            if ($password) {
                $this->redis->auth($password);
            }
            $this->redis->select($database);
        } catch (Exception $e) {
            error_log('Redis connection failed: ' . $e->getMessage());
            throw $e; // Rethrow the exception after logging
        }
    }

    //设置键值对,可以选择性地设置过期时间
    public function set($key, $value, $ttl = 0) {
        try {
            if ($ttl > 0) {
                return $this->redis->setex($key, $ttl, $value);
            } else {
                return $this->redis->set($key, $value);
            }
        } catch (Exception $e) {
            error_log('Redis set failed: ' . $e->getMessage());
            return false;
        }
    }

    // 获取指定键的值。
    public function get($key) {
        try {
            return $this->redis->get($key);
        } catch (Exception $e) {
            error_log('Redis get failed: ' . $e->getMessage());
            return false;
        }
    }

    // 删除指定键。
    public function delete($key) {
        try {
            return $this->redis->del($key);
        } catch (Exception $e) {
            error_log('Redis delete failed: ' . $e->getMessage());
            return false;
        }
    }

    // 检查键是否存在。
    public function exists($key) {
        try {
            return $this->redis->exists($key);
        } catch (Exception $e) {
            error_log('Redis exists check failed: ' . $e->getMessage());
            return false;
        }
    }

    // 将键的值增加指定的量。
    public function increment($key, $amount = 1) {
        try {
            return $this->redis->incrBy($key, $amount);
        } catch (Exception $e) {
            error_log('Redis increment failed: ' . $e->getMessage());
            return false;
        }
    }

    // 将键的值减少指定的量。
    public function decrement($key, $amount = 1) {
        try {
            return $this->redis->decrBy($key, $amount);
        } catch (Exception $e) {
            error_log('Redis decrement failed: ' . $e->getMessage());
            return false;
        }
    }

    // 设置多个键值对。
    public function mset(array $data) {
        try {
            return $this->redis->mSet($data);
        } catch (Exception $e) {
            error_log('Redis mset failed: ' . $e->getMessage());
            return false;
        }
    }

    // mget(array $keys):获取多个键的值。
    public function mget(array $keys) {
        try {
            return $this->redis->mGet($keys);
        } catch (Exception $e) {
            error_log('Redis mget failed: ' . $e->getMessage());
            return false;
        }
    }

    // 清空所有数据库的数据。
    public function flushAll() {
        try {
            return $this->redis->flushAll();
        } catch (Exception $e) {
            error_log('Redis flushAll failed: ' . $e->getMessage());
            return false;
        }
    }

    //  清空当前选择的数据库的数据。
    public function flushDB() {
        try {
            return $this->redis->flushDB();
        } catch (Exception $e) {
            error_log('Redis flushDB failed: ' . $e->getMessage());
            return false;
        }
    }

    // 设置键的过期时间。
    public function expire($key, $ttl) {
        try {
            return $this->redis->expire($key, $ttl);
        } catch (Exception $e) {
            error_log('Redis expire failed: ' . $e->getMessage());
            return false;
        }
    }


       // 将一个元素推送到列表的左边
       public function lPush($key, $value) {
        try {
            return $this->redis->lPush($key, $value);
        } catch (Exception $e) {
            error_log('Redis lPush 失败: ' . $e->getMessage());
            return false;
        }
    }

    // 将一个元素推送到列表的右边
    public function rPush($key, $value) {
        try {
            return $this->redis->rPush($key, $value);
        } catch (Exception $e) {
            error_log('Redis rPush 失败: ' . $e->getMessage());
            return false;
        }
    }

    // 从列表的左边弹出一个元素
    public function lPop($key) {
        try {
            return $this->redis->lPop($key);
        } catch (Exception $e) {
            error_log('Redis lPop 失败: ' . $e->getMessage());
            return false;
        }
    }

    // 从列表的右边弹出一个元素
    public function rPop($key) {
        try {
            return $this->redis->rPop($key);
        } catch (Exception $e) {
            error_log('Redis rPop 失败: ' . $e->getMessage());
            return false;
        }
    }

    // 获取列表中指定范围的元素
    public function lRange($key, $start, $end) {
        try {
            return $this->redis->lRange($key, $start, $end);
        } catch (Exception $e) {
            error_log('Redis lRange 失败: ' . $e->getMessage());
            return false;
        }
    }

    // 获取列表的长度
    public function lLen($key) {
        try {
            return $this->redis->lLen($key);
        } catch (Exception $e) {
            error_log('Redis lLen 失败: ' . $e->getMessage());
            return false;
        }
    }

    // 从列表中移除指定数量的元素
    public function lRem($key, $count, $value) {
        try {
            return $this->redis->lRem($key, $count, $value);
        } catch (Exception $e) {
            error_log('Redis lRem 失败: ' . $e->getMessage());
            return false;
        }
    }

    // 获取列表中指定索引位置的元素
    public function lIndex($key, $index) {
        try {
            return $this->redis->lIndex($key, $index);
        } catch (Exception $e) {
            error_log('Redis lIndex 失败: ' . $e->getMessage());
            return false;
        }
    }

    // 设置列表中指定索引位置的元素
    public function lSet($key, $index, $value) {
        try {
            return $this->redis->lSet($key, $index, $value);
        } catch (Exception $e) {
            error_log('Redis lSet 失败: ' . $e->getMessage());
            return false;
        }
    }

      // 设置哈希表中的字段值
      public function hSet($key, $field, $value) {
        try {
            return $this->redis->hSet($key, $field, $value);
        } catch (Exception $e) {
            error_log('Redis hSet 失败: ' . $e->getMessage());
            return false;
        }
    }

    // 获取哈希表中指定字段的值
    public function hGet($key, $field) {
        try {
            return $this->redis->hGet($key, $field);
        } catch (Exception $e) {
            error_log('Redis hGet 失败: ' . $e->getMessage());
            return false;
        }
    }

    // 删除哈希表中的字段
    public function hDel($key, $field) {
        try {
            return $this->redis->hDel($key, $field);
        } catch (Exception $e) {
            error_log('Redis hDel 失败: ' . $e->getMessage());
            return false;
        }
    }

    // 获取哈希表中所有字段和值
    public function hGetAll($key) {
        try {
            return $this->redis->hGetAll($key);
        } catch (Exception $e) {
            error_log('Redis hGetAll 失败: ' . $e->getMessage());
            return false;
        }
    }

    // 获取哈希表中所有字段
    public function hKeys($key) {
        try {
            return $this->redis->hKeys($key);
        } catch (Exception $e) {
            error_log('Redis hKeys 失败: ' . $e->getMessage());
            return false;
        }
    }

    // 获取哈希表中所有值
    public function hVals($key) {
        try {
            return $this->redis->hVals($key);
        } catch (Exception $e) {
            error_log('Redis hVals 失败: ' . $e->getMessage());
            return false;
        }
    }

    // 检查哈希表中字段是否存在
    public function hExists($key, $field) {
        try {
            return $this->redis->hExists($key, $field);
        } catch (Exception $e) {
            error_log('Redis hExists 失败: ' . $e->getMessage());
            return false;
        }
    }

    // 获取哈希表中字段的数量
    public function hLen($key) {
        try {
            return $this->redis->hLen($key);
        } catch (Exception $e) {
            error_log('Redis hLen 失败: ' . $e->getMessage());
            return false;
        }
    }
    
       // 向集合中添加一个或多个成员
    public function sAdd($key, ...$members) {
        try {
            return $this->redis->sAdd($key, ...$members);
        } catch (Exception $e) {
            error_log('Redis sAdd 失败: ' . $e->getMessage());
            return false;
        }
    }

    // 移除集合中的一个或多个成员
    public function sRem($key, ...$members) {
        try {
            return $this->redis->sRem($key, ...$members);
        } catch (Exception $e) {
            error_log('Redis sRem 失败: ' . $e->getMessage());
            return false;
        }
    }

    // 获取集合中的所有成员
    public function sMembers($key) {
        try {
            return $this->redis->sMembers($key);
        } catch (Exception $e) {
            error_log('Redis sMembers 失败: ' . $e->getMessage());
            return false;
        }
    }

    // 判断指定成员是否在集合中
    public function sIsMember($key, $member) {
        try {
            return $this->redis->sIsMember($key, $member);
        } catch (Exception $e) {
            error_log('Redis sIsMember 失败: ' . $e->getMessage());
            return false;
        }
    }

    // 获取集合的成员数量
    public function sCard($key) {
        try {
            return $this->redis->sCard($key);
        } catch (Exception $e) {
            error_log('Redis sCard 失败: ' . $e->getMessage());
            return false;
        }
    }

    // 获取集合的随机成员
    public function sRandMember($key, $count = 1) {
        try {
            return $this->redis->sRandMember($key, $count);
        } catch (Exception $e) {
            error_log('Redis sRandMember 失败: ' . $e->getMessage());
            return false;
        }
    }

    // 获取集合的差集
    public function sDiff(...$keys) {
        try {
            return $this->redis->sDiff(...$keys);
        } catch (Exception $e) {
            error_log('Redis sDiff 失败: ' . $e->getMessage());
            return false;
        }
    }

    // 获取集合的交集
    public function sInter(...$keys) {
        try {
            return $this->redis->sInter(...$keys);
        } catch (Exception $e) {
            error_log('Redis sInter 失败: ' . $e->getMessage());
            return false;
        }
    }

    // 获取集合的并集
    public function sUnion(...$keys) {
        try {
            return $this->redis->sUnion(...$keys);
        } catch (Exception $e) {
            error_log('Redis sUnion 失败: ' . $e->getMessage());
            return false;
        }
    }

     // 向有序集合中添加一个或多个成员
     public function zAdd($key, $score, $member) {
        try {
            return $this->redis->zAdd($key, $score, $member);
        } catch (Exception $e) {
            error_log('Redis zAdd 失败: ' . $e->getMessage());
            return false;
        }
    }

    // 移除有序集合中的一个或多个成员
    public function zRem($key, $member) {
        try {
            return $this->redis->zRem($key, $member);
        } catch (Exception $e) {
            error_log('Redis zRem 失败: ' . $e->getMessage());
            return false;
        }
    }

    // 获取有序集合中指定成员的分数
    public function zScore($key, $member) {
        try {
            return $this->redis->zScore($key, $member);
        } catch (Exception $e) {
            error_log('Redis zScore 失败: ' . $e->getMessage());
            return false;
        }
    }

    // 获取有序集合中指定范围内的成员
    public function zRange($key, $start, $stop, $withScores = false) {
        try {
            return $this->redis->zRange($key, $start, $stop, $withScores);
        } catch (Exception $e) {
            error_log('Redis zRange 失败: ' . $e->getMessage());
            return false;
        }
    }

    // 获取有序集合中指定分数范围内的成员
    public function zRangeByScore($key, $min, $max, $withScores = false) {
        try {
            return $this->redis->zRangeByScore($key, $min, $max, $withScores);
        } catch (Exception $e) {
            error_log('Redis zRangeByScore 失败: ' . $e->getMessage());
            return false;
        }
    }

    // 获取有序集合中成员的总数
    public function zCard($key) {
        try {
            return $this->redis->zCard($key);
        } catch (Exception $e) {
            error_log('Redis zCard 失败: ' . $e->getMessage());
            return false;
        }
    }

    // 获取有序集合的成员数量在指定分数范围内
    public function zCount($key, $min, $max) {
        try {
            return $this->redis->zCount($key, $min, $max);
        } catch (Exception $e) {
            error_log('Redis zCount 失败: ' . $e->getMessage());
            return false;
        }
    }

    // 获取有序集合中指定范围内的成员与分数
    public function zRangeWithScores($key, $start, $stop) {
        try {
            return $this->redis->zRange($key, $start, $stop, true);
        } catch (Exception $e) {
            error_log('Redis zRangeWithScores 失败: ' . $e->getMessage());
            return false;
        }
    }
}

REDIS使用实例
$redos_config = new RedisWrapper( '127.0.0.1' , 6379 ,'' , 15 );

$redos_config->set( 'key1' , '313' );

echo $redos_config->get( 'key1' );

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值