Java Redis实现session共享
1 package com.hzlg.ui.util; 2 3 import javax.annotation.Resource; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Component; 7 8 import redis.clients.jedis.Jedis; 9 import redis.clients.jedis.JedisPool; 10 @Component 11 public class RedisUtil { 12 13 /** 14 * redis 连接池,这里 jedisPool 我们再之前 spring 配置中配置好了,交给 spring 管理,这里可以自动注入 15 */ 16 @Autowired 17 private JedisPool jedisPool; 18 19 public void setPool(JedisPool jedisPool) { 20 this.jedisPool = jedisPool; 21 } 22 /** 23 * 获取 jedis 24 * @return 25 */ 26 public Jedis getResource(){ 27 Jedis jedis =null; 28 try { 29 jedis =jedisPool.getResource(); 30 } catch (Exception e) { 31 CommonUtils.logger.info(e); 32 CommonUtils.logger.info("can't get the redis resource"); 33 } 34 return jedis; 35 } 36 /** 37 * 关闭连接 38 * @param jedis 39 */ 40 public void disconnect(Jedis jedis){ 41 jedis.disconnect(); 42 } 43 /** 44 * 将 jedis 返还连接池 45 * @param jedis 46 */ 47 public void returnResource(Jedis jedis){ 48 if(null != jedis){ 49 try { 50 jedisPool.returnResource(jedis); 51 } catch (Exception e) { 52 CommonUtils.logger.info(e); 53 CommonUtils.logger.info("can't return jedis to jedisPool"); 54 } 55 } 56 } 57 /** 58 * 无法返还 jedispool,释放 jedis 客户端对象, 59 * @param jedis 60 */ 61 public void brokenResource(Jedis jedis){ 62 if (jedis!=null) { 63 try { 64 jedisPool.returnBrokenResource(jedis); 65 } catch (Exception e) { 66 CommonUtils.logger.info(e); 67 CommonUtils.logger.info("can't release jedis Object"); 68 } 69 } 70 } 71 }
package com.hzlg.ui.service;import java.util.Map;
public interface RedisCacheStorageService<K,V> {
/
* 在 redis 数据库中插入 key 和 value
* @param key
* @param value
* @return
*/
boolean set(K key,V value);
/
* 在 redis 数据库中插入 key 和 value 并且设置过期时间
* @param key
* @param value
* @param exp 过期时间
* @return
/
boolean set(K key, V value, int exp);
/**
* 根据 key 去 redis 中获取 value
* @param key
* @return
/
V get(K key,Object object);
/
* 删除 redis 库中的数据
* @param key
* @return
*/
boolean remove(K key);
/
* 设置哈希类型数据到 redis 数据库
* @param cacheKey 可以看做一张表
* @param key 表字段
* @param value
* @return
/
boolean hset(String cacheKey,K key,V value);
/**
* 获取哈希表数据类型的值
* @param cacheKey
* @param key
* @return
/
V hget(String cacheKey,K key,Object object);
/
* 获取哈希类型的数据
* @param cacheKey
* @return
*/
Map<K,V> hget(String cacheKey,Object object);
/
* 该条记录在 redis 中是否存在
* /
boolean exists(String sessionid);
}
package com.hzlg.ui.service.impl;import java.util.HashMap;
import java.util.Map;import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisException;import com.hzlg.ui.entity.UserInfo;
import com.hzlg.ui.service.RedisCacheStorageService;
import com.hzlg.ui.util.CommonUtils;
import com.hzlg.ui.util.RedisUtil;@Service("redisCacheStorageServiceImpl")
public class RedisCacheStorageServiceImpl<V> implements RedisCacheStorageService<String,V>{</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 默认过时时间 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> <span style="color: rgba(0, 0, 255, 1)">int</span> EXPRIE_TIME =3600*24<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 获取Jedis相关操作 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> @Autowired </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> RedisUtil redisUtil; @Override </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> set(String key, V value) { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> set(key,value,EXPRIE_TIME); } @Override </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span> set(String key, V value, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> exp) { Jedis jedis</span>=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(StringUtils.isEmpty(key)){ </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取jedis对象</span> jedis=<span style="color: rgba(0, 0, 0, 1)"> redisUtil.getResource(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">使用对象转换为Json格式插入redis</span> JSONObject json = JSONObject.fromObject(value);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将java对象转换为json对象</span> String jsonValue = json.toString();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将json对象转换为json字符串</span>
jedis.setex(key,exp,jsonValue);
}catch (Exception e){
CommonUtils.logger.info(e);
//释放 jedis 对象
redisUtil.brokenResource(jedis);
CommonUtils.logger.info("client can't connect server");
return false;
}finally {
redisUtil.returnResource(jedis);//返还连接池
return true;
}
}@Override </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> V get(String key,Object object) { Jedis jedis</span>=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; V v</span>=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(StringUtils.isEmpty(key)){ CommonUtils.logger.info(</span>"redis取值,key为空"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">{ jedis</span>=redisUtil.getResource(); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取连接</span> String jsonValue=jedis.get(key); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">从redis得到值,得到的是json字符串,因为我们之前插入的时候是使用的json字符串</span> <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(StringUtils.isEmpty(jsonValue)){ </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; } JSONObject obj </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> JSONObject().fromObject(jsonValue);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将json字符串转换为json对象</span> v = (V)JSONObject.toBean(obj,object.getClass());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将建json对象转换为你想要的java对象</span> <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> v; }</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e){ CommonUtils.logger.info(e); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">释放jedis对象</span> <span style="color: rgba(0, 0, 255, 1)">if</span>(jedis!=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){ redisUtil.brokenResource(jedis); } CommonUtils.logger.info(</span>"client can't get value"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; }</span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返还连接池</span>
redisUtil.returnResource(jedis);
} } @Override </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> remove(String key) { Jedis jedis</span>=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">{ jedis</span>=<span style="color: rgba(0, 0, 0, 1)">redisUtil.getResource(); </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(StringUtils.isEmpty(key)){ CommonUtils.logger.info(</span>"redis取值,key为空"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">; } jedis.del(key); }</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) { CommonUtils.logger.info(e); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">释放jedis对象</span> <span style="color: rgba(0, 0, 255, 1)">if</span>(jedis!=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){ redisUtil.brokenResource(jedis); } CommonUtils.logger.info(</span>" del fail from redis"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">; }</span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)">{ </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返还连接池</span>
redisUtil.returnResource(jedis);
return true;
}} @Override </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> hset(String cacheKey, String key, V value) { Jedis jedis </span>=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将key 和value 转换成 json 对象</span> JSONObject json = JSONObject.fromObject(cacheKey);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将java对象转换为json对象</span> String jCacheKey = json.toString();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将json对象转换为json字符串</span> JSONObject json2 = JSONObject.fromObject(value);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将java对象转换为json对象</span> String jsonValue = json2.toString();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将json对象转换为json字符串 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">操作是否成功</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span> isSucess =<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(StringUtils.isEmpty(jCacheKey)){ CommonUtils.logger.info(</span>"cacheKey is empty"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { jedis </span>=<span style="color: rgba(0, 0, 0, 1)">redisUtil.getResource(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">执行插入哈希</span>
jedis.hset(jCacheKey, key, jsonValue);
} catch (Exception e) {
CommonUtils.logger.info("client can't connect server");
isSucess =false;
if(null !=jedis){
//释放 jedis 对象
redisUtil.brokenResource(jedis);
}
return false;
}finally{
if (isSucess) {
//返还连接池
redisUtil.returnResource(jedis);
}
return true;
}
}v = (V)JSONObject.toBean(obj,object.getClass());//将建 json 对象转换为 java 对象 } //返还连接池 redisUtil.returnResource(jedis); } catch (JedisException e) { CommonUtils.logger.info("client can't connect server"); if(null !=jedis){ //redisUtil 对象 redisUtil.brokenResource(jedis); } } return v; }@Override </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> V hget(String cacheKey, String key,Object object) { Jedis jedis </span>=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; V v </span>=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; JSONObject json </span>= JSONObject.fromObject(cacheKey);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将java对象转换为json对象</span> String jCacheKey = json.toString();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将json对象转换为json字符串</span> <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(StringUtils.isEmpty(jCacheKey)){ CommonUtils.logger.info(</span>"cacheKey is empty"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取客户端对象</span> jedis =<span style="color: rgba(0, 0, 0, 1)">redisUtil.getResource(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">执行查询</span> String jsonValue =<span style="color: rgba(0, 0, 0, 1)"> jedis.hget(jCacheKey, key); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">判断值是否非空</span> <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(StringUtils.isEmpty(jsonValue)){ </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{ JSONObject obj </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> JSONObject().fromObject(jsonValue);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将json字符串转换为json对象</span>
result.put(entry.getKey(), v);} } } catch (JedisException e) { CommonUtils.logger.info("client can't connect server"); if(null !=jedis){ //释放 jedis 对象 redisUtil.brokenResource(jedis); } } return result; }@Override </span><span style="color: rgba(0, 0, 255, 1)">public</span> Map<String, V><span style="color: rgba(0, 0, 0, 1)"> hget(String cacheKey,Object object) { JSONObject json </span>= JSONObject.fromObject(cacheKey);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将java对象转换为json对象</span> String jCacheKey = json.toString();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将json对象转换为字符串 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">非空校验</span> <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(StringUtils.isEmpty(jCacheKey)){ CommonUtils.logger.info(</span>"cacheKey is empty!"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; } Jedis jedis </span>=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; Map</span><String,V> result =<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; V v</span>=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { jedis </span>=<span style="color: rgba(0, 0, 0, 1)">redisUtil.getResource(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取列表集合 因为插入redis的时候是jsonString格式,所以取出来key是String value也是String</span> Map<String,String> map =<span style="color: rgba(0, 0, 0, 1)"> jedis.hgetAll(jCacheKey); </span><span style="color: rgba(0, 0, 255, 1)">if</span>(<span style="color: rgba(0, 0, 255, 1)">null</span> !=<span style="color: rgba(0, 0, 0, 1)">map){ </span><span style="color: rgba(0, 0, 255, 1)">for</span>(Map.Entry<String, String><span style="color: rgba(0, 0, 0, 1)"> entry : map.entrySet()){ </span><span style="color: rgba(0, 0, 255, 1)">if</span>(result ==<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){ result </span>=<span style="color: rgba(0, 0, 255, 1)">new</span> HashMap<String,V><span style="color: rgba(0, 0, 0, 1)">(); } JSONObject obj </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> JSONObject().fromObject(entry.getValue());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将json字符串转换为json对象</span> v = (V)JSONObject.toBean(obj,object.getClass());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将建json对象转换为java对象</span>
@Override </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> exists(String sessionid) { UserInfo userInfo</span>=(UserInfo) get(sessionid,<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> UserInfo()); </span><span style="color: rgba(0, 0, 255, 1)">if</span>(userInfo!=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){ </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">; }
}
package com.hzlg.ui.service.impl;import java.util.HashMap;
import java.util.Map;import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisException;import com.hzlg.ui.entity.UserInfo;
import com.hzlg.ui.service.RedisCacheStorageService;
import com.hzlg.ui.util.CommonUtils;
import com.hzlg.ui.util.RedisUtil;@Service("redisCacheStorageServiceImpl")
public class RedisCacheStorageServiceImpl<V> implements RedisCacheStorageService<String,V>{</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 默认过时时间 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> <span style="color: rgba(0, 0, 255, 1)">int</span> EXPRIE_TIME =3600*24<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 获取Jedis相关操作 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> @Autowired </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> RedisUtil redisUtil; @Override </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> set(String key, V value) { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> set(key,value,EXPRIE_TIME); } @Override </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span> set(String key, V value, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> exp) { Jedis jedis</span>=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(StringUtils.isEmpty(key)){ </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取jedis对象</span> jedis=<span style="color: rgba(0, 0, 0, 1)"> redisUtil.getResource(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">使用对象转换为Json格式插入redis</span> JSONObject json = JSONObject.fromObject(value);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将java对象转换为json对象</span> String jsonValue = json.toString();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将json对象转换为json字符串</span>
jedis.setex(key,exp,jsonValue);
}catch (Exception e){
CommonUtils.logger.info(e);
//释放 jedis 对象
redisUtil.brokenResource(jedis);
CommonUtils.logger.info("client can't connect server");
return false;
}finally {
redisUtil.returnResource(jedis);//返还连接池
return true;
}
}@Override </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> V get(String key,Object object) { Jedis jedis</span>=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; V v</span>=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(StringUtils.isEmpty(key)){ CommonUtils.logger.info(</span>"redis取值,key为空"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">{ jedis</span>=redisUtil.getResource(); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取连接</span> String jsonValue=jedis.get(key); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">从redis得到值,得到的是json字符串,因为我们之前插入的时候是使用的json字符串</span> <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(StringUtils.isEmpty(jsonValue)){ </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; } JSONObject obj </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> JSONObject().fromObject(jsonValue);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将json字符串转换为json对象</span> v = (V)JSONObject.toBean(obj,object.getClass());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将建json对象转换为你想要的java对象</span> <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> v; }</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e){ CommonUtils.logger.info(e); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">释放jedis对象</span> <span style="color: rgba(0, 0, 255, 1)">if</span>(jedis!=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){ redisUtil.brokenResource(jedis); } CommonUtils.logger.info(</span>"client can't get value"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; }</span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返还连接池</span>
redisUtil.returnResource(jedis);
} } @Override </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> remove(String key) { Jedis jedis</span>=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">{ jedis</span>=<span style="color: rgba(0, 0, 0, 1)">redisUtil.getResource(); </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(StringUtils.isEmpty(key)){ CommonUtils.logger.info(</span>"redis取值,key为空"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">; } jedis.del(key); }</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) { CommonUtils.logger.info(e); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">释放jedis对象</span> <span style="color: rgba(0, 0, 255, 1)">if</span>(jedis!=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){ redisUtil.brokenResource(jedis); } CommonUtils.logger.info(</span>" del fail from redis"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">; }</span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)">{ </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返还连接池</span>
redisUtil.returnResource(jedis);
return true;
}} @Override </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> hset(String cacheKey, String key, V value) { Jedis jedis </span>=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将key 和value 转换成 json 对象</span> JSONObject json = JSONObject.fromObject(cacheKey);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将java对象转换为json对象</span> String jCacheKey = json.toString();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将json对象转换为json字符串</span> JSONObject json2 = JSONObject.fromObject(value);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将java对象转换为json对象</span> String jsonValue = json2.toString();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将json对象转换为json字符串 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">操作是否成功</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span> isSucess =<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(StringUtils.isEmpty(jCacheKey)){ CommonUtils.logger.info(</span>"cacheKey is empty"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { jedis </span>=<span style="color: rgba(0, 0, 0, 1)">redisUtil.getResource(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">执行插入哈希</span>
jedis.hset(jCacheKey, key, jsonValue);
} catch (Exception e) {
CommonUtils.logger.info("client can't connect server");
isSucess =false;
if(null !=jedis){
//释放 jedis 对象
redisUtil.brokenResource(jedis);
}
return false;
}finally{
if (isSucess) {
//返还连接池
redisUtil.returnResource(jedis);
}
return true;
}
}v = (V)JSONObject.toBean(obj,object.getClass());//将建 json 对象转换为 java 对象 } //返还连接池 redisUtil.returnResource(jedis); } catch (JedisException e) { CommonUtils.logger.info("client can't connect server"); if(null !=jedis){ //redisUtil 对象 redisUtil.brokenResource(jedis); } } return v; }@Override </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> V hget(String cacheKey, String key,Object object) { Jedis jedis </span>=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; V v </span>=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; JSONObject json </span>= JSONObject.fromObject(cacheKey);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将java对象转换为json对象</span> String jCacheKey = json.toString();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将json对象转换为json字符串</span> <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(StringUtils.isEmpty(jCacheKey)){ CommonUtils.logger.info(</span>"cacheKey is empty"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取客户端对象</span> jedis =<span style="color: rgba(0, 0, 0, 1)">redisUtil.getResource(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">执行查询</span> String jsonValue =<span style="color: rgba(0, 0, 0, 1)"> jedis.hget(jCacheKey, key); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">判断值是否非空</span> <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(StringUtils.isEmpty(jsonValue)){ </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{ JSONObject obj </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> JSONObject().fromObject(jsonValue);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将json字符串转换为json对象</span>
result.put(entry.getKey(), v);} } } catch (JedisException e) { CommonUtils.logger.info("client can't connect server"); if(null !=jedis){ //释放 jedis 对象 redisUtil.brokenResource(jedis); } } return result; }@Override </span><span style="color: rgba(0, 0, 255, 1)">public</span> Map<String, V><span style="color: rgba(0, 0, 0, 1)"> hget(String cacheKey,Object object) { JSONObject json </span>= JSONObject.fromObject(cacheKey);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将java对象转换为json对象</span> String jCacheKey = json.toString();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将json对象转换为字符串 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">非空校验</span> <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(StringUtils.isEmpty(jCacheKey)){ CommonUtils.logger.info(</span>"cacheKey is empty!"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; } Jedis jedis </span>=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; Map</span><String,V> result =<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; V v</span>=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { jedis </span>=<span style="color: rgba(0, 0, 0, 1)">redisUtil.getResource(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取列表集合 因为插入redis的时候是jsonString格式,所以取出来key是String value也是String</span> Map<String,String> map =<span style="color: rgba(0, 0, 0, 1)"> jedis.hgetAll(jCacheKey); </span><span style="color: rgba(0, 0, 255, 1)">if</span>(<span style="color: rgba(0, 0, 255, 1)">null</span> !=<span style="color: rgba(0, 0, 0, 1)">map){ </span><span style="color: rgba(0, 0, 255, 1)">for</span>(Map.Entry<String, String><span style="color: rgba(0, 0, 0, 1)"> entry : map.entrySet()){ </span><span style="color: rgba(0, 0, 255, 1)">if</span>(result ==<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){ result </span>=<span style="color: rgba(0, 0, 255, 1)">new</span> HashMap<String,V><span style="color: rgba(0, 0, 0, 1)">(); } JSONObject obj </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> JSONObject().fromObject(entry.getValue());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将json字符串转换为json对象</span> v = (V)JSONObject.toBean(obj,object.getClass());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将建json对象转换为java对象</span>
@Override </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> exists(String sessionid) { UserInfo userInfo</span>=(UserInfo) get(sessionid,<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> UserInfo()); </span><span style="color: rgba(0, 0, 255, 1)">if</span>(userInfo!=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){ </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">; }
}
applicationContext.xml 配置
<context:property-placeholder location="classpath*:/application.properties" ignore-resource-not-found="true" ignore-unresolvable="true" /> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="500"/><!-- 最大连接数 --> <property name="maxIdle" value="100"/><!-- 最大闲置 --> <property name="minIdle" value="10"/><!-- 最小闲置 --> <property name="maxWaitMillis" value="5000"/><!-- 最大等待 --> <property name="testOnBorrow" value="true"/><!-- 可以获取 --> </bean> <bean id="jedisPool" class="redis.clients.jedis.JedisPool"> <constructor-arg index="0" ref="jedisPoolConfig"/> <constructor-arg index="2" value="6379"/><!-- 端口 --> <constructor-arg index="3" value="5000"/><!-- 超时 --> <constructor-arg index="1" value="127.0.0.1"/><!-- Redis IP 地址 --> <constructor-arg index="4" value="123456"/><!-- 密码 --> </bean>
application.properties
#-------Redis--------
redis.pool.maxTotal=1000
redis.pool.maxIdle=200
redis.pool.maxWaitMillis=2000
redis.pool.testOnBorrow=true
redis.host=127.0.0.1
redis.password=123456
redis.port=6379
所需 jar 包
commons-pool2-2.4.2-javadoc.jar,commons-pool2-2.4.2.jar,jedis-2.7.2.jar,spring-data-redis-2.0.9.RELEASE.jar,spring-session-1.2.1.RELEASE.jar