SpringBoot集成原生redis
阿新 • • 發佈:2018-06-30
reids face gethost driver let 字段名 camel nic lsi
redis的使用之一是Spring-data-redis,前面有介紹。
本篇介紹原生redis也就是jedis。這個效率更高
1.maven引入依賴
<!--springBoot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--druid pool-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.5</version>
</dependency>
<!--redis-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<!--json-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.38</version>
</dependency>
2.在application.properties中配置數據源
# mybatis
#別名
mybatis.type-aliases-package=com.imooc.miaosha.domain
#mybatis默認是屬性名和數據庫字段名一一對應的,即
#數據庫表列:user_name
#實體類屬性:user_name
#但是java中一般使用駝峰命名
#數據庫表列:user_name
#實體類屬性:userName
#在Springboot中,可以通過設置map-underscore-to-camel-case屬性為true來開啟駝峰功能。
#mybatis.configuration.mapUnderscoreToCamelCase屬性為true也是開啟駝峰,且優先級更高
mybatis.configuration.mapUnderscoreToCamelCase=true
mybatis.configuration.default-fetch-size=100
mybatis.configuration.default-statement-timeout=3000
#掃描映射文件
mybatis.mapperLocations = classpath:com/imooc/miaosha/dao/*.xml
# druid
spring.datasource.url=jdbc:mysql://192.168.220.128:3306/miaosha?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.filters=stat
spring.datasource.maxActive=2
spring.datasource.initialSize=1
spring.datasource.maxWait=60000
spring.datasource.minIdle=1
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=select ‘x‘
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxOpenPreparedStatements=20
#redis
redis.host=192.168.220.128
redis.port=6379
redis.timeout=3
redis.password=123456
redis.poolMaxTotal=10
redis.poolMaxIdle=10
redis.poolMaxWait=3
3.封裝Reids
第一步:編寫RedisConfig.java 加載配置的redis文件
/**
* 加載讀取配置文件
*/
@Component
@ConfigurationProperties(prefix="redis")//掃描配置文件,提取以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;
}
}
第二步:編寫RedisPoolFactory.java獲取redispool
/**
* 獲取redisPo
*/
@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);//database屬性是redis是支持多個庫的,默認16個庫,索引從0開始
return jp;
}
}
第三步:定義redis Key的生成策略。一般使用interface+abstract+extends
/**
* key接口
*/
public interface KeyPrefix {
public int expireSeconds();//過期時間
public String getPrefix();
}
/**
* key的抽象類
* 抽象類是可以定義私有的變量和方法,不可以對象
* 接口裏面都是公有的
*/
public abstract class BasePrefix implements KeyPrefix{
private int expireSeconds;//過期時間
private String prefix;//key值
public BasePrefix(String prefix) {//0代表永不過期
this(0, prefix);
}
public BasePrefix( int expireSeconds, String prefix) {
this.expireSeconds = expireSeconds;
this.prefix = prefix;
}
public int expireSeconds() {//默認0代表永不過期
return expireSeconds;
}
public String getPrefix() {
String className = getClass().getSimpleName();
return className+":" + prefix;//類名+
}
}
/**
* 用戶類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");
}
4.編寫RedisService.java 提供redis服務,主要是get(),set(),exists(),序列化,反序列化,
註意使用redis鏈接後 一定要歸還到pool中。
@Service
public class RedisService {
//註解推薦使用@Resource,需要導包
@Autowired
JedisPool jedisPool;
/**
* 獲取當個對象
* */
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 <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);//設置key值,再設置一個過期時間
}
return true;
}finally {
returnToPool(jedis);
}
}
/**
* 判斷key是否存在
* */
public <T> boolean exists(KeyPrefix prefix, String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
//生成真正的key
String realKey = prefix.getPrefix() + key;
return jedis.exists(realKey);
}finally {
returnToPool(jedis);
}
}
/**
* 增加值
* */
public <T> Long incr(KeyPrefix prefix, String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
//生成真正的key
String realKey = prefix.getPrefix() + key;
return jedis.incr(realKey);
}finally {
returnToPool(jedis);
}
}
/**
* 減少值
* */
public <T> Long decr(KeyPrefix prefix, String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
//生成真正的key
String realKey = prefix.getPrefix() + key;
return jedis.decr(realKey);
}finally {
returnToPool(jedis);
}
}
/**
* 序列化
* @param value
* @param <T>
* @return
*/
private <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);
}
}
/**
* 反序列化
* @param str
* @param clazz
* @param <T>
* @return
*/
@SuppressWarnings("unchecked")
private <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();
}
}
}
5.在業務層調用 redisService,本篇從簡,在controller調用,只是做一個測試。
邏輯:先在redis判斷是否存在key,存在則讀取,否則從數據庫讀取,再存入redis。
執行修改後value要覆蓋原來的值
@Controller
@RequestMapping("/demo")
public class SampleController {
@Autowired
UserService userService;
@Autowired
RedisService redisService;
@RequestMapping("/redis/get")
@ResponseBody
public Result<User> redisGet() {
User user = redisService.get(UserKey.getById, ""+1, User.class);
return Result.success(user);
}
@RequestMapping("/redis/set")
@ResponseBody
public Result<Boolean> redisSet() {
User user = new User();
user.setId(1);
user.setName("1111");
redisService.set(UserKey.getById, ""+1, user);//UserKey:id1
return Result.success(true);
}
}
SpringBoot集成原生redis