组件-------(一)redis系列--安装部署redis+实现redis分布式缓存 java+Spring+redis

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

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

准备开始 
第一步:

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

############### redis 连接池配置
#redis 主机地址
redis.host=127.0.0.1
#redis 端口
redis.port=6379
#redis 连接池最大值
redis.pool.maxTotal=300
#redis 连接最大空闲值
redis.pool.maxIdle=20
#redis 连接最小空闲值
redis.pool.minIdle=5
#redis 获取连接时是否验证可用性
redis.pool.testOnBorrow=true
#redis 连接超时时间
redis.timeout=15000
#是否使用连接池管理连接
redis.usePool=true

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

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

************************spring-context-redis.xml*****************
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:aop="http://www.springframework.org/schema/aop"  
    xsi:schemaLocation="  
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">    
 <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">context:component-scan </span><span style="color: rgba(255, 0, 0, 1)">base-package</span><span style="color: rgba(0, 0, 255, 1)">="com.tianrong"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">context:component-scan</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">context:component-scan </span><span style="color: rgba(255, 0, 0, 1)">base-package</span><span style="color: rgba(0, 0, 255, 1)">="com.etianrong"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">context:component-scan</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> Jedis </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">bean </span><span style="color: rgba(255, 0, 0, 1)">id</span><span style="color: rgba(0, 0, 255, 1)">="jedisPoolConfig"</span><span style="color: rgba(255, 0, 0, 1)"> class</span><span style="color: rgba(0, 0, 255, 1)">="redis.clients.jedis.JedisPoolConfig"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="maxTotal"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="${redis.pool.maxTotal}"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="maxIdle"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="${redis.pool.maxIdle}"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="minIdle"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="${redis.pool.minIdle}"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="testOnBorrow"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="${redis.pool.testOnBorrow}"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">bean</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">bean </span><span style="color: rgba(255, 0, 0, 1)">id</span><span style="color: rgba(0, 0, 255, 1)">="jedisConnectionFactory"</span><span style="color: rgba(255, 0, 0, 1)">
    class</span><span style="color: rgba(0, 0, 255, 1)">="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="hostName"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="${redis.host}"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="port"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="${redis.port}"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="timeout"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="${redis.timeout}"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="usePool"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="${redis.usePool}"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="poolConfig"</span><span style="color: rgba(255, 0, 0, 1)"> ref</span><span style="color: rgba(0, 0, 255, 1)">="jedisPoolConfig"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">bean</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">bean </span><span style="color: rgba(255, 0, 0, 1)">id</span><span style="color: rgba(0, 0, 255, 1)">="stringRedisSerializer"</span><span style="color: rgba(255, 0, 0, 1)">
    class</span><span style="color: rgba(0, 0, 255, 1)">="org.springframework.data.redis.serializer.StringRedisSerializer"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">bean </span><span style="color: rgba(255, 0, 0, 1)">id</span><span style="color: rgba(0, 0, 255, 1)">="redisTemplate"</span><span style="color: rgba(255, 0, 0, 1)"> class</span><span style="color: rgba(0, 0, 255, 1)">="org.springframework.data.redis.core.RedisTemplate"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="connectionFactory"</span><span style="color: rgba(255, 0, 0, 1)"> ref</span><span style="color: rgba(0, 0, 255, 1)">="jedisConnectionFactory"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="keySerializer"</span><span style="color: rgba(255, 0, 0, 1)"> ref</span><span style="color: rgba(0, 0, 255, 1)">="stringRedisSerializer"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="hashKeySerializer"</span><span style="color: rgba(255, 0, 0, 1)"> ref</span><span style="color: rgba(0, 0, 255, 1)">="stringRedisSerializer"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">bean</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

</beans>
*******spring-context-redis.xml

applicationContext.xml*start
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath
:
.properties</value>
</property>
<property name="fileEncoding" value="utf-8" />
</bean>
<import resource="spring-context-redis.xml"/>#在这导入
************************applicationContext.xml
end
*****

web.xml*start
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
************************web.xml
end
*

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

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

import com.tianrong.product.entity.Integral;
import com.tianrong.product.service.IntegralService;

import org.springframework.data.redis.core.RedisTemplate;

/*

  • 监听器,用于项目启动的时候初始化信息
    */
    @Service
    public class StartAddCacheListener implements ApplicationListener<ContextRefreshedEvent>
    {
    //日志
    private final Logger log= Logger.getLogger(StartAddCacheListener.class);

@Autowired
public RedisUtil<Object> redisCache;

@Override
public void onApplicationEvent(ContextRefreshedEvent event)
{
//初始化数据
redisCache.setCacheObject("dataList", "Hello World!");
}

public void test (){
String dataList
= redisCache.getCacheObject("dataList");
log.info(dataList
+"****************************************");
}

}
*******************初始化数据监听器end
**********工具类
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
/**

  • @author Wolf

*/
@Service
public class RedisUtil<T> {

// @Autowired @Qualifier("redisTemplate")
// public RedisTemplate redisTemplate;

@Autowired
public RedisTemplate redisTemplate;

 </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
  * 缓存基本的对象,Integer、String、实体类等
  * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> key 缓存的键值
  * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> value 缓存的值
  * </span><span style="color: rgba(128, 128, 128, 1)">@return</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)">public</span> &lt;T&gt; ValueOperations&lt;String,T&gt;<span style="color: rgba(0, 0, 0, 1)"> setCacheObject(String key,T value)
 {
  System.out.println(key</span>+"*****"+<span style="color: rgba(0, 0, 0, 1)">value.toString());
  ValueOperations</span>&lt;String,T&gt; operation =<span style="color: rgba(0, 0, 0, 1)"> redisTemplate.opsForValue(); 
  operation.set(key,value);
  </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> operation;
 }

 </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
  * 获得缓存的基本对象。
  * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> key  缓存键值
  * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> operation
  * </span><span style="color: rgba(128, 128, 128, 1)">@return</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)">public</span> &lt;T&gt; T getCacheObject(String key<span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">,ValueOperations&lt;String,T&gt; operation</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">)
 {
  ValueOperations</span>&lt;String,T&gt; operation =<span style="color: rgba(0, 0, 0, 1)"> redisTemplate.opsForValue(); 
  </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> operation.get(key);
 }

 </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
  * 缓存List数据
  * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> key  缓存的键值
  * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> dataList 待缓存的List数据
  * </span><span style="color: rgba(128, 128, 128, 1)">@return</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)">public</span> &lt;T&gt; ListOperations&lt;String, T&gt; setCacheList(String key,List&lt;T&gt;<span style="color: rgba(0, 0, 0, 1)"> dataList)
 {
  ListOperations listOperation </span>=<span style="color: rgba(0, 0, 0, 1)"> redisTemplate.opsForList();
  </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)"> dataList)
  {
   </span><span style="color: rgba(0, 0, 255, 1)">int</span> size =<span style="color: rgba(0, 0, 0, 1)"> dataList.size();
   </span><span style="color: rgba(0, 0, 255, 1)">for</span>(<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i &lt; size ; i ++<span style="color: rgba(0, 0, 0, 1)">)
   {

    listOperation.rightPush(key,dataList.get(i));
   }
  }

  </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> listOperation;
 }

 </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
  * 获得缓存的list对象
  * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> key 缓存的键值
  * </span><span style="color: rgba(128, 128, 128, 1)">@return</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)">public</span> &lt;T&gt; List&lt;T&gt;<span style="color: rgba(0, 0, 0, 1)"> getCacheList(String key)
 {
  List</span>&lt;T&gt; dataList = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList&lt;T&gt;<span style="color: rgba(0, 0, 0, 1)">();
  ListOperations</span>&lt;String,T&gt; listOperation =<span style="color: rgba(0, 0, 0, 1)"> redisTemplate.opsForList();
  Long size </span>=<span style="color: rgba(0, 0, 0, 1)"> listOperation.size(key);

  </span><span style="color: rgba(0, 0, 255, 1)">for</span>(<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0 ; i &lt; size ; i ++<span style="color: rgba(0, 0, 0, 1)">)
  {
   dataList.add((T) listOperation.leftPop(key));
  }

  </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> dataList;
 }

 </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
  * 缓存Set
  * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> key  缓存键值
  * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> dataSet 缓存的数据
  * </span><span style="color: rgba(128, 128, 128, 1)">@return</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)">public</span> &lt;T&gt; BoundSetOperations&lt;String,T&gt; setCacheSet(String key,Set&lt;T&gt;<span style="color: rgba(0, 0, 0, 1)"> dataSet)
 {
  BoundSetOperations</span>&lt;String,T&gt; setOperation =<span style="color: rgba(0, 0, 0, 1)"> redisTemplate.boundSetOps(key); 
  </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">T[] t = (T[]) dataSet.toArray();
    setOperation.add(t);</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">


  Iterator</span>&lt;T&gt; it =<span style="color: rgba(0, 0, 0, 1)"> dataSet.iterator();
  </span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)">(it.hasNext())
  {
   setOperation.add(it.next());
  }

  </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> setOperation;
 }

 </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
  * 获得缓存的set
  * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> key
  * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> operation
  * </span><span style="color: rgba(128, 128, 128, 1)">@return</span>
  <span style="color: rgba(0, 128, 0, 1)">*/</span>
 <span style="color: rgba(0, 0, 255, 1)">public</span> Set&lt;T&gt; getCacheSet(String key<span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">,BoundSetOperations&lt;String,T&gt; operation</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">)
 {
  Set</span>&lt;T&gt; dataSet = <span style="color: rgba(0, 0, 255, 1)">new</span> HashSet&lt;T&gt;<span style="color: rgba(0, 0, 0, 1)">();
  BoundSetOperations</span>&lt;String,T&gt; operation =<span style="color: rgba(0, 0, 0, 1)"> redisTemplate.boundSetOps(key); 

  Long size </span>=<span style="color: rgba(0, 0, 0, 1)"> operation.size();
  </span><span style="color: rgba(0, 0, 255, 1)">for</span>(<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0 ; i &lt; size ; i++<span style="color: rgba(0, 0, 0, 1)">)
  {
   dataSet.add(operation.pop());
  }
  </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> dataSet;
 }

 </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
  * 缓存Map
  * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> key
  * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> dataMap
  * </span><span style="color: rgba(128, 128, 128, 1)">@return</span>
  <span style="color: rgba(0, 128, 0, 1)">*/</span>
 <span style="color: rgba(0, 0, 255, 1)">public</span> &lt;T&gt; HashOperations&lt;String,String,T&gt; setCacheMap(String key,Map&lt;String,T&gt;<span style="color: rgba(0, 0, 0, 1)"> dataMap)
 {

  HashOperations hashOperations </span>=<span style="color: rgba(0, 0, 0, 1)"> redisTemplate.opsForHash();
  </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)"> dataMap)
  {

   </span><span style="color: rgba(0, 0, 255, 1)">for</span> (Map.Entry&lt;String, T&gt;<span style="color: rgba(0, 0, 0, 1)"> entry : dataMap.entrySet()) { 

    </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
    hashOperations.put(key,entry.getKey(),entry.getValue());
   } 

  }

  </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> hashOperations;
 }

 </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
  * 获得缓存的Map
  * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> key
  * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> hashOperation
  * </span><span style="color: rgba(128, 128, 128, 1)">@return</span>
  <span style="color: rgba(0, 128, 0, 1)">*/</span>
 <span style="color: rgba(0, 0, 255, 1)">public</span> &lt;T&gt; Map&lt;String,T&gt; getCacheMap(String key<span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">,HashOperations&lt;String,String,T&gt; hashOperation</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">)
 {
  Map</span>&lt;String, T&gt; map =<span style="color: rgba(0, 0, 0, 1)"> redisTemplate.opsForHash().entries(key);
  </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">Map&lt;String, T&gt; map = hashOperation.entries(key);</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
  <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> map;
 }







 </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
  * 缓存Map
  * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> key
  * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> dataMap
  * </span><span style="color: rgba(128, 128, 128, 1)">@return</span>
  <span style="color: rgba(0, 128, 0, 1)">*/</span>
 <span style="color: rgba(0, 0, 255, 1)">public</span> &lt;T&gt; HashOperations&lt;String,Integer,T&gt; setCacheIntegerMap(String key,Map&lt;Integer,T&gt;<span style="color: rgba(0, 0, 0, 1)"> dataMap)
 {
  HashOperations hashOperations </span>=<span style="color: rgba(0, 0, 0, 1)"> redisTemplate.opsForHash();
  </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)"> dataMap)
  {

   </span><span style="color: rgba(0, 0, 255, 1)">for</span> (Map.Entry&lt;Integer, T&gt;<span style="color: rgba(0, 0, 0, 1)"> entry : dataMap.entrySet()) { 

    </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
    hashOperations.put(key,entry.getKey(),entry.getValue());
   } 

  }

  </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> hashOperations;
 }

 </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
  * 获得缓存的Map
  * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> key
  * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> hashOperation
  * </span><span style="color: rgba(128, 128, 128, 1)">@return</span>
  <span style="color: rgba(0, 128, 0, 1)">*/</span>
 <span style="color: rgba(0, 0, 255, 1)">public</span> &lt;T&gt; Map&lt;Integer,T&gt; getCacheIntegerMap(String key<span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">,HashOperations&lt;String,String,T&gt; hashOperation</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">)
 {
  Map</span>&lt;Integer, T&gt; map =<span style="color: rgba(0, 0, 0, 1)"> redisTemplate.opsForHash().entries(key);
  </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">Map&lt;String, T&gt; map = hashOperation.entries(key);</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
  <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> map;
 }

}
************工具类

下面是初始化监听器效果图************ 
这里写图片描述 
以下是单元测试图 
这里写图片描述