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.      /**注解:opsForValue()方法的使用:

  35.     set(K key, V value)

          * 新增一个字符串类型的值,key是键,value是值。

    *Java代码
    1. redisTemplate.opsForValue().set("stringValue","bbb");  
  36.   **/

  37.             operations.set(key, value);  
  38.             result = true;  
  39.         } catch (Exception e) {  
  40.             logger.error("write redis is faill");  
  41.             e.printStackTrace();  
  42.               
  43.         }  
  44.         return result;  
  45.     }  
  46.       
  47.       
  48.       /**  
  49.      * 写入缓存  
  50.      *  设置失效时间 
  51.      * @param key  
  52.      * @param value  
  53.      * @return  
  54.      */    
  55.     public static boolean set(final String key, Object value, Long expireTime) {    
  56.         boolean result = false;    
  57.         try {    
  58.             ValueOperations<Serializable, Object> operations = redisTemplate    
  59.                     .opsForValue();    
  60.             operations.set(key, value);    
  61.             redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);    
  62.             result = true;    
  63.         } catch (Exception e) {    
  64.             e.printStackTrace();    
  65.         }    
  66.         return result;    
  67.     }    
  68.     /** 
  69.      * 读取缓存 
  70.      * @param key 
  71.      * @return 
  72.      */  
  73.     public static Object get(final String key)  
  74.     {  
  75.         Object result = null;  
  76.         ValueOperations<Serializable, Object> operations = redisTemplate  
  77.                 .opsForValue();  
  78.         result = operations.get(key);  
  79.         return result;  
  80.     }  
  81.       
  82.     /** 
  83.      * 删除对应的value 
  84.      * @param key 
  85.      */  
  86.     public static void remove(final String key)  
  87.     {  
  88.         if (exists(key)) {  
  89.             redisTemplate.delete(key);  
  90.         }  
  91.     }  
  92.       
  93.     /** 
  94.      * 批量删除对应的value 
  95.      *  
  96.      * @param keys 
  97.      */  
  98.     public static void remove(final String... keys) {  
  99.         for (String key : keys) {  
  100.             remove(key);  
  101.         }  
  102.     }  
  103.       
  104.     /** 
  105.      * 批量删除key 
  106.      *  
  107.      * @param pattern 正则表达式 
  108.      */  
  109.     public static void removePattern(final String pattern) {  
  110.         Set<Serializable> keys = redisTemplate.keys(pattern);  
  111.         if (keys.size() > 0)  
  112.             redisTemplate.delete(keys);  
  113.     }  
  114.       
  115.     /** 
  116.      * 判断缓存中是否有对应的value 
  117.      *  
  118.      * @param key 
  119.      * @return 
  120.      */  
  121.     public static boolean exists(final String key) {  
  122.         return redisTemplate.hasKey(key);  
  123.     }  
  124.   
  125.     public  void setRedisTemplate(  
  126.             RedisTemplate<Serializable, Object> redisTemplate) {  
  127.         this.redisTemplate = redisTemplate;  
  128.     }  
  129. }  

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值