spring boot RedisTemplateHelper

位置:首页>文章>详情   分类: 教程分享 > Java教程   阅读(812)   2024-04-17 12:31:29

spring boot RedisTemplateHelper

 


import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;


@Component
public class RedisTemplateHelper {
    /** 初始化 */
    private static RedisTemplateHelper redisTemplateHelper;

    @Resource
    private RedisTemplate<String,Object> stringObjectRedisTemplate;

    @PostConstruct
    public void init() {
        redisTemplateHelper = this;
    }

    @Bean(name="stringObjectRedisTemplate")
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String,Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        // 值采用json序列化
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        //使用StringRedisSerializer来序列化和反序列化redis的key值
        template.setKeySerializer(new StringRedisSerializer());
        // 设置hash key 和value序列化模式
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }

    /**
     * 写入缓存
     * @param key
     * @param value
     * @return
     */
    public static boolean set(String key, Object value) {
        boolean result = false;
        try {
            ValueOperations<String, Object> operations = redisTemplateHelper.stringObjectRedisTemplate.opsForValue();
            operations.set(key,value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    /**
     * 写入缓存设置时效时间
     * @param key
     * @param value
     * @return
     */
    public static boolean set(String key, Object value, Long expireTime) {
        boolean result = false;
        try {
            ValueOperations<String, Object> operations = redisTemplateHelper.stringObjectRedisTemplate.opsForValue();
            operations.set(key, value);
            redisTemplateHelper.stringObjectRedisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    /**
     * 批量删除对应的value
     * @param keys
     */
    public static void remove(String... keys) {
        for (String key : keys) {
            remove(key);
        }
    }

    /**
     * 批量删除key
     * @param pattern
     */
    public static void removePattern(String pattern) {
        Set<String> keys = redisTemplateHelper.stringObjectRedisTemplate.keys(pattern);
        if (keys.size() > 0)
            redisTemplateHelper.stringObjectRedisTemplate.delete(keys);
    }
    /**
     * 删除对应的value
     * @param key
     */
    public static void remove(String key) {
        if (exists(key)) {
            redisTemplateHelper.stringObjectRedisTemplate.delete(key);
        }
    }

    /**
     * 判断缓存中是否有对应的value
     * @param key
     * @return
     */
    public static boolean exists(String key) {
        return redisTemplateHelper.stringObjectRedisTemplate.hasKey(key);
    }
    /**
     * 读取缓存
     * @param key
     * @return
     */
    public static Object get(String key) {
        Object result = null;
        ValueOperations<String, Object> operations = redisTemplateHelper.stringObjectRedisTemplate.opsForValue();
        result = operations.get(key);
        return result;
    }
    /**
     * 哈希 添加
     * @param key
     * @param hashKey
     * @param value
     */
    public static void hmSet(String key, Object hashKey, Object value){
        HashOperations<String, Object, Object> hash = redisTemplateHelper.stringObjectRedisTemplate.opsForHash();
        hash.put(key,hashKey,value);
    }

    /**
     * 哈希获取数据
     * @param key
     * @param hashKey
     * @return
     */
    public static Object hmGet(String key, Object hashKey){
        HashOperations<String, Object, Object>  hash = redisTemplateHelper.stringObjectRedisTemplate.opsForHash();
        return hash.get(key,hashKey);
    }

    /**
     * 列表添加
     * @param k
     * @param v 
     */
    public static void lPush(String k,Object v){
        ListOperations<String, Object> list = redisTemplateHelper.stringObjectRedisTemplate.opsForList();
        list.rightPush(k,v);
    }

    /**
     * 列表删除
     */
    public static void lrem(String k,Object v){
        ListOperations<String, Object> list = redisTemplateHelper.stringObjectRedisTemplate.opsForList();
    	list.remove(k, 1, v);
    }
    /**
     * 列表获取
     * @param k
     * @param l
     * @param l1
     * @return
     */
    public static List<Object> lRange(String k, long l, long l1){
        ListOperations<String, Object> list = redisTemplateHelper.stringObjectRedisTemplate.opsForList();
        return list.range(k,l,l1);
    }

    /**
     * 集合添加
     * @param key
     * @param value
     */
    public static void add(String key,Object value){
        SetOperations<String, Object> set = redisTemplateHelper.stringObjectRedisTemplate.opsForSet();
        set.add(key,value);
    }

    /**
     * 集合获取
     * @param key
     * @return
     */
    public static Set<Object> setMembers(String key){
        SetOperations<String, Object> set = redisTemplateHelper.stringObjectRedisTemplate.opsForSet();
        return set.members(key);
    }

    /**
     * 有序集合添加
     * @param key
     * @param value
     * @param scoure
     */
    public static void zAdd(String key,Object value,double scoure){
        ZSetOperations<String, Object> zset = redisTemplateHelper.stringObjectRedisTemplate.opsForZSet();
        zset.add(key,value,scoure);
    }

    /**
     * 有序集合获取
     * @param key
     * @param scoure
     * @param scoure1
     * @return
     */
    public static Set<Object> rangeByScore(String key,double scoure,double scoure1){
        ZSetOperations<String, Object> zset = redisTemplateHelper.stringObjectRedisTemplate.opsForZSet();
        return zset.rangeByScore(key, scoure, scoure1);
    }  
}
 
标签2: redis spirng boot java
地址:https://www.leftso.com/article/939.html

相关阅读

Spring Boot 2.0 Redis整合,通过spring boot 2.0整合Redis作为spring缓存框架的实现。
spring boot 1.5整合redis实现spring的缓存框架,spring boot,redis
简述本文主要通过一个简单的例子模拟实现秒杀情景,其中主要使用Redis事物进行实现spring boot为提供方便的环境
Java连接redis启动报错Error redis clients jedis HostAndPort cant resolve localhost address
spring boot RedisTemplateHelper import org.springframework.context.annotation.Bean; import org.s...
Java编程之spring boot shiro redis整合基于角色和权限的安全管理,Java编程,spring boot,shiro,权限控制
前言继续上一篇Spring Boot Redis 秒杀实现 的一个修改版本,主要实现用ab工具进行网页正式访问的一个版本,其主要目的还是介绍Redis实现秒杀活动的一种方式
思路通过redis的有效期和切面来配合处理环境springboot2.7.xspring-boot-starter-webspring-boot-starter-aopjava1.8redis编...
简述在本博客中,我们将会创建一个reids的消息队列,Redis可以被当成消息队列使用
redis 命令查看使用情况redis info命令详解,redis查看内存使用情况。redis info命令的详细解释