Spring 4.0.3 spring-data-redis jedisの統合
10369 ワード
最近、プロジェクトはキャッシュシステムとしてredisを必要とし、多くの穴を登った.
まずpom.xml
spring-redis.xml
config.properties
RedisUtil.java
簡単な方法stringとlistしかできませんでした
後記:spring-data-redisとjedisのバージョン(ここで使用しているspring-data-redis 1.4.2とjedis 2.62)に注意してください.この2つの商品のバージョンの要求は厳しすぎます.私のプロジェクトはspring 4を使用しています.5をアップグレードしたいと思っていましたが、エラーが多すぎて諦めました.spring 5をアップグレードした後、このバージョンの問題はずっとよくなります.
感謝:https://www.cnblogs.com/hjwublog/p/5749929.html
まずpom.xml
org.springframework.data
spring-data-redis
1.4.2.RELEASE
redis.clients
jedis
2.6.2
spring-redis.xml
config.properties
#redis
redis.host=127.0.0.1
#redis , 6379
redis.port=6379
#redis
redis.pass=
# , maxIdle ,
redis.maxIdle=100
# , “ ”
redis.maxActive=300
# : ms
redis.maxWait=1000
# ,
redis.testOnBorrow=true
# , 0,
redis.timeout=10000
RedisUtil.java
簡単な方法stringとlistしかできませんでした
public class RedisUtil {
//https://blog.csdn.net/javaxiaibai0414/article/details/88666453
private static RedisUtil redisUtil = new RedisUtil();
private static RedisTemplate client = null; // , “RedisTemplate”
private static synchronized RedisTemplate getRedisUtil() {
if(client == null){
client = SpringContextHolder.getBean("redisTemplate");
}
return client;
}
private RedisUtil() {}
/**
* .
* @return
*/
public static RedisUtil getInstance() {
if(redisUtil == null){
synchronized (RedisUtil.class) {
redisUtil = new RedisUtil();
}
}
return redisUtil;
}
//-------------------------------------------------
//------------------ ----------------
/**
*
* @param key
* @param time
* @return
*/
public boolean expire(String key,long time){
try{
if(time>0){
getRedisUtil().expire(key,time,TimeUnit.SECONDS);
}
return true;
}catch (Exception e){
e.printStackTrace();
return false;
}
}
/**
* key
* @param key null
* @return 0
*/
public long getExpire(String key){
return getRedisUtil().getExpire(key);
}
/**
* key
* @param key
* @return
*/
public boolean hasKey(String key){
try{
return getRedisUtil().hasKey(key);
}catch (Exception e){
e.printStackTrace();
return false;
}
}
/**
*
* @param key
*/
@SuppressWarnings("unchecked")
public void del(String ... key){
if(key!=null&&key.length>0){
if (key.length ==1) {
getRedisUtil().delete(key[0]);
}else {
getRedisUtil().delete(CollectionUtils.arrayToList(key));
}
}
}
//-------------------------------------------------
//------------------String ----------
/**
*
* @param key
* @return
*/
public Object get(String key) {
return key==null?null:getRedisUtil().opsForValue().get(key);
}
/**
* ( )
* @param key
* @param value
* @return T F
*/
public boolean set(String key ,String value) {
try{
getRedisUtil().opsForValue().set(key,value);
return true;
}catch (Exception e){
e.printStackTrace();
return false;
}
}
/**
* -- token
* @param key
* @param value
* @param time time 0
* @return
*/
public boolean set(String key,String value,long time){
try {
if (time>0){
getRedisUtil().opsForValue().set(key,value,time,TimeUnit.SECONDS);
}else {
set(key,value);
}
return true;
}catch (Exception e){
e.printStackTrace();
return false;
}
}
/**
*
* @param key
* @param delta
* @return
*/
public long incr(String key,long delta){
if(delta<0){
throw new RuntimeException(" 0");
}
return getRedisUtil().opsForValue().increment(key,delta);
}
/**
*
* @param key
* @param delta
* @return
*/
public long decr(String key,long delta){
if(delta<0){
throw new RuntimeException(" 0");
}
return getRedisUtil().opsForValue().increment(key,-delta);
}
//-------------------------------------------------
//------------------LIST ------------
/**
* list
* @param key
* @param start
* @param end 0 -1
* @return
*/
public List
後記:spring-data-redisとjedisのバージョン(ここで使用しているspring-data-redis 1.4.2とjedis 2.62)に注意してください.この2つの商品のバージョンの要求は厳しすぎます.私のプロジェクトはspring 4を使用しています.5をアップグレードしたいと思っていましたが、エラーが多すぎて諦めました.spring 5をアップグレードした後、このバージョンの問題はずっとよくなります.
感謝:https://www.cnblogs.com/hjwublog/p/5749929.html