Redis底层封装细节

news/2024/7/8 2:08:11

日常我们程序员在使用redis做缓存的时候,很少会直接使用到RedisTemplate直接操作k-v键值对,而是通过对RedisTemplate原生代码的封装,来构建我们日常便于使用习惯的代码来操作数据,这里我分享一下日常基本的对RedisTemplate底层的封装原理和使用方法

package com.ljj.common.core.redis;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

/**
 * spring redis 工具类
 *
 * @author ljj
 **/
@SuppressWarnings(value = { "unchecked", "rawtypes" })
@Component
public class RedisCache
{
    @Autowired
    public RedisTemplate redisTemplate;

    /**
     * 缓存基本的对象,Integer、String、实体类等
     *
     * @param key 缓存的键值
     * @param value 缓存的值
     */
    public <T> void setCacheObject(final String key, final T value)
    {
        redisTemplate.opsForValue().set(key, value);
    }

    /**
     * 缓存基本的对象,Integer、String、实体类等
     *
     * @param key 缓存的键值
     * @param value 缓存的值
     * @param timeout 时间
     * @param timeUnit 时间颗粒度
     */
    public <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit)
    {
        redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
    }

    /**
     * 设置有效时间
     *
     * @param key Redis键
     * @param timeout 超时时间
     * @return true=设置成功;false=设置失败
     */
    public boolean expire(final String key, final long timeout)
    {
        return expire(key, timeout, TimeUnit.SECONDS);
    }

    /**
     * 设置有效时间
     *
     * @param key Redis键
     * @param timeout 超时时间
     * @param unit 时间单位
     * @return true=设置成功;false=设置失败
     */
    public boolean expire(final String key, final long timeout, final TimeUnit unit)
    {
        return redisTemplate.expire(key, timeout, unit);
    }

    /**
     * 获取有效时间
     *
     * @param key Redis键
     * @return 有效时间
     */
    public long getExpire(final String key)
    {
        return redisTemplate.getExpire(key);
    }

    /**
     * 判断 key是否存在
     *
     * @param key 键
     * @return true 存在 false不存在
     */
    public Boolean hasKey(String key)
    {
        return redisTemplate.hasKey(key);
    }

    /**
     * 获得缓存的基本对象。
     *
     * @param key 缓存键值
     * @return 缓存键值对应的数据
     */
    public <T> T getCacheObject(final String key)
    {
        ValueOperations<String, T> operation = redisTemplate.opsForValue();
        return operation.get(key);
    }

    /**
     * 删除单个对象
     *
     * @param key
     */
    public boolean deleteObject(final String key)
    {
        return redisTemplate.delete(key);
    }

    /**
     * 删除集合对象
     *
     * @param collection 多个对象
     * @return
     */
    public boolean deleteObject(final Collection collection)
    {
        return redisTemplate.delete(collection) > 0;
    }

    /**
     * 缓存List数据
     *
     * @param key 缓存的键值
     * @param dataList 待缓存的List数据
     * @return 缓存的对象
     */
    public <T> long setCacheList(final String key, final List<T> dataList)
    {
        Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
        return count == null ? 0 : count;
    }

    /**
     * 获得缓存的list对象
     *
     * @param key 缓存的键值
     * @return 缓存键值对应的数据
     */
    public <T> List<T> getCacheList(final String key)
    {
        return redisTemplate.opsForList().range(key, 0, -1);
    }

    /**
     * 缓存Set
     *
     * @param key 缓存键值
     * @param dataSet 缓存的数据
     * @return 缓存数据的对象
     */
    public <T> BoundSetOperations<String, T> setCacheSet(final String key, final Set<T> dataSet)
    {
        BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);
        Iterator<T> it = dataSet.iterator();
        while (it.hasNext())
        {
            setOperation.add(it.next());
        }
        return setOperation;
    }

    /**
     * 获得缓存的set
     *
     * @param key
     * @return
     */
    public <T> Set<T> getCacheSet(final String key)
    {
        return redisTemplate.opsForSet().members(key);
    }

    /**
     * 缓存Map
     *
     * @param key
     * @param dataMap
     */
    public <T> void setCacheMap(final String key, final Map<String, T> dataMap)
    {
        if (dataMap != null) {
            redisTemplate.opsForHash().putAll(key, dataMap);
        }
    }

    /**
     * 获得缓存的Map
     *
     * @param key
     * @return
     */
    public <T> Map<String, T> getCacheMap(final String key)
    {
        return redisTemplate.opsForHash().entries(key);
    }

    /**
     * 往Hash中存入数据
     *
     * @param key Redis键
     * @param hKey Hash键
     * @param value 值
     */
    public <T> void setCacheMapValue(final String key, final String hKey, final T value)
    {
        redisTemplate.opsForHash().put(key, hKey, value);
    }

    /**
     * 获取Hash中的数据
     *
     * @param key Redis键
     * @param hKey Hash键
     * @return Hash中的对象
     */
    public <T> T getCacheMapValue(final String key, final String hKey)
    {
        HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash();
        return opsForHash.get(key, hKey);
    }

    /**
     * 获取多个Hash中的数据
     *
     * @param key Redis键
     * @param hKeys Hash键集合
     * @return Hash对象集合
     */
    public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys)
    {
        return redisTemplate.opsForHash().multiGet(key, hKeys);
    }

    /**
     * 删除Hash中的某条数据
     *
     * @param key Redis键
     * @param hKey Hash键
     * @return 是否成功
     */
    public boolean deleteCacheMapValue(final String key, final String hKey)
    {
        return redisTemplate.opsForHash().delete(key, hKey) > 0;
    }

    /**
     * 获得缓存的基本对象列表
     *
     * @param pattern 字符串前缀
     * @return 对象列表
     */
    public Collection<String> keys(final String pattern)
    {
        return redisTemplate.keys(pattern);
    }
}

