集成redis-jar包依赖,java对应的是jedis
在pom中增加依赖关系
我们集成redis的目标是将对象写入到redis中,因此还需要json的功能
配置文件
将redis的配置属性配置到application.properties中
##redis==========================
#redis
redis.host=localhost
redis.port=6379
redis.timeout=3
redis.password=123456
redis.poolMaxTotal=10
redis.poolMaxIdle=10
redis.poolMaxWait=3
读取redis配置属性
我们可以使用spring的注解 @ConfigurationProperties 来进行配置属性的读取
===============RedisConfig.java
package miaosha.redis.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* 使用 @ConfigurationProperties 读取 配置文件前缀为 redis的配置项目
* @author kaifeng1
*
*/
@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;// 秒
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public int getTimeout() {
return timeout;
}
public void setTimeout(int timeout) {
this.timeout = timeout;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getPoolMaxTotal() {
return poolMaxTotal;
}
public void setPoolMaxTotal(int poolMaxTotal) {
this.poolMaxTotal = poolMaxTotal;
}
public int getPoolMaxIdle() {
return poolMaxIdle;
}
public void setPoolMaxIdle(int poolMaxIdle) {
this.poolMaxIdle = poolMaxIdle;
}
public int getPoolMaxWait() {
return poolMaxWait;
}
public void setPoolMaxWait(int poolMaxWait) {
this.poolMaxWait = poolMaxWait;
}
}
做一个操作redis的 接口,访问redis
这里核心的用法就是
创建 JedisPoolConfig ,然后 得到 JedisPool ,连接池中获取到 Jedis
最终我们使用Jedis进行操作。这个过程和大多数数据库的java操作是类似的。
属性参数-得到连接-然后进行操作
RedisService 接口
==========RedisService.java========
package miaosha.redis.service;
/**
* redis 操作服务
* @author kaifeng1
*
*/
public interface RedisService {
/**
* 读取key对应的bean
* @param key
* @param claz
* @return
* @throws Exception
*/
<T> T getBean(String key , Class<T> claz) throws Exception ;
/**
* 将bean写进redis
* @param key
* @param bean
* @throws Exception
*/
<T> void setBean(String key , T bean) throws Exception;
/**
* 删除key
* @param key
* @throws Exception
*/
<T> void delBean(String key,Class<T> claz) throws Exception;
}
RedisServiceImpl 实现类
RedisServiceImpl.java
package miaosha.redis.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import miaosha.redis.service.RedisService;
import miaosha.util.JsonCom;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
/**
* redis的操作类
*
* @author kaifeng1
*
*/
@Service
public class RedisServiceImpl implements RedisService {
@Autowired
JedisPool jedisPool;
public <T> T getBean(String key, Class<T> claz) {
Jedis jedis = null;
T r = null;
try {
jedis = this.jedisPool.getResource();
String json = jedis.get(key);
r = JsonCom.jsonToBean(json, claz);
} finally {
this.returnPool(jedis);
}
return r;
}
public <T> void setBean(String key, T bean) {
Jedis jedis = null;
try {
jedis = this.jedisPool.getResource();
String json = JsonCom.beanToJson(bean);
jedis.set(key, json);
} finally {
this.returnPool(jedis);
}
}
/**
* 连接回到连接池
*
* @param jedis
*/
private void returnPool(Jedis jedis) {
if (jedis != null) {
jedis.close();
}
}
/**
* 删除指定的key
*/
public <T> void delBean(String key, Class<T> claz) {
Jedis jedis = null;
try {
jedis = this.jedisPool.getResource();
if (claz == null) {
jedis.del(key);
return;
}
T bean = this.getBean(key, claz);
if (bean != null) {
jedis.del(key);
} else {
System.out.println("key:" + key + " has not value!");
}
} finally {
this.returnPool(jedis);
}
}
}
使用@Bean注解来注册一个JedisPool 的bean
JedisPoolFactory.java
package miaosha.redis.factory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import miaosha.redis.config.RedisConfig;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* 我们需要获取到一个redis的连接池对象,从而可以获取到Jredis来进行redis的操作
*
* @author kaifeng1
*
*/
@Component
public class JedisPoolFactory {
@Autowired
RedisConfig redisConfig;
@Bean
public JedisPool createJedisPool() {
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;
}
}
Json的处理
JsonCom.java
package miaosha.util;
import com.alibaba.fastjson.JSON;
/**
* json处理
* @author kaifeng1
*
*/
public final class JsonCom {
public static <T> String beanToJson(T bean) {
String value = null;
if (bean == null) {
return value;
}
Class<?> claz = bean.getClass();
if (claz == int.class || claz == Integer.class) {
return String.valueOf(bean);
} else if (claz == float.class || claz == Float.class) {
return String.valueOf(bean);
} else if (claz == long.class || claz == Long.class) {
return String.valueOf(bean);
} else if (claz == boolean.class || claz == Boolean.class) {
return String.valueOf(bean);
} else if (claz == String.class) {
return (String) bean;
}
return JSON.toJSONString(bean);
}
@SuppressWarnings("unchecked")
public static <T> T jsonToBean(String json, Class<T> claz) {
T t = null;
if (claz == int.class || claz == Integer.class) {
t = (T) Integer.valueOf(json);
} else if (claz == float.class || claz == Float.class) {
t = (T) Float.valueOf(json);
} else if (claz == long.class || claz == Long.class) {
t = (T) Long.valueOf(json);
} else if (claz == boolean.class || claz == Boolean.class) {
t = (T) Boolean.valueOf(json);
} else if (claz == String.class) {
t = (T) json;
} else {
t = (T) JSON.toJavaObject(JSON.parseObject(json), claz);
}
return t;
}
}
防止key重复的设计考虑
不同的人来使用这个service可能key会重复,因此我们要在key上做一些处理。
可以类比命名空间的使用,我们做一个前缀空间,这样不同的业务人员使用不同的前缀。
这里我们简单的做法是先做好前缀空间,然后使用对应的数据库的bean的class作为key
将对应的前缀空间存储起来。
为了不改变上面的redis的设计,我们使用代理模式来完成这个 前缀的处理.
详情参照下一节 【springboot-No4 集成redis 下 redis的key前缀空间引入】