《redis与Spring的整合及对redis的简单操作封装》要点:
本文介绍了redis与Spring的整合及对redis的简单操作封装,希望对您有用。如果有疑问,可以联系我们。
项目中使用到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等操作,学习永无止境,且行且努力!
《redis与Spring的整合及对redis的简单操作封装》是否对您有启发,欢迎查看更多与《redis与Spring的整合及对redis的简单操作封装》相关教程,学精学透。维易PHP学院为您提供精彩教程。
转载请注明本页网址:
http://www.vephp.com/jiaocheng/9247.html