以上是我日常使用过程中对redis封装的代码,屏蔽了底层的细节操作,可以让程序员可以非常便捷的使用到redis服务


http://lihuaxi.xjx100.cn/news/1357728.html

相关文章

【C++】STL反向迭代器模拟实现,迭代器适配器,迭代器类型简单介绍

反向迭代器 前言正式开始基本演示模拟实现的大致思路基本框架前置、--*重载->重载list和vector适配反向迭代器迭代器的类型库中reverse和sort模板参数中的迭代器reversesort 前言 本篇主要讲反向迭代器的模拟实现。 能够加深各位对泛型的理解。 前面我那篇string介绍里面…

将maven库中没有的jar包导入本地库后编译还提示缺这个jar包

Maven本地仓库有对应的jar包但是报找不到 问题原因 第一&#xff0c;你本地仓库对应的包文件夹下有_remote.repositories这个文件&#xff1b; 第二&#xff0c;你的项目现在连接不到下载这个包的仓库&#xff1b; 以上两点就是本地明明有对应的jar包&#xff0c;但项目中还…

Anaconda安装和激活

一、Anaconda下载地址 https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/?CM&OD 说明&#xff1a;使用paddlepaddle需要先安装python环境&#xff0c;这里我们选择python集成环境Anaconda工具包 Anaconda是1个常用的python包管理程序安装完Anaconda后&#xff0c…

C语言程序设计——结构体、枚举、联合

一、结构体 1. 概念 结构体是一种自定义的数据类型&#xff0c;它是一些值的集合&#xff0c;这些值称为成员变量&#xff0c;结构体由多个数据成员组成&#xff0c;这些成员可以是不同类型的变量。 2. 结构体的声明 2.1 一般声明 struct student {char name[10];int age; …

左右指针向中间相遇

Leetcode原题&#xff1a;Container With Most Water 以及 华为OD考题–太阳能板最大面积&#xff0c;底层完全是相同的。有的题解这样解释为什么可以用双指针&#xff1a; 初始化双指针分列水槽左右两端&#xff0c;循环每轮将短板向内移动一格&#xff0c;并更新面积最大值&a…

Java对日志文件进行加密

最近碰到了一个新的需求&#xff0c;生产环境中Java程序部署的服务器会定期清理数据&#xff0c;需要将保存在程序所在服务器上的日志文件挂载到网盘上&#xff0c;但又不想让用户看到日志文件中的信息&#xff0c;因此需要对日志文件中的内容进行加密。 这里&#xff0c;并不是…

【C++】开源:跨平台轻量日志库easyloggingpp

&#x1f60f;★,:.☆(&#xffe3;▽&#xffe3;)/$:.★ &#x1f60f; 这篇文章主要介绍跨平台轻量日志库easyloggingpp。 无专精则不能成&#xff0c;无涉猎则不能通。。——梁启超 欢迎来到我的博客&#xff0c;一起学习&#xff0c;共同进步。 喜欢的朋友可以关注一下&am…

ChatGPT在智能城市和智能交通中的应用如何?

ChatGPT在智能城市和智能交通领域的应用具有潜力和广泛的可能性。智能城市和智能交通是现代科技与信息技术在城市规划和交通管理中的应用&#xff0c;旨在提高城市的运行效率、生活质量&#xff0c;减少交通拥堵和环境污染&#xff0c;为居民提供更便利、安全的出行方式。ChatG…