SpringBoot封装Redis并实现通用缓存key

Redis中自己带了一个继承的模板,RedisTemplate ,但是相对来说,自带的使用起来没有那么灵活,本文使用redis搭建一个原生的通用模板

添加相关的依赖

首先添加redis的客户端依赖,以及集成FastJson用来将对象序列化,序列化为json的格式,明文可读的

      <!--redis 依赖-->
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
    </dependency>
      <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>fastjson</artifactId>
          <version>1.2.68</version>
      </dependency>

创建redis配置类,读取配置文件信息

在application.propertis配置文件中创建一下配置信息

#redis
redis.host=127.0.0.1
redis.port=6379
redis.timeout=3
redis.poolMaxTotal=10
redis.poolMaxIdle=10
redis.poolMaxWait=3

新建配置类,用来读取配置信息

package org.example.miaosha.redis;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "redis")
public class RedisConfig {
    private String host;
    private int port;
    private int timeout;//秒
    private String password;
    private int poolMaxTotal;
    private int poolMaxIdle;
    private int poolMaxWait;//秒
}

创建redis连接工厂

将配置类注入到spring中之后,随后创建redis连接工厂,产生工厂供后续调用,将JedisPool的配置放到封装的redisService中,这样操作容易造成,循环依赖的问题

package org.example.miaosha.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Service
public class RedisPoolFactory {
    @Autowired
    RedisConfig redisConfig;

    @Bean
    public JedisPool jedisPoolFactory(){
        JedisPoolConfig poolConfig =new JedisPoolConfig();
        poolConfig.setMaxIdle(redisConfig.getPoolMaxIdle());
        poolConfig.setMaxTotal(redisConfig.getPoolMaxTotal());
        poolConfig.setMaxWaitMillis(redisConfig.getPoolMaxWait()*1000);
        JedisPool jp=new JedisPool(poolConfig,redisConfig.getHost(),redisConfig.getPort()
                ,redisConfig.getTimeout()*1000,redisConfig.getPassword(),0);
        return jp;
    }
}

封装吧redis工具类

使用redis连接池封装redis工具类对象

package org.example.miaosha.redis;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
//线程池
@Service
public class RedisService {

    @Autowired
    JedisPool jedisPool;

    /**
     * 获取当个对象
     * @param prefix
     * @param key
     * @param clazz
     * @param <T>
     * @return
     */
    public<T> T get(KeyPrefix prefix,String key,Class<T> clazz){
        Jedis jedis = null;
        //连接池用完之后释放掉
        try{
           jedis = jedisPool.getResource();
           //生成真正的key
            String realKey=prefix.getPrefix()+key;
            String str = jedis.get(realKey);
            T t=stringToBean(str,clazz);
            return t;
        }finally {
            returnToPool(jedis);
        }
    }

    /**
     * 删除
     * */
    public boolean delete(KeyPrefix prefix, String key) {
        Jedis jedis = null;
        try {
            jedis =  jedisPool.getResource();
            //生成真正的key
            String realKey  = prefix.getPrefix() + key;
            long ret =  jedis.del(realKey);
            return ret > 0;
        }finally {
            returnToPool(jedis);
        }
    }

    /**
     * 判断key是否存在
     * @param prefix
     * @param key
     * @param <T>
     * @return
     */
    public<T> boolean exists(KeyPrefix prefix,String key){
        Jedis jedis = null;
        //连接池用完之后释放掉
        try{
            jedis = jedisPool.getResource();
            //生成真正的key
            String realKey=prefix.getPrefix()+key;
            jedis.exists(realKey);
            return true;
        }finally {
            returnToPool(jedis);
        }
    }

//返回long类型

    /**
     * 增加值
     * @param prefix
     * @param key
     * @param <T>
     * @return
     */
    public<T> Long incr(KeyPrefix prefix,String key){
        Jedis jedis = null;
        //连接池用完之后释放掉
        try{
            jedis = jedisPool.getResource();
            //生成真正的key
            String realKey=prefix.getPrefix()+key;
            jedis.exists(realKey);
            return jedis.incr(realKey);
        }finally {
            returnToPool(jedis);
        }
    }

    /**
     * 减少值  原子操作 不可分 不会有中间状态
     * @param prefix
     * @param key
     * @param <T>
     * @return
     */
    public<T> Long decr(KeyPrefix prefix,String key){
        Jedis jedis = null;
        //连接池用完之后释放掉
        try{
            jedis = jedisPool.getResource();
            //生成真正的key
            String realKey=prefix.getPrefix()+key;
            jedis.exists(realKey);
            return jedis.decr(realKey);
        }finally {
            returnToPool(jedis);
        }
    }


    public static <T> String beanToString(T value) {
        if(value == null){
            return null;
        }
        Class<?> clazz = value.getClass();
        if(clazz == int.class || clazz==Integer.class)
        {
            return ""+value;
        }else if(clazz == String.class){
            return (String)value;
        }else if(clazz == long.class || clazz==Long.class){
            return ""+value;
        }else{
            return JSON.toJSONString(value);
        }
    }

    @SuppressWarnings("unchecked")
    public <T> T stringToBean(String str,Class<T> clazz) {
        if(str == null||str.length()<0||clazz==null){
            return null;
        }
        if(clazz == int.class || clazz==Integer.class)
        {
            return (T)Integer.valueOf(str);
        }else if(clazz == String.class){
            return (T) str;
        }else if(clazz == long.class || clazz==Long.class){
            return (T)Long.valueOf(str);
        }else{
            return JSON.toJavaObject(JSON.parseObject(str),clazz);
        }
    }

    private void returnToPool(Jedis jedis) {
        if(jedis!=null){
            jedis.close();
        }
    }


    /**
     * 设置对象
     * */
    public <T> boolean set(KeyPrefix prefix, String key,  T value) {
        Jedis jedis = null;
        try {
            jedis =  jedisPool.getResource();
            String str = beanToString(value);
            if(str == null || str.length() <= 0) {
                return false;
            }
            //生成真正的key
            String realKey  = prefix.getPrefix() + key;
            int seconds =  prefix.expireSeconds();
            if(seconds <= 0) {
                jedis.set(realKey, str);
            }else {
                jedis.setex(realKey, seconds, str);
            }
            return true;
        }finally {
            returnToPool(jedis);
        }
    }
}

通用缓存key的封装

对于同一个key如果不进行区别,可能会出现value值的覆盖,所以将不同用途的key进行了区分,并进行封装,于是就加上了prefix,前缀,这里的封装采用了设计模式中的模板模式,首先定义公用的接口,然后在使用抽象类,实现接口中的一些共有的内容,最后所有具体的实现类都继承于这个公有的模板类
在这里插入图片描述

实现相应缓存key的接口

为了区别每一个key,会加上一个前缀,另外前缀还会有过期时间,所有接口类就约束了这样的一个规范

package org.example.miaosha.redis;
public interface KeyPrefix {
    public int expireSeconds();
    public String getPrefix();
}

抽象类,实现默认代码

package org.example.miaosha.redis;
import lombok.AllArgsConstructor;
/**
 * 抽象类 不会不小心被创建出来
 */
@AllArgsConstructor
public abstract class BasePrefix implements KeyPrefix{

    private int expireSeconds;

    private String prefix;

    @Override
    public int expireSeconds() {//默认0代表永不过期
        return expireSeconds;
    }

    @Override
    public String getPrefix() {
        String className = getClass().getSimpleName();
        return className+":"+prefix;
    }

    public BasePrefix(String prefix){//0代表用不过期
        this(0,prefix);
    }
}

子类详细封装

最后,所有不同的key分别继承于这个抽象类,实现不同的功能,这个是用户key

public class UserKey extends BasePrefix {
     private UserKey(String prefix){
         super(prefix);
     }
    //防止在外面实例化
    public static UserKey getById=new UserKey("id");
    public static UserKey getByName=new UserKey("name");
}

下面是订单key

package org.example.miaosha.redis;

public class OrderKey extends BasePrefix{
    public OrderKey(String prefix) {
        super(prefix);
    }

    public static OrderKey getMiaoshaOrderByUidGid = new OrderKey("moug");
}

这样就使用redis封装了一个通用的缓存key的一个接口

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值