項目中使用到redis作為緩存,以前只簡單的瞭解和學習過,沒有在實際項目中使用,正好將redis系統的學習下然後集成在項目中使用。然後分享出來共同進步。對redis的操作,使用Spring提供的對redis的操作的jar包,它對redis操作進行高度封裝,能夠加快開發和使用,個人覺得還是很不錯。
1、Spring和redis的整合xml:
以上就是對redis和Spring的整合,整合過程中出現很多問題,最大的問題出現在jar包問題,調整好一會總算可以使用。
2、redis的加載資源文件:
3、項目使用redis作為緩存操作的封裝:
首先接口類:
對項目需求分析後抽出的接口層。
接口的具體實現(實現內容太多,不太好截圖,可能不太好讀:-D):
@Service
public class RedisCacheImpl implements RedisCache {
private static Logger logger = Logger.getLogger(RedisCacheImpl.class);
@Autowired
private RedisTemplate<String, String> redisTemplate;
/**
*
* @Description:
* @param key
* 鍵
* @param value
* 值
* @param time
* 有效期
* @return
* @throws Exception
* @time:2016-12-29 上午10:13:01
*/
public boolean cacheValue(String key, String value, long time)
throws Exception {
try {
ValueOperations<String, String> ops = redisTemplate.opsForValue();
ops.set(key, value);
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
throw new Exception("緩存[" + key + "]失敗, value[" + value + "],有效期["
+ time + "]");
}
}
@Override
public boolean cacheValue(String key, String value) throws Exception {
return cacheValue(key, value, -1);
}
/**
*
* @Description:根據鍵獲取緩存的值
* @param key
* @return
* @throws Exception
* @time:2016-12-29 上午10:14:34
*/
@Override
public String getCacheValue(String key) throws Exception {
String val = null;
try {
ValueOperations<String, String> ops = redisTemplate.opsForValue();
val = ops.get(key);
} catch (Exception e) {
logger.error("not find this key:"+key+",you can make sure this key is right!");
}
return val;
}
/*
* String 判斷緩存是否存在
*/
@Override
public boolean containsValueKey(String key) throws Exception {
return containsKey(key);
}
/*
* Set 判斷緩存是否存在
*/
@Override
public boolean containsSetKey(String key) throws Exception {
return containsKey(key);
}
/*
* List 判斷緩存是否存在
*/
@Override
public boolean containsListKey(String key) throws Exception {
return containsKey(key);
}
protected boolean containsKey(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Throwable t) {
logger.error("判斷緩存存在失敗key[" + key + ", error[" + t + "]");
}
return false;
}
/**
* 移除緩存
*
* @param key
* @return
*/
public boolean remove(String key) throws Exception {
try {
redisTemplate.delete(key);
return true;
} catch (Throwable t) {
logger.error("獲取緩存失敗key[" + key + ", error[" + t + "]");
}
return false;
}
/**
*
* @Description:緩存Set集合數據
* @param key
* @param v
* @param time
* @return
* @throws Exception
*/
@Override
public boolean cacheSetValue(String key, Set<String> v, long time)
throws Exception {
try {
SetOperations<String, String> setOps = redisTemplate.opsForSet();
setOps.add(key, v.toArray(new String[v.size()]));
if (time > 0)
redisTemplate.expire(key, time, TimeUnit.SECONDS);
return true;
} catch (Throwable t) {
logger.error("緩存[" + key + "]失敗, value[" + v + "]", t);
}
return false;
}
/**
*
* @Description:緩存Set數據
* @param key
* @param v
* @return
* @throws Exception
*/
@Override
public boolean cacheSetValue(String key, Set<String> v) throws Exception {
return cacheSetValue(key, v, -1);
}
/*
* 緩存Set數據,參數類型不同
*/
public boolean cacheSetValue(String key, String value, long time) {
try {
SetOperations<String, String> ops = redisTemplate.opsForSet();
ops.add(key, value);
if (time > 0)
redisTemplate.expire(key, time, TimeUnit.SECONDS);
return true;
} catch (Exception e) {
logger.error("緩存[" + key + "]失敗, value[" + value + "]", e);
}
return false;
}
public boolean cacheSetValue(String key, String value) {
return cacheSetValue(key, value, -1);
}
/**
*
* @Description:根據key獲取Set中的數據
* @param key
* @return
* @throws Exception
*/
@Override
public Set<String> getSetValues(String key) throws Exception {
try {
SetOperations<String, String> ops = redisTemplate.opsForSet();
return ops.members(key);
} catch (Throwable t) {
logger.error("獲取set緩存失敗key[" + key + ", error[" + t + "]");
}
return null;
}
/**
* @Description:獲取Set類型的大小
* @param key
* @return Long
* @throws Exception
*/
@Override
public Long getSetSize(String key) throws Exception {
return redisTemplate.opsForSet().size(key);
}
// ===========================================Map操作===========================
/**
*
* @Description:添加map
* @param key
* @param map
* @throws Exception
*/
@Override
public boolean cacheMapValue(String key, Map<?, ?> map) throws Exception {
try {
HashOperations<String, Object, Object> hashOps = redisTemplate.opsForHash();
hashOps.putAll(key, map);
return true;
} catch (Exception e) {
logger.error("緩存map失敗key[" + key + ", error[" + e.getMessage() + "]",e);
}
return false;
}
/**
*
* @Description:獲取map的所有鍵
* @param key
* @return
* @throws Exception
*/
@Override
public Set<?> getMapkeys(String key) throws Exception {
Set<?> keySet = null;
HashOperations<String, Object, Object> hashOps;
try {
hashOps = redisTemplate.opsForHash();
keySet = hashOps.keys(key);
} catch (Exception e) {
logger.error("根據key[" + key + "獲取Hash所有鍵值失敗,, error[" + e.getMessage() + "]",e);
}
return keySet;
}
/**
* @Description:獲取map
* @param key
* @return
* @throws Exception
*/
@Override
public Map<?, ?> getMap(String key) throws Exception {
Map<Object, Object> map = null;
try {
HashOperations<String, Object, Object> hashOps = redisTemplate.opsForHash();
map = hashOps.entries(key);
} catch (Exception e) {
logger.error("根據key[" + key + "獲取map失敗,, error[" + e.getMessage() + "]",e);
}
return map;
}
/**
*
* @Description:獲取map的長度
* @param key
* @return
* @throws Exception
*/
@Override
public long getMapkeysLength(String key) throws Exception {
try {
return redisTemplate.opsForHash().entries(key).size();
} catch (Exception e) {
logger.error("獲取Hash類型的key["+key+"]失敗",e);
return (Long) null;
}
}
/**
* @Description:獲取map中所有value數據
* @param key
* @return
* @throws Exception
*/
@Override
public List<?> getMapValues(String key) throws Exception {
try {
return redisTemplate.opsForHash().values(key);
} catch (Exception e) {
logger.error("獲取Hash類型的value["+key+"]失敗",e);
return null;
}
}
/**
* @Description:獲取map中指定字段的值
* @param key
* @param s1 字段名
* @param s2 字段名
* @return
* @throws Exception
*/
//@Override
//public List<String> getMapFieldValue(String key, String s1, String s2)
//throws Exception {
//redisTemplate.opsForHash().multiGet(arg0, arg1)
//
//return null;
//}
/**
*
* @Description:map刪除操作
* @param key
* @param s
* @return
* @throws Exception
*/
@Override
public boolean removeMap(String key, Object s) throws Exception {
try {
redisTemplate.opsForHash().delete(key, s);
return true;
} catch (Exception e) {
return false;
}
}
// =================================List 操作===========================
/**
*
* @Description:緩存list集合
* @param key
* @param list
* @return
* @throws Exception
*/
@Override
public boolean cacheListValue(String key, List<String> list)
throws Exception {
return cacheListValue(key, list, -1);
}
@Override
public boolean cacheListValue(String key, List<String> list, long time)
throws Exception {
try {
ListOperations<String, String> ops = redisTemplate.opsForList();
for(int i = 0; i < list.size(); i++){
long l = ops.rightPush(key, list.get(i));
}
if (time > 0)
redisTemplate.expire(key, time, TimeUnit.SECONDS);
return true;
} catch (Exception e) {
logger.error("緩存[" + key + "]失敗, value[" + list + "]", e);
}
return false;
}
@Override
public boolean cacheListValue(String key, String v) throws Exception {
return cacheListValue(key, v, -1);
}
/**
*
* @Description:緩存字符串
* @param key
* @param v
* @param time
* @return
* @throws Exception
*/
@Override
public boolean cacheListValue(String key, String v, long time)
throws Exception {
try {
ListOperations<String, String> listOps = redisTemplate.opsForList();
listOps.rightPush(key, v);
if (time > 0)
redisTemplate.expire(key, time, TimeUnit.SECONDS);
return true;
} catch (Exception t) {
logger.error("緩存[" + key + "]失敗, value[" + v + "]", t);
}
return false;
}
/**
*
* @Description:獲取某一鍵的某一範圍的數據
* @param key
* @param start
* @param end
* @return
* @throws Exception
*/
@Override
public List<String> lrange(String key, Long start, Long end)
throws Exception {
try {
ListOperations<String, String> ops = redisTemplate.opsForList();
return ops.range(key, start, end);
} catch (Exception e) {
logger.error("獲取list緩存失敗key[" + key + ", error["+ e.getMessage() + "]");
}
return null;
}
/**
*
* @Description:返回指定鍵的list大小
* @param key
* @return
* @throws Exception
*/
public long getListSize(String key) throws Exception{
try {
return redisTemplate.opsForList().size(key);
} catch (Exception e) {
logger.error("獲取list長度失敗key[" + key + "], error[" + e + "]");
}
return 0;
}
/**
*
* @Description:移除List的key
* @param key
* @return
* @throws Exception
*/
@Override
public boolean removeList(String key) throws Exception {
try {
redisTemplate.opsForList().rightPop(key);
return true;
} catch (Exception e) {
logger.error("移除list緩存失敗,key[" + key + ", error[" + e.getMessage()
+ "]");
}
return false;
}
/**
*
* @Description:在key 後追加
* @param key
* @param value
* @return
* @throws Exception
*/
@Override
public boolean append(String key, String value) throws Exception {
try {
redisTemplate.opsForValue().append(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
上面就是抽離出的接口具體實現,redis真正使用還需要安裝redis服務。更改上自己的信息就可以查詢,redis緩存服務器也可以使用客戶端RedisDesktopManager來查看redis裡的數據。通過命令的話可以查一下相關命令學習使用。
總結:由於系統的數據量比較大,交互比較頻繁,數據更新頻率快,所以考慮redis緩存服務,它的性能和效率等都比較出色,在企業中使用也很多。在實際學習可以自己搭建下redis的高可用的集群服務、共享session等操作,學習永無止境,且行且努力!