目的:解决单机session不能共享问题,插入查询数据库时间效率问题,实现分布式缓存。 
准备材料:Redis 下载链接 http://pan.baidu.com/s/1dEGTxvV 
相关jar包如果需要可以留言也可以自行下载 
 
redis 下载之后安装部署: 
解压压缩包,第一步点击run.bat如下图

############### redis连接池配置
 
第二步会出现如下图,有端口号的界面标示启动成功。 
 
第三步如果发生产时候需要改掉端口号,防止被攻击,在redis.conf配置文件里面修改 
 
第四步点击安装客户端 
 
安装好后按如下操作 
 
 
好了以上就将redis安装部署完成了,下面需要将其用到我们的项目里面。与spring相结合

准备开始 
第一步:

首先需要配置文件,也可以不配置直接在xml中写,但是为了以后的方便修改,建议写到配置文件名字命名为redis.properties里面。

############### redis连接池配置
  1. #redis主机地址
  2. redis.host=127.0.0.1
  3. #redis端口
  4. redis.port=6379
  5. #redis连接池最大值
  6. redis.pool.maxTotal=300
  7. #redis连接最大空闲值
  8. redis.pool.maxIdle=20
  9. #redis连接最小空闲值
  10. redis.pool.minIdle=5
  11. #redis获取连接时是否验证可用性
  12. redis.pool.testOnBorrow=true
  13. #redis连接超时时间
  14. redis.timeout=15000
  15. #是否使用连接池管理连接
  16. redis.usePool=true

#####################  redis管理session  #############################

然后配置spring-context-redis.xml也可以直接写在applicationContext.xml里面但是我这里通过导入的方式

  1. ************************spring-context-redis.xml*****************
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
  7. xmlns:aop="http://www.springframework.org/schema/aop"
  8. xsi:schemaLocation="
  9. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  10. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  11.  
  12. <context:component-scan base-package="com.tianrong">
  13. </context:component-scan>
  14. <context:component-scan base-package="com.etianrong">
  15. </context:component-scan>
  16. <!-- Jedis -->
  17. <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
  18. <property name="maxTotal" value="${redis.pool.maxTotal}" />
  19. <property name="maxIdle" value="${redis.pool.maxIdle}" />
  20. <property name="minIdle" value="${redis.pool.minIdle}" />
  21. <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
  22. </bean>
  23. <bean id="jedisConnectionFactory"
  24. class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
  25. <property name="hostName" value="${redis.host}" />
  26. <property name="port" value="${redis.port}" />
  27. <property name="timeout" value="${redis.timeout}" />
  28. <property name="usePool" value="${redis.usePool}" />
  29. <property name="poolConfig" ref="jedisPoolConfig" />
  30. </bean>
  31. <bean id="stringRedisSerializer"
  32. class="org.springframework.data.redis.serializer.StringRedisSerializer" />
  33. <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
  34. <property name="connectionFactory" ref="jedisConnectionFactory"/>
  35. <property name="keySerializer" ref="stringRedisSerializer"/>
  36. <property name="hashKeySerializer" ref="stringRedisSerializer"/>
  37. </bean>
  38.  
  39. </beans>
  40. ************************spring-context-redis.xml*****************
  41.  
  42. ************************applicationContext.xml***start*************
  43. <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  44. <property name="location">
  45. <value>classpath*:*.properties</value>
  46. </property>
  47. <property name="fileEncoding" value="utf-8" />
  48. </bean>
  49. <import resource="spring-context-redis.xml"/>#在这导入
  50. ************************applicationContext.xml***end*************
  51.  
  52. ************************web.xml***start*************
  53. <servlet>
  54. <servlet-name>spring</servlet-name>
  55. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  56. <init-param>
  57. <param-name>contextConfigLocation</param-name>
  58. <param-value>classpath:applicationContext.xml</param-value>
  59. </init-param>
  60. <load-on-startup>1</load-on-startup>
  61. </servlet>
  62. ************************web.xml***end*************

**************初始化数据监听器******Start***********

  1. import java.util.HashMap;
  2. import java.util.List;
  3. import java.util.Map;
  4.  
  5. import org.apache.log4j.Logger;
  6.  
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.context.ApplicationListener;
  9. import org.springframework.context.event.ContextRefreshedEvent;
  10. import org.springframework.stereotype.Service;
  11.  
  12. import com.tianrong.product.entity.Integral;
  13. import com.tianrong.product.service.IntegralService;
  14.  
  15. import org.springframework.data.redis.core.RedisTemplate;
  16.  
  17. /*
  18. * 监听器,用于项目启动的时候初始化信息
  19. */
  20. @Service
  21. public class StartAddCacheListener implements ApplicationListener<ContextRefreshedEvent>
  22. {
  23. //日志
  24. private final Logger log= Logger.getLogger(StartAddCacheListener.class);
  25.  
  26. @Autowired
  27. public RedisUtil<Object> redisCache;
  28.  
  29. @Override
  30. public void onApplicationEvent(ContextRefreshedEvent event)
  31. {
  32. //初始化数据
  33. redisCache.setCacheObject("dataList", "Hello World!");
  34. }
  35.  
  36. public void test (){
  37. String dataList= redisCache.getCacheObject("dataList");
  38. log.info(dataList+"****************************************");
  39. }
  40.  
  41. }
  42. ************************初始化数据监听器*****end
  43. ********************************工具类**********************
  44. import java.io.Serializable;
  45. import java.util.ArrayList;
  46. import java.util.HashMap;
  47. import java.util.HashSet;
  48. import java.util.Iterator;
  49. import java.util.List;
  50. import java.util.Map;
  51. import java.util.Set;
  52.  
  53. import org.springframework.beans.factory.annotation.Autowired;
  54. import org.springframework.beans.factory.annotation.Qualifier;
  55. import org.springframework.context.support.ClassPathXmlApplicationContext;
  56. import org.springframework.data.redis.core.BoundSetOperations;
  57. import org.springframework.data.redis.core.HashOperations;
  58. import org.springframework.data.redis.core.ListOperations;
  59. import org.springframework.data.redis.core.RedisTemplate;
  60. import org.springframework.data.redis.core.SetOperations;
  61. import org.springframework.data.redis.core.ValueOperations;
  62. import org.springframework.stereotype.Service;
  63. /**
  64. * @author Wolf
  65. *
  66. */
  67. @Service
  68. public class RedisUtil<T> {
  69.  
  70. // @Autowired @Qualifier("redisTemplate")
  71. // public RedisTemplate redisTemplate;
  72.  
  73. @Autowired
  74. public RedisTemplate redisTemplate;
  75.  
  76. /**
  77. * 缓存基本的对象,Integer、String、实体类等
  78. * @param key 缓存的键值
  79. * @param value 缓存的值
  80. * @return 缓存的对象
  81. */
  82. public <T> ValueOperations<String,T> setCacheObject(String key,T value)
  83. {
  84. System.out.println(key+"*****"+value.toString());
  85. ValueOperations<String,T> operation = redisTemplate.opsForValue();
  86. operation.set(key,value);
  87. return operation;
  88. }
  89.  
  90. /**
  91. * 获得缓存的基本对象。
  92. * @param key 缓存键值
  93. * @param operation
  94. * @return 缓存键值对应的数据
  95. */
  96. public <T> T getCacheObject(String key/*,ValueOperations<String,T> operation*/)
  97. {
  98. ValueOperations<String,T> operation = redisTemplate.opsForValue();
  99. return operation.get(key);
  100. }
  101.  
  102. /**
  103. * 缓存List数据
  104. * @param key 缓存的键值
  105. * @param dataList 待缓存的List数据
  106. * @return 缓存的对象
  107. */
  108. public <T> ListOperations<String, T> setCacheList(String key,List<T> dataList)
  109. {
  110. ListOperations listOperation = redisTemplate.opsForList();
  111. if(null != dataList)
  112. {
  113. int size = dataList.size();
  114. for(int i = 0; i < size ; i ++)
  115. {
  116.  
  117. listOperation.rightPush(key,dataList.get(i));
  118. }
  119. }
  120.  
  121. return listOperation;
  122. }
  123.  
  124. /**
  125. * 获得缓存的list对象
  126. * @param key 缓存的键值
  127. * @return 缓存键值对应的数据
  128. */
  129. public <T> List<T> getCacheList(String key)
  130. {
  131. List<T> dataList = new ArrayList<T>();
  132. ListOperations<String,T> listOperation = redisTemplate.opsForList();
  133. Long size = listOperation.size(key);
  134.  
  135. for(int i = 0 ; i < size ; i ++)
  136. {
  137. dataList.add((T) listOperation.leftPop(key));
  138. }
  139.  
  140. return dataList;
  141. }
  142.  
  143. /**
  144. * 缓存Set
  145. * @param key 缓存键值
  146. * @param dataSet 缓存的数据
  147. * @return 缓存数据的对象
  148. */
  149. public <T> BoundSetOperations<String,T> setCacheSet(String key,Set<T> dataSet)
  150. {
  151. BoundSetOperations<String,T> setOperation = redisTemplate.boundSetOps(key);
  152. /*T[] t = (T[]) dataSet.toArray();
  153. setOperation.add(t);*/
  154.  
  155. Iterator<T> it = dataSet.iterator();
  156. while(it.hasNext())
  157. {
  158. setOperation.add(it.next());
  159. }
  160.  
  161. return setOperation;
  162. }
  163.  
  164. /**
  165. * 获得缓存的set
  166. * @param key
  167. * @param operation
  168. * @return
  169. */
  170. public Set<T> getCacheSet(String key/*,BoundSetOperations<String,T> operation*/)
  171. {
  172. Set<T> dataSet = new HashSet<T>();
  173. BoundSetOperations<String,T> operation = redisTemplate.boundSetOps(key);
  174.  
  175. Long size = operation.size();
  176. for(int i = 0 ; i < size ; i++)
  177. {
  178. dataSet.add(operation.pop());
  179. }
  180. return dataSet;
  181. }
  182.  
  183. /**
  184. * 缓存Map
  185. * @param key
  186. * @param dataMap
  187. * @return
  188. */
  189. public <T> HashOperations<String,String,T> setCacheMap(String key,Map<String,T> dataMap)
  190. {
  191.  
  192. HashOperations hashOperations = redisTemplate.opsForHash();
  193. if(null != dataMap)
  194. {
  195.  
  196. for (Map.Entry<String, T> entry : dataMap.entrySet()) {
  197.  
  198. /*System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); */
  199. hashOperations.put(key,entry.getKey(),entry.getValue());
  200. }
  201.  
  202. }
  203.  
  204. return hashOperations;
  205. }
  206.  
  207. /**
  208. * 获得缓存的Map
  209. * @param key
  210. * @param hashOperation
  211. * @return
  212. */
  213. public <T> Map<String,T> getCacheMap(String key/*,HashOperations<String,String,T> hashOperation*/)
  214. {
  215. Map<String, T> map = redisTemplate.opsForHash().entries(key);
  216. /*Map<String, T> map = hashOperation.entries(key);*/
  217. return map;
  218. }
  219.  
  220. /**
  221. * 缓存Map
  222. * @param key
  223. * @param dataMap
  224. * @return
  225. */
  226. public <T> HashOperations<String,Integer,T> setCacheIntegerMap(String key,Map<Integer,T> dataMap)
  227. {
  228. HashOperations hashOperations = redisTemplate.opsForHash();
  229. if(null != dataMap)
  230. {
  231.  
  232. for (Map.Entry<Integer, T> entry : dataMap.entrySet()) {
  233.  
  234. /*System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); */
  235. hashOperations.put(key,entry.getKey(),entry.getValue());
  236. }
  237.  
  238. }
  239.  
  240. return hashOperations;
  241. }
  242.  
  243. /**
  244. * 获得缓存的Map
  245. * @param key
  246. * @param hashOperation
  247. * @return
  248. */
  249. public <T> Map<Integer,T> getCacheIntegerMap(String key/*,HashOperations<String,String,T> hashOperation*/)
  250. {
  251. Map<Integer, T> map = redisTemplate.opsForHash().entries(key);
  252. /*Map<String, T> map = hashOperation.entries(key);*/
  253. return map;
  254. }
  255.  
  256. }
  257. **************************************工具类**************************

下面是初始化监听器效果图************ 
 
以下是单元测试图 

组件-------(一)redis系列--安装部署redis+实现redis分布式缓存 java+Spring+redis的更多相关文章

  1. redis的安装部署启动停止<17.3.21已更新>

    --------------------------------------------------------- 启动redis时使用下面两条命令: redis-server /etc/redis. ...

  2. Redis- 内存数据库Redis之安装部署

    内存数据库Redis之安装部署 Redis是一款非关系型,key-value存储的内存数据库,Redis数据库完全在内存中,使用磁盘仅用于持久性.Redis的速度非常快,每秒能执行约11万集合,每秒约 ...

  3. 如何用分布式缓存服务实现Redis内存优化

    Redis是一种支持Key-Value等多种数据结构的存储系统,其数据特性是“ALL IN MEMORY”,因此优化内存十分重要.在对Redis进行内存优化时,先要掌握Redis内存存储的特性比如字符 ...

  4. redis cluster安装部署(测试环境)

    redis 应用于web前端,做缓存和数据存取的速度是挺可观的,最近看了一些资料,手痒了,就弄了一个测试环境,两台方案,试用一下. ##Redis 集群部署## 一,方案调研: 参考博客: http: ...

  5. windows环境redis主从安装部署

    准备工作 下载windows环境redis,我下载的是2.4.5,解压,拷贝一主(master)两从(slaveof).主机端口使用6379,两从的端口分别为6380和6381, 我本地索性用6379 ...

  6. Redis系列之(一):10分钟玩转Redis(转)

    1. Redis介绍 Redis是一个开源的使用ANSI C语言编写.基于内存的Key-Value数据库. 它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集 ...

  7. Redis系列之(一):10分钟玩转Redis

    1. Redis介绍 Redis是一个开源的使用ANSI C语言编写.基于内存的Key-Value数据库. 它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集 ...

  8. Redis 学习-安装、数据类型与 API 理解、Java 客户端

    本博客是在学习<Redis从入门到高可用,分布式实践>教程时的笔记. 同时参考: https://www.cnblogs.com/jiang910/p/10020048.html 一.Re ...

  9. 第十二节:Asp.Net Core 之分布式缓存(SQLServer和Redis)

    一. 整体说明 1. 说明 分布式缓存通常是指在多个应用程序服务器的架构下,作为他们共享的外部服务共享缓存,常用的有SQLServer.Redis.NCache.     特别说明一下:这里的分布式是 ...

随机推荐

  1. ASP.NET - 记住滚动条的位置

    MaintainScrollPositionOnPostback ="true" 如果是滚动条在最下面,那么如果刷新页面,滚动条回到最上面. 使用这个属性之后,滚动条会在刷新之前的 ...

  2. JS - 焦点图

    下载地址:http://www.lanrentuku.com/js/jiaodiantu-1076.html 修改焦点图: CSS代码: /* 懒人图库 搜集整理 www.lanrentuku.com ...

  3. Jsoup API解析HTML中input标签

    Jsoup官网地址:http://jsoup.org/ 1. 解析单个input元素     String html = "<p><input align=\"t ...

  4. php与文件操作

    一.目录操作 首先是从目录读取的函数,opendir(),readdir(),closedir(),使用的时候是先打开文件句柄,而后迭代列出: <?php $base_dir="fil ...

  5. ALV判断修改后是否有不合法数据,有则选中错误行,高亮度显示。

    alv数据表维护表时错误行需要高亮度显示 gt_index_rows TYPE lvc_t_row,"用以存放要选择行的内表 gs_index_rows TYPE lvc_s_row.&qu ...

  6. c/c++使用VS2013连接MySQL与ubuntu下c链接mysql

    vs连接数据库事实上就是将mysql数据库.h头文件接口.lib链接文件和dll运行文件增加到项目中.以下是配置怎样增加. 转于http://www.cnblogs.com/justinzhang/a ...

  7. HDU 3853 期望概率DP

    期望概率DP简单题 从[1,1]点走到[r,c]点,每走一步的代价为2 给出每一个点走相邻位置的概率,共3中方向,不动: [x,y]->[x][y]=p[x][y][0] ,  右移:[x][y ...

  8. J2EE之初识JSP

    上篇博客已经简介了下Servlet.从上篇博客中能够看到.Servlet获得返回来的数据后.显示给client时,须要不断的拼串.从而构成完整的html页面,这就在无形中加大了程序猿的压力和劳动力.而 ...

  9. inline与lnk2001、lnk2019,鸡肋?

    inline函数居然出现了lnk2001.lnk2019,先贴代码. a.h #pragma once class A { public: inline void foo();     void us ...

  10. [Android学习笔记]Android中多线程开发的一些概念

    线程安全: 在多线程的情况下,不会因为线程之间的操作而导致数据错误. 线程同步: 同一个资源,可能在同一时间被多个线程操作,这样会导致数据错误.这是一个现象,也是一个问题,而研究如何解决此类问题的相关 ...