springBoot集成redis

在pom中引入redis 依赖

     <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>3.1.0</version>
        </dependency>  

配置文件定义,spring.redis.pool在新版本已移除,修改为spring.redis.jedis.pool

#redis
#redis索引 默认0,最大15
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
#超时时间,单位毫秒
spring.redis.timeout=2000
#连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8
#连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
 

创建redis配置文件,在项目启动时注入

package com.reptile.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * redis 配置
 *
 * @author ru
 */
@Configuration
public class JredisConfig {


    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private int port;


    @Value("${spring.redis.timeout}")
    private int timeout;

    @Value("${spring.redis.jedis.pool.max-active}")
    private int maxActive;

    @Value("${spring.redis.jedis.pool.max-wait}")
    private int maxWaitMillis;

    @Value("${spring.redis.jedis.pool.max-idle}")
    private int minIdle;

    @Value("${spring.redis.jedis.pool.min-idle}")
    private int maxIdle;

    /**
     * 注入redis
     *
     * @return
     */
    @Bean
    public JedisPool redisPoolFactory() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        jedisPoolConfig.setMaxTotal(maxActive);
        jedisPoolConfig.setMinIdle(minIdle);
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, null);
        return jedisPool;
    }
}

 

 

创建序列化工具类  

package com.reptile.util;

import java.io.*;

/**
 * 对象序列化
 * @author ru
 */
public class SerializeUtil {
    /**
     * 序列化
     * @param obj
     * @return
     */
    public static byte [] serialize(Object obj){
        ObjectOutputStream obi=null;
        ByteArrayOutputStream bai=null;
        try {
            bai=new ByteArrayOutputStream();
            obi=new ObjectOutputStream(bai);
            obi.writeObject(obj);
            byte[] byt=bai.toByteArray();
            return byt;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 反序列化
     * @param byt
     * @return
     */
    public static Object unserizlize(byte[] byt){
        ObjectInputStream oii=null;
        ByteArrayInputStream bis=null;
        bis=new ByteArrayInputStream(byt);
        try {
            oii=new ObjectInputStream(bis);
            Object obj=oii.readObject();
            return obj;
        } catch (Exception e) {

            e.printStackTrace();
        }


        return null;
    }
}

创建redis常用工具类

package com.reptile.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 *  redis 常用工具类
 *  Component注解其他类注解
 * @author ru
 */
@Component
public class JredisUtil {

    @Autowired
    private JedisPool jedisPool;

    /**
     * 存储字符串键值对
     * @param key
     * @param value
     * @return
     */
    public String set(String key, String value) {
        Jedis jedis = jedisPool.getResource();
        try {
            return jedis.set(key, value);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            jedis.close();
        }
    }

    /**
     * 获取字符串key对应值
     * @param key
     * @return
     */
    public String get(String key){
        Jedis jedis = jedisPool.getResource();
        try {
            return jedis.get(key);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            jedis.close();
        }
    }

    /**
     * 删除字符串键值对
     * @param key
     * @return
     */
    public Long del(String key) {
        Jedis jedis = jedisPool.getResource();
        try {
            return jedis.del(key);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            jedis.close();
        }
    }

    /**
     * 存储对象
     * @param key
     * @param value
     * @return

     */
    public String setObject(String key, Object value) {
        Jedis jedis = jedisPool.getResource();
        try {
            return jedis.set(key.getBytes(), SerializeUtil.serialize(value));
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            jedis.close();
        }
    }

    /**
     * 得到对应键的对象
     * @param key
     * @return
     */
    public Object getObject(String key) {
        Jedis jedis = jedisPool.getResource();
        try {
            byte[] byteArr =  jedis.get(key.getBytes());
            return SerializeUtil.unserizlize(byteArr);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            jedis.close();
        }
    }

    /**
     * 判断键值是否存在
     * @param key
     * @return
     */
    public boolean isExist(String key){
        Jedis jedis = jedisPool.getResource();
        try {
            return jedis.exists(key);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            jedis.close();
        }
    }

}

单元测试

package com.reptile.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 *  redis 常用工具类
 *  Component注解其他类注解
 * @author ru
 */
@Component
public class JredisUtil {

    @Autowired
    private JedisPool jedisPool;

    /**
     * 存储字符串键值对
     * @param key
     * @param value
     * @return
     */
    public String set(String key, String value) {
        Jedis jedis = jedisPool.getResource();
        try {
            return jedis.set(key, value);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            jedis.close();
        }
    }

    /**
     * 获取字符串key对应值
     * @param key
     * @return
     */
    public String get(String key){
        Jedis jedis = jedisPool.getResource();
        try {
            return jedis.get(key);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            jedis.close();
        }
    }

    /**
     * 删除字符串键值对
     * @param key
     * @return
     */
    public Long del(String key) {
        Jedis jedis = jedisPool.getResource();
        try {
            return jedis.del(key);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            jedis.close();
        }
    }

    /**
     * 存储对象
     * @param key
     * @param value
     * @return

     */
    public String setObject(String key, Object value) {
        Jedis jedis = jedisPool.getResource();
        try {
            return jedis.set(key.getBytes(), SerializeUtil.serialize(value));
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            jedis.close();
        }
    }

    /**
     * 得到对应键的对象
     * @param key
     * @return
     */
    public Object getObject(String key) {
        Jedis jedis = jedisPool.getResource();
        try {
            byte[] byteArr =  jedis.get(key.getBytes());
            return SerializeUtil.unserizlize(byteArr);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            jedis.close();
        }
    }

    /**
     * 判断键值是否存在
     * @param key
     * @return
     */
    public boolean isExist(String key){
        Jedis jedis = jedisPool.getResource();
        try {
            return jedis.exists(key);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            jedis.close();
        }
    }

}

 

posted @ 2019-10-25 15:57  不列颠剑圣  阅读(115)  评论(0)    收藏  举报