当前位置:   article > 正文

使用spring-data-redis进行对redis的操作,封装的一些操作方法_基于spring-data-redis3封装的

基于spring-data-redis3封装的

   这个算是工作笔记吧,因为是我的实际工作内容

   spring-data-redis api地址  http://docs.spring.io/spring-data/redis/docs/current/api/

   

  依赖maven包(当前spring-data-redis的最新版本是1.7.2.RELEASE, jedis的最新版本是2.9.0):

spring-data-redis的maven仓库地址是: https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis

jedis的maven仓库地址是: https://mvnrepository.com/artifact/redis.clients/jedis

 

  1. <!-- spring-data -->
  2. <dependency>
  3. <groupId>org.springframework.data</groupId>
  4. <artifactId>spring-data-redis</artifactId>
  5. <version>1.6.4.RELEASE</version>
  6. </dependency>
  7. <!-- redis客户端 -->
  8. <dependency>
  9. <groupId>redis.clients</groupId>
  10. <artifactId>jedis</artifactId>
  11. <version>2.5.2</version>
  12. </dependency>

 

 

 

spring-data-redis的配置文件:

 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:util="http://www.springframework.org/schema/util"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
  8. <util:properties id="redisProps" location="classpath:redis.properties"/>
  9. <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" id="stringRedisSerializer"/>
  10. <bean class="org.springframework.data.redis.serializer.JacksonJsonRedisSerializer" id="jacksonJsonRedisSerializer">
  11. <constructor-arg type="java.lang.Class" value="java.lang.Object"/>
  12. </bean>
  13. <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"
  14. id="jdkSerializationRedisSerializer"/>
  15. <!-- <bean class="org.codehaus.jackson.map.ObjectMapper" id="jackObjMapper"/> -->
  16. <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
  17. <property name="maxTotal" value="#{redisProps['redis.pool.maxActive']}"/>
  18. <property name="maxIdle" value="#{redisProps['redis.pool.maxIdle']}"/>
  19. <property name="maxWaitMillis" value="#{redisProps['redis.pool.maxWait']}"/>
  20. <property name="testOnBorrow" value="#{redisProps['redis.pool.testOnBorrow']}"/>
  21. </bean>
  22. <bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
  23. p:hostName="#{redisProps['redis.ip']}"
  24. p:port="#{redisProps['redis.port']}"
  25. p:timeout="#{redisProps['redis.timeout']}"
  26. p:password="#{redisProps['redis.password']}"
  27. p:database="#{redisProps['redis.database']}"
  28. p:poolConfig-ref="jedisPoolConfig"
  29. p:usePool="true"/>
  30. <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
  31. p:connectionFactory-ref="redisConnectionFactory"
  32. p:keySerializer-ref="stringRedisSerializer"
  33. p:valueSerializer-ref="jacksonJsonRedisSerializer"
  34. p:hashKeySerializer-ref="stringRedisSerializer"
  35. p:hashValueSerializer-ref="jacksonJsonRedisSerializer"/>
  36. <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
  37. <constructor-arg ref="jedisPoolConfig"/>
  38. <constructor-arg value="#{redisProps['redis.ip']}"/>
  39. <constructor-arg type="int" value="#{redisProps['redis.port']}"/>
  40. <constructor-arg type="int" value="#{redisProps['redis.timeout']}"/>
  41. <constructor-arg type="java.lang.String" value="#{redisProps['redis.password']}"/>
  42. <constructor-arg type="int" value="#{redisProps['redis.database']}"/>
  43. </bean>
  44. </beans>

 

 

 

redis.properties的文件内容如下,maven管理的话,可以分别设置开发环境,测试环境,预发布环境,生产环境等

 

  1. redis.pool.maxActive=1024
  2. redis.pool.maxIdle=200
  3. redis.pool.maxWait=1000
  4. redis.pool.testOnBorrow=true
  5. redis.pool.testOnReturn=true
  6. #database
  7. redis.ip=192.168.0.246
  8. redis.port=6378
  9. redis.password=
  10. redis.timeout=5000
  11. redis.database=1

 

 

 

工具类如下:

 

  1. package com.yjy.dg.app.report.dao;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.data.redis.core.ListOperations;
  6. import org.springframework.data.redis.core.RedisTemplate;
  7. import org.springframework.data.redis.core.SetOperations;
  8. import org.springframework.data.redis.core.ValueOperations;
  9. import java.util.List;
  10. import java.util.Set;
  11. import java.util.concurrent.TimeUnit;
  12. /**
  13. * ParentDao 操作字符串redis缓存方法
  14. * list中的操作全是按照right方式
  15. *
  16. * @author littlehow
  17. * @time 2016-08-12 09:02
  18. */
  19. public class RedisParentDao {
  20. /**
  21. * 日志记录
  22. */
  23. private Logger logger = LoggerFactory.getLogger(this.getClass());
  24. @Autowired
  25. protected RedisTemplate<String, String> redisTemplate;
  26. /**
  27. * 前缀
  28. */
  29. public static final String KEY_PREFIX_VALUE = "dg:report:value:";
  30. public static final String KEY_PREFIX_SET = "dg:report:set:";
  31. public static final String KEY_PREFIX_LIST = "dg:report:list:";
  32. /**
  33. * 缓存value操作
  34. * @param k
  35. * @param v
  36. * @param time
  37. * @return
  38. */
  39. protected boolean cacheValue(String k, String v, long time) {
  40. String key = KEY_PREFIX_VALUE + k;
  41. try {
  42. ValueOperations<String, String> valueOps = redisTemplate.opsForValue();
  43. valueOps.set(key, v);
  44. if (time > 0) redisTemplate.expire(key, time, TimeUnit.SECONDS);
  45. return true;
  46. } catch (Throwable t) {
  47. logger.error("缓存[" + key + "]失败, value[" + v + "]", t);
  48. }
  49. return false;
  50. }
  51. /**
  52. * 缓存value操作
  53. * @param k
  54. * @param v
  55. * @return
  56. */
  57. protected boolean cacheValue(String k, String v) {
  58. return cacheValue(k, v, -1);
  59. }
  60. /**
  61. * 判断缓存是否存在
  62. * @param k
  63. * @return
  64. */
  65. protected boolean containsValueKey(String k) {
  66. return containsKey(KEY_PREFIX_VALUE + k);
  67. }
  68. /**
  69. * 判断缓存是否存在
  70. * @param k
  71. * @return
  72. */
  73. protected boolean containsSetKey(String k) {
  74. return containsKey(KEY_PREFIX_SET + k);
  75. }
  76. /**
  77. * 判断缓存是否存在
  78. * @param k
  79. * @return
  80. */
  81. protected boolean containsListKey(String k) {
  82. return containsKey(KEY_PREFIX_LIST + k);
  83. }
  84. protected boolean containsKey(String key) {
  85. try {
  86. return redisTemplate.hasKey(key);
  87. } catch (Throwable t) {
  88. logger.error("判断缓存存在失败key[" + key + ", error[" + t + "]");
  89. }
  90. return false;
  91. }
  92. /**
  93. * 获取缓存
  94. * @param k
  95. * @return
  96. */
  97. protected String getValue(String k) {
  98. try {
  99. ValueOperations<String, String> valueOps = redisTemplate.opsForValue();
  100. return valueOps.get(KEY_PREFIX_VALUE + k);
  101. } catch (Throwable t) {
  102. logger.error("获取缓存失败key[" + KEY_PREFIX_VALUE + k + ", error[" + t + "]");
  103. }
  104. return null;
  105. }
  106. /**
  107. * 移除缓存
  108. * @param k
  109. * @return
  110. */
  111. protected boolean removeValue(String k) {
  112. return remove(KEY_PREFIX_VALUE + k);
  113. }
  114. protected boolean removeSet(String k) {
  115. return remove(KEY_PREFIX_SET + k);
  116. }
  117. protected boolean removeList(String k) {
  118. return remove(KEY_PREFIX_LIST + k);
  119. }
  120. /**
  121. * 移除缓存
  122. * @param key
  123. * @return
  124. */
  125. protected boolean remove(String key) {
  126. try {
  127. redisTemplate.delete(key);
  128. return true;
  129. } catch (Throwable t) {
  130. logger.error("获取缓存失败key[" + key + ", error[" + t + "]");
  131. }
  132. return false;
  133. }
  134. /**
  135. * 缓存set操作
  136. * @param k
  137. * @param v
  138. * @param time
  139. * @return
  140. */
  141. protected boolean cacheSet(String k, String v, long time) {
  142. String key = KEY_PREFIX_SET + k;
  143. try {
  144. SetOperations<String, String> valueOps = redisTemplate.opsForSet();
  145. valueOps.add(key, v);
  146. if (time > 0) redisTemplate.expire(key, time, TimeUnit.SECONDS);
  147. return true;
  148. } catch (Throwable t) {
  149. logger.error("缓存[" + key + "]失败, value[" + v + "]", t);
  150. }
  151. return false;
  152. }
  153. /**
  154. * 缓存set
  155. * @param k
  156. * @param v
  157. * @return
  158. */
  159. protected boolean cacheSet(String k, String v) {
  160. return cacheSet(k, v, -1);
  161. }
  162. /**
  163. * 缓存set
  164. * @param k
  165. * @param v
  166. * @param time
  167. * @return
  168. */
  169. protected boolean cacheSet(String k, Set<String> v, long time) {
  170. String key = KEY_PREFIX_SET + k;
  171. try {
  172. SetOperations<String, String> setOps = redisTemplate.opsForSet();
  173. setOps.add(key, v.toArray(new String[v.size()]));
  174. if (time > 0) redisTemplate.expire(key, time, TimeUnit.SECONDS);
  175. return true;
  176. } catch (Throwable t) {
  177. logger.error("缓存[" + key + "]失败, value[" + v + "]", t);
  178. }
  179. return false;
  180. }
  181. /**
  182. * 缓存set
  183. * @param k
  184. * @param v
  185. * @return
  186. */
  187. protected boolean cacheSet(String k, Set<String> v) {
  188. return cacheSet(k, v, -1);
  189. }
  190. /**
  191. * 获取缓存set数据
  192. * @param k
  193. * @return
  194. */
  195. protected Set<String> getSet(String k) {
  196. try {
  197. SetOperations<String, String> setOps = redisTemplate.opsForSet();
  198. return setOps.members(KEY_PREFIX_SET + k);
  199. } catch (Throwable t) {
  200. logger.error("获取set缓存失败key[" + KEY_PREFIX_SET + k + ", error[" + t + "]");
  201. }
  202. return null;
  203. }
  204. /**
  205. * list缓存
  206. * @param k
  207. * @param v
  208. * @param time
  209. * @return
  210. */
  211. protected boolean cacheList(String k, String v, long time) {
  212. String key = KEY_PREFIX_LIST + k;
  213. try {
  214. ListOperations<String, String> listOps = redisTemplate.opsForList();
  215. listOps.rightPush(key, v);
  216. if (time > 0) redisTemplate.expire(key, time, TimeUnit.SECONDS);
  217. return true;
  218. } catch (Throwable t) {
  219. logger.error("缓存[" + key + "]失败, value[" + v + "]", t);
  220. }
  221. return false;
  222. }
  223. /**
  224. * 缓存list
  225. * @param k
  226. * @param v
  227. * @return
  228. */
  229. protected boolean cacheList(String k, String v) {
  230. return cacheList(k, v, -1);
  231. }
  232. /**
  233. * 缓存list
  234. * @param k
  235. * @param v
  236. * @param time
  237. * @return
  238. */
  239. protected boolean cacheList(String k, List<String> v, long time) {
  240. String key = KEY_PREFIX_LIST + k;
  241. try {
  242. ListOperations<String, String> listOps = redisTemplate.opsForList();
  243. long l = listOps.rightPushAll(key, v);
  244. if (time > 0) redisTemplate.expire(key, time, TimeUnit.SECONDS);
  245. return true;
  246. } catch (Throwable t) {
  247. logger.error("缓存[" + key + "]失败, value[" + v + "]", t);
  248. }
  249. return false;
  250. }
  251. /**
  252. * 缓存list
  253. * @param k
  254. * @param v
  255. * @return
  256. */
  257. protected boolean cacheList(String k, List<String> v) {
  258. return cacheList(k, v, -1);
  259. }
  260. /**
  261. * 获取list缓存
  262. * @param k
  263. * @param start
  264. * @param end
  265. * @return
  266. */
  267. protected List<String> getList(String k, long start, long end) {
  268. try {
  269. ListOperations<String, String> listOps = redisTemplate.opsForList();
  270. return listOps.range(KEY_PREFIX_LIST + k, start, end);
  271. } catch (Throwable t) {
  272. logger.error("获取list缓存失败key[" + KEY_PREFIX_LIST + k + ", error[" + t + "]");
  273. }
  274. return null;
  275. }
  276. /**
  277. * 获取总条数, 可用于分页
  278. * @param k
  279. * @return
  280. */
  281. protected long getListSize(String k) {
  282. try {
  283. ListOperations<String, String> listOps = redisTemplate.opsForList();
  284. return listOps.size(KEY_PREFIX_LIST + k);
  285. } catch (Throwable t) {
  286. logger.error("获取list长度失败key[" + KEY_PREFIX_LIST + k + "], error[" + t + "]");
  287. }
  288. return 0;
  289. }
  290. /**
  291. * 获取总条数, 可用于分页
  292. * @param listOps
  293. * @param k
  294. * @return
  295. */
  296. protected long getListSize(ListOperations<String, String> listOps, String k) {
  297. try {
  298. return listOps.size(KEY_PREFIX_LIST + k);
  299. } catch (Throwable t) {
  300. logger.error("获取list长度失败key[" + KEY_PREFIX_LIST + k + "], error[" + t + "]");
  301. }
  302. return 0;
  303. }
  304. /**
  305. * 移除list缓存
  306. * @param k
  307. * @return
  308. */
  309. protected boolean removeOneOfList(String k) {
  310. String key = KEY_PREFIX_LIST + k;
  311. try {
  312. ListOperations<String, String> listOps = redisTemplate.opsForList();
  313. listOps.rightPop(KEY_PREFIX_LIST + k);
  314. return true;
  315. } catch (Throwable t) {
  316. logger.error("移除list缓存失败key[" + KEY_PREFIX_LIST + k + ", error[" + t + "]");
  317. }
  318. return false;
  319. }
  320. }

 

 

 

以上就是对redis的操作工具实现了,学习笔记over

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/220850
推荐阅读
相关标签
  

闽ICP备14008679号