java web项目中Spring整合Redis的使用

一、添加Redis Maven依赖

[html]  view plain  copy
  1. <span style="white-space:pre;"> </span><!-- redis -->  
  2.         <dependency>  
  3.             <groupId>redis.clients</groupId>  
  4.             <artifactId>jedis</artifactId>  
  5.             <version>2.6.2</version>  
  6.         </dependency>  
  7.   
  8.         <dependency>  
  9.             <groupId>org.springframework.data</groupId>  
  10.             <artifactId>spring-data-redis</artifactId>  
  11.             <version>1.7.1.RELEASE</version>  
  12.         </dependency>  


二、config.properties

[html]  view plain  copy
  1. redis.host=192.168.0.254  
  2. redis.port=6379    
  3. redis.password=jedis21tb  
  4.   
  5. redis.maxIdle=300    
  6. redis.maxWait=1000    
  7. redis.testOnBorrow=true    
  8. redis.timeout=3000  


三、spring配置文件(applicationContext.xml):

[html]  view plain  copy
  1. <!--引入配置属性文件 -->  
  2.     <context:property-placeholder location="classpath:config.properties" />  
  3.     <!-- redis -->  
  4.     <!-- jedis 配置 -->  
  5.     <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig" >  
  6.           <property name="maxIdle" value="${redis.maxIdle}" />  
  7.           <property name="maxWaitMillis" value="${redis.maxWait}" />  
  8.           <property name="testOnBorrow" value="${redis.testOnBorrow}" />  
  9.     </bean >  
  10.    <!-- redis服务器中心 -->  
  11.     <bean id="connectionFactory"  class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >  
  12.           <property name="poolConfig" ref="poolConfig" />  
  13.           <property name="port" value="${redis.port}" />  
  14.           <property name="hostName" value="${redis.host}" />  
  15.           <property name="password" value="${redis.password}" />  
  16.           <property name="timeout" value="${redis.timeout}" ></property>  
  17.     </bean >  
  18.     <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" >  
  19.           <property name="connectionFactory" ref="connectionFactory" />  
  20.           <property name="keySerializer" >  
  21.               <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />  
  22.           </property>  
  23.           <!-- <property name="valueSerializer" >  
  24.               <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />  
  25.           </property> -->  
  26.     </bean >  
  27.   
  28.     <bean id="redisUtil" class="com.kttx.common.RedisUtil" >  
  29.           <property name="redisTemplate" ref="redisTemplate" />  
  30.     </bean >  
四、java代码

[java]  view plain  copy
  1. package com.kttx.common;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.Set;  
  5. import java.util.concurrent.TimeUnit;  
  6.   
  7. import org.apache.log4j.Logger;  
  8. import org.springframework.data.redis.core.RedisTemplate;  
  9. import org.springframework.data.redis.core.ValueOperations;  
  10.   
  11. /** 
  12.  * redis 
  13.  * @author txs 
  14.  * 
  15.  */  
  16. public class RedisUtil {  
  17.       
  18.     private static Logger logger = Logger.getLogger(RedisUtil.class);  
  19.       
  20.     private static RedisTemplate<Serializable, Object> redisTemplate;  
  21.       
  22.     /** 
  23.      * 写入或更新缓存 
  24.      * @param key 
  25.      * @param value 
  26.      * @return 
  27.      */  
  28.     public static boolean set(final String key, Object value)  
  29.     {  
  30.         boolean result = false;  
  31.         try {  
  32.             ValueOperations<Serializable, Object> operations = redisTemplate  
  33.                     .opsForValue();  
  34.             operations.set(key, value);  
  35.             result = true;  
  36.         } catch (Exception e) {  
  37.             logger.error("write redis is faill");  
  38.             e.printStackTrace();  
  39.               
  40.         }  
  41.         return result;  
  42.     }  
  43.       
  44.       
  45.       /**  
  46.      * 写入缓存  
  47.      *  设置失效时间 
  48.      * @param key  
  49.      * @param value  
  50.      * @return  
  51.      */    
  52.     public static boolean set(final String key, Object value, Long expireTime) {    
  53.         boolean result = false;    
  54.         try {    
  55.             ValueOperations<Serializable, Object> operations = redisTemplate    
  56.                     .opsForValue();    
  57.             operations.set(key, value);    
  58.             redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);    
  59.             result = true;    
  60.         } catch (Exception e) {    
  61.             e.printStackTrace();    
  62.         }    
  63.         return result;    
  64.     }    
  65.     /** 
  66.      * 读取缓存 
  67.      * @param key 
  68.      * @return 
  69.      */  
  70.     public static Object get(final String key)  
  71.     {  
  72.         Object result = null;  
  73.         ValueOperations<Serializable, Object> operations = redisTemplate  
  74.                 .opsForValue();  
  75.         result = operations.get(key);  
  76.         return result;  
  77.     }  
  78.       
  79.     /** 
  80.      * 删除对应的value 
  81.      * @param key 
  82.      */  
  83.     public static void remove(final String key)  
  84.     {  
  85.         if (exists(key)) {  
  86.             redisTemplate.delete(key);  
  87.         }  
  88.     }  
  89.       
  90.     /** 
  91.      * 批量删除对应的value 
  92.      *  
  93.      * @param keys 
  94.      */  
  95.     public static void remove(final String... keys) {  
  96.         for (String key : keys) {  
  97.             remove(key);  
  98.         }  
  99.     }  
  100.       
  101.     /** 
  102.      * 批量删除key 
  103.      *  
  104.      * @param pattern 正则表达式 
  105.      */  
  106.     public static void removePattern(final String pattern) {  
  107.         Set<Serializable> keys = redisTemplate.keys(pattern);  
  108.         if (keys.size() > 0)  
  109.             redisTemplate.delete(keys);  
  110.     }  
  111.       
  112.     /** 
  113.      * 判断缓存中是否有对应的value 
  114.      *  
  115.      * @param key 
  116.      * @return 
  117.      */  
  118.     public static boolean exists(final String key) {  
  119.         return redisTemplate.hasKey(key);  
  120.     }  
  121.   
  122.     public  void setRedisTemplate(  
  123.             RedisTemplate<Serializable, Object> redisTemplate) {  
  124.         this.redisTemplate = redisTemplate;  
  125.     }  
  126. }  
使用:RedisUtil.set(key,value);
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值