SSM框架中使用Redis單機版
阿新 • • 發佈:2018-12-07
上一篇文章就說要嘗試在程式碼中實現Redis使用,但是有一段時間很(tou)忙(lan),所以現在才寫。
Maven依賴
<!-- Redis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency >
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.1.RELEASE</version>
</dependency>
配置檔案
config.properties:
#Redis config
redis.host=localhost
redis.port=6379
spring-jedis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
<!-- 連線池配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大連線數 -->
<property name="maxTotal" value="30" />
<!-- 最大空閒連線數 -->
<property name="maxIdle" value="10" />
<!-- 每次釋放連線的最大數目 -->
<property name="numTestsPerEvictionRun" value="1024" />
<!-- 釋放連線的掃描間隔(毫秒) -->
<property name="timeBetweenEvictionRunsMillis" value="30000" />
<!-- 連線最小空閒時間 -->
<property name="minEvictableIdleTimeMillis" value="1800000" />
<!-- 連線空閒多久後釋放, 當空閒時間>該值 且 空閒連線>最大空閒連線數 時直接釋放 -->
<property name="softMinEvictableIdleTimeMillis" value="10000" />
<!-- 獲取連線時的最大等待毫秒數,小於零:阻塞不確定的時間,預設-1 -->
<property name="maxWaitMillis" value="1500" />
<!-- 在獲取連線的時候檢查有效性, 預設false -->
<property name="testOnBorrow" value="true" />
<!-- 在空閒時檢查有效性, 預設false -->
<property name="testWhileIdle" value="true" />
<!-- 連線耗盡時是否阻塞, false報異常,ture阻塞直到超時, 預設true -->
<property name="blockWhenExhausted" value="false" />
</bean>
<!-- jedis客戶端單機版 -->
<bean id="redisClient" class="redis.clients.jedis.JedisPool">
<constructor-arg name="host" value="${redis.host}"/>
<constructor-arg name="port" value="${redis.port}"/>
<constructor-arg name="poolConfig" ref="jedisPoolConfig"/>
</bean>
</beans>
配置簡單的API
演示簡單的使用,所以很多異常狀態沒有設定。
建立一個介面和介面實現類。
IJedisClientService:
package cn.sevenyuan.demo.cache;
/**
* Created by JingQ on 2017/9/15.
*/
public interface IJedisClientService {
String get(String key);
String set(String key, String value);
String hget(String hkey, String key);
boolean exist(String key);
long hset(String hkey, String key, String value);
long incr(String key);
long expire(String key, int second);
long ttl(String key);
long del(String key);
long hdel(String hkey, String key);
}
JedsiClientService:
package cn.sevenyuan.demo.cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
/**
* Created by JingQ on 2017/9/15.
*/
@Service
public class JedisClientService implements IJedisClientService{
@Autowired
private JedisPool jedisPool;
@Override
public String get(String key) {
Jedis jedis = jedisPool.getResource();
String string = jedis.get(key);
jedis.close();
return string;
}
@Override
public String set(String key, String value) {
Jedis jedis = jedisPool.getResource();
String string = jedis.set(key, value);
jedis.close();
return string;
}
@Override
public String hget(String hkey, String key) {
Jedis jedis = jedisPool.getResource();
String string = jedis.hget(hkey, key);
jedis.close();
return string;
}
@Override
public boolean exist(String key) {
Jedis jedis = jedisPool.getResource();
boolean result = jedis.exists(key);
jedis.close();
return result;
}
@Override
public long hset(String hkey, String key, String value) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.hset(hkey, key, value);
jedis.close();
return result;
}
@Override
public long incr(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.incr(key);
jedis.close();
return result;
}
@Override
public long expire(String key, int second) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.expire(key, second);
jedis.close();
return result;
}
@Override
public long ttl(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.ttl(key);
jedis.close();
return result;
}
@Override
public long del(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.del(key);
jedis.close();
return result;
}
@Override
public long hdel(String hkey, String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.hdel(hkey, key);
jedis.close();
return result;
}
}
進行呼叫
demo使用的是mysql作為最終結果儲存,redis作為快取呼叫,一般的套路是查詢到的結果放進redis中,設定過期時間,在資料更新或者刪除,將這個key值刪掉,重新設定。
例:圖書的增刪查改
package cn.sevenyuan.demo.service.impl;
import cn.sevenyuan.demo.cache.IJedisClientService;
import cn.sevenyuan.demo.dao.IBookDao;
import cn.sevenyuan.demo.domain.Book;
import cn.sevenyuan.demo.service.IBookService;
import cn.sevenyuan.demo.utils.RedisKeyUtils;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import java.util.Collections;
import java.util.List;
/**
* RedisKeyUtils是自己寫的key值生成器,按照個人命名習慣去寫
* Created by JingQ on 2017/9/5.
*/
@Service("bookService")
public class BookService implements IBookService {
private final String className = "BookService";
@Autowired
private IBookDao bookDao;
@Autowired
private IJedisClientService jedisClientService;
@Override
public Book selectBookById(Integer id) {
String key = RedisKeyUtils.generateKey(className, "book", String.valueOf(id));
boolean isExist = jedisClientService.exist(key);
Book book = null;
if (isExist){
String result = jedisClientService.get(key);
book = JSONObject.parseObject(result, Book.class);
}else {
book = bookDao.selectById(id);
if (!ObjectUtils.isEmpty(book)){
jedisClientService.set(key, JSONObject.toJSON(book).toString());
}
}
return book;
}
@Override
public int insert(Book book) {
if (ObjectUtils.isEmpty(book)){
return 0;
}
return bookDao.insert(book);
}
@Override
public int deleteByPrimaryKey(Integer id) {
String key = RedisKeyUtils.generateKey(className, "book", String.valueOf(id));
if (jedisClientService.exist(key)){
jedisClientService.del(key);
}
return bookDao.deleteByPrimaryId(id);
}
@Override
public int updateByPrimaryKey(Book book) {
if (ObjectUtils.isEmpty(book)){
return 0;
}
String key = RedisKeyUtils.generateKey(className, "book", String.valueOf(book.getBookId()));
if (jedisClientService.exist(key)){
jedisClientService.del(key);
}
return bookDao.updateByPrimaryId(book);
}
@Override
public List<Book> selectBookByIds(List<Integer> list) {
if (CollectionUtils.isEmpty(list)){
Collections.emptyList();
}
return bookDao.selectByIds(list);
}
}
單機版使用基本就是這個套路了,最近還在看ZooKeeper和簡單的jvm,之後也會記錄一下學習的內容~