详解Spring Boot Redis集成配置

spring Boot 熟悉后,集成一个外部扩展是一件很容易的事,集成Redis也很简单,以下是本站小编搜索整理的关于详解Spring Boot Redis集成配置,供参考学习,希望对大家有所帮助!想了解更多相关信息请持续关注我们应届毕业生考试网!

详解Spring Boot Redis集成配置

  一、添加pom依赖

<dependency>

<groupId></groupId>

<artifactId>spring-boot-starter-redis</artifactId>

</dependency>

  二、创建

注意该类存放的package

package s;

import ArrayInputStream;

import ArrayOutputStream;

import ception;

import ctInputStream;

import ctOutputStream;

import pportedEncodingException;

import ngUtils;

import er;

import erFactory;

import s;

import ocol;

import sException;

/**

* 工具类 RedisClient

* 因为本类中获取JedisPool调用的是JedisConnectionFactory中protected修饰的方法fetchJedisConnector()

* 所以该类需要与JedisConnectionFactory在同一个package中

*

* @author 单红宇(CSDN CATOOP)

* @create 2017年4月9日

*/

public class RedisClient {

private static Logger logger = ogger(s);

private JedisConnectionFactory factory;

public RedisClient(JedisConnectionFactory factory) {

super();

ory = factory;

}

/**

* put操作(存储序列化对象)+ 生效时间

*

* @param key

* @param value

* @return

*/

public void putObject(final String key, final Object value, final int cacheSeconds) {

if (tBlank(key)) {

redisTemplete(key, new RedisExecute<Object>() {

@Override

public Object doInvoker(Jedis jedis) {

try {

x(ytes(SET), cacheSeconds, serialize(value));

} catch (UnsupportedEncodingException e) {

}

return null;

}

});

}

}

/**

* get操作(获取序列化对象)

*

* @param key

* @return

*/

public Object getObject(final String key) {

return redisTemplete(key, new RedisExecute<Object>() {

@Override

public Object doInvoker(Jedis jedis) {

try {

byte[] byteKey = ytes(SET);

byte[] byteValue = (byteKey);

if (byteValue != null) {

return deserialize(byteValue);

}

} catch (UnsupportedEncodingException e) {

return null;

}

return null;

}

});

}

/**

* setex操作

*

* @param key

* 键

* @param value

* 值

* @param cacheSeconds

* 超时时间,0为不超时

* @return

*/

public String set(final String key, final String value, final int cacheSeconds) {

return redisTemplete(key, new RedisExecute<String>() {

@Override

public String doInvoker(Jedis jedis) {

if (cacheSeconds == 0) {

return (key, value);

}

return x(key, cacheSeconds, value);

}

});

}

/**

* get操作

*

* @param key

* 键

* @return 值

*/

public String get(final String key) {

return redisTemplete(key, new RedisExecute<String>() {

@Override

public String doInvoker(Jedis jedis) {

String value = (key);

return tBlank(value) && !"nil"lsIgnoreCase(value) ? value : null;

}

});

}

/**

* del操作

*

* @param key

* 键

* @return

*/

public long del(final String key) {

return redisTemplete(key, new RedisExecute<Long>() {

@Override

public Long doInvoker(Jedis jedis) {

return (key);

}

});

}

/**

* 获取资源

*

* @return

* @throws JedisException

*/

public Jedis getResource() throws JedisException {

Jedis jedis = null;

try {

jedis = hJedisConnector();

} catch (JedisException e) {

r("getResource.", e);

returnBrokenResource(jedis);

throw e;

}

return jedis;

}

/**

* 获取资源

*

* @return

* @throws JedisException

*/

public Jedis getJedis() throws JedisException {

return getResource();

}

/**

* 归还资源

*

* @param jedis

* @param isBroken

*/

public void returnBrokenResource(Jedis jedis) {

if (jedis != null) {

e();

}

}

/**

* 释放资源

*

* @param jedis

* @param isBroken

*/

public void returnResource(Jedis jedis) {

if (jedis != null) {

e();

}

}

/**