【Redis基础】SSH 中Spring-data-redis使用体验

15 篇文章 0 订阅
3 篇文章 0 订阅

1、项目中导入所需3个jar包

   commons-pool-2.2.jar
   jedis-2.3.1.jar
   spring-data-redis-1.3.4.relese.jar
2、新建redis配置文件redis.properties内容为
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. redis.host=127.0.0.1  #ip  
  2. redis.port=6379     #端口  
  3. redis.default.db=0  
  4. redis.timeout=100000  
  5. redis.maxActive=300  
  6. redis.maxIdle=100  
  7. redis.maxWait=1000  
  8. redis.testOnBorrow=true   


 3、新建spring配置redis的文件redis.xml,内容为 
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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:p="http://www.springframework.org/schema/p"   
  5. xmlns:context="http://www.springframework.org/schema/context"   
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">   
  8. <!-- 加载Reids属性配置文件 -->  
  9. <context:property-placeholder  
  10. location="classpath:redis.properties" />  
  11.   
  12. <bean id="propertyConfigurerRedis"  
  13. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  14. <property name="order" value="1" />  
  15. <property name="ignoreUnresolvablePlaceholders" value="true" />  
  16. <property name="systemPropertiesMode" value="1" />  
  17. <property name="searchSystemEnvironment" value="true" />  
  18. <property name="locations">  
  19. <list>  
  20. <value>classpath:redis.properties</value>  
  21. </list>  
  22. </property>  
  23. </bean>  
  24.   
  25. <!-- jedis pool配置 -->  
  26. <bean id="jedisPoolConfig"  
  27. class="redis.clients.jedis.JedisPoolConfig">  
  28. <property name="maxIdle" value="${redis.maxIdle}" />  
  29. <property name="testOnBorrow" value="${redis.testOnBorrow}" />  
  30. </bean>  
  31.   
  32. <!-- spring data redis -->  
  33. <bean id="jedisConnectionFactory"  
  34. class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">  
  35. <property name="usePool" value="true"></property>  
  36. <property name="hostName" value="${redis.host}" />  
  37. <property name="port" value="${redis.port}" />  
  38. <property name="timeout" value="${redis.timeout}" />  
  39. <property name="database" value="${redis.default.db}"></property>  
  40. <constructor-arg index="0" ref="jedisPoolConfig" />  
  41. </bean>  
  42.   
  43. <bean id="redisTemplate"  
  44. class="org.springframework.data.redis.core.StringRedisTemplate"  
  45. p:connectionFactory-ref="jedisConnectionFactory"  
  46. p:keySerializer-ref="stringRedisSerializer">  
  47. </bean>  
  48.   
  49. <!--这个主要是解决用cmd查看数据时为了显示正常-->  
  50. <bean id="stringRedisSerializer"  
  51. class="org.springframework.data.redis.serializer.StringRedisSerializer" />  
  52.   
  53. <!--配置一个基础类(之后的业务类继承于该类)、将redisTemplate注入 -->  
  54. <bean id="redisBase" abstract="true">  
  55. <property name="template" ref="redisTemplate"/>  
  56. </bean>  
  57.   
  58. <!-- 自动扫描 : 加载构建bean,以便通过注解方式注册bean -->  
  59. <context:component-scan base-package="com.fudabdlwj.redisclient" />  
  60.   
  61. </beans>   

4、web配置文件中添加reids配置
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <context-param>  
  2.     <param-name>contextConfigLocation</param-name>  
  3.     <param-value>  
  4.      classpath:applicationContext.xml,  
  5.      classpath:cxfservice.xml  
  6.      classpath:redis.xml  
  7.     </param-value>  
  8.   </context-param>  

5、编写接口,测试例子是网上找到的,做些点修改
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.fudabdlwj.redisclient;  
  2. public interface IRedisService<K, V> {       
  3.     public void set(K key, V value, long expiredTime);  
  4.     public V get(K key);  
  5.     public void del(K key);       
  6.     public Boolean check(K key,V value);         
  7. }  
6、抽象类
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.fudabdlwj.redisclient;  
  2.   
  3. import java.util.concurrent.TimeUnit;  
  4. import org.springframework.beans.factory.annotation.Autowired;  
  5. import org.springframework.data.redis.core.BoundValueOperations;  
  6. import org.springframework.data.redis.core.RedisTemplate;  
  7.   
  8. public abstract class AbstractRedisService<K, V> implements IRedisService<K, V> {  
  9.    
  10.    @Autowired  
  11.     private RedisTemplate<K, V> redisTemplate;  
  12.    
  13.     public RedisTemplate<K, V> getRedisTemplate() {  
  14.         return redisTemplate;  
  15.     }  
  16.    
  17.     public void setRedisTemplate(RedisTemplate<K, V> redisTemplate) {  
  18.         this.redisTemplate = redisTemplate;  
  19.     }  
  20.      
  21.     @Override  
  22.     public void set(final K key, final V value, final long expiredTime) {  
  23.         BoundValueOperations<K, V> valueOper = redisTemplate.boundValueOps(key);  
  24.         if (expiredTime <= 0) {  
  25.             valueOper.set(value);  
  26.         } else {  
  27.             valueOper.set(value, expiredTime, TimeUnit.MILLISECONDS);  
  28.         }  
  29.     }  
  30.    
  31.     @Override  
  32.     public V get(final K key) {  
  33.         BoundValueOperations<K, V> valueOper = redisTemplate.boundValueOps(key);  
  34.         return valueOper.get();  
  35.     }  
  36.    
  37.     @Override  
  38.     public void del(K key) {  
  39.         if (redisTemplate.hasKey(key)) {  
  40.             redisTemplate.delete(key);  
  41.         }  
  42.     }  
  43.     @Override  
  44.     public Boolean check(K key,V value){  
  45.      Boolean flag=false;  
  46.      if (redisTemplate.hasKey(key)) {  
  47.       if(value.equals(get(key))){  
  48.        flag=true;  
  49.       }  
  50.      }  
  51.      return flag;  
  52.     }  
  53.    
  54. }  


7、实现类
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.fudabdlwj.redisclient;  
  2.   
  3. import org.springframework.stereotype.Service;  
  4.   
  5. @Service("redisService")  
  6. public class RedisService extends AbstractRedisService<String, String> {  
  7.   
  8. }  

8、测试
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public static void main(String[] args) {  
  2.    
  3.   @SuppressWarnings("resource")    
  4.   ApplicationContext factory=new ClassPathXmlApplicationContext("classpath:redis.xml");  
  5.        
  6.   RedisService rs= (RedisService) factory.getBean("redisService");  
  7.   if(rs!=null){  
  8.    System.out.println("RedisService : "+rs);  
  9.    if(rs.check("lwj""123456")){ //</span><span style="font-family: 微软雅黑;">lwj</span><span style="font-family: 微软雅黑;">已事先写进redis的数据</span><span style="font-family:微软雅黑;">  
  10. </span><span style="font-family:微软雅黑;">    System.out.println("redis has contianed </span><span style="font-family: 微软雅黑;">lwj 123456</span><span style="font-family: 微软雅黑;">");</span><span style="font-family:微软雅黑;">  
  11.     System.out.println("add one new key-value : lwj 1234567 expire 15000");  
  12.     rs.set("lwj""123456"15000);  
  13.    }else{  
  14.     System.out.println("add one new key-value : lwj 123456 expire 15000");  
  15.     rs.set("lwj""1234567"15000);  
  16.    }  
  17.      
  18.    
  19.    String value=rs.get("lwj");  
  20.    System.out.println(value);  
  21. }  
结果
RedisService :  com.fudabdlwj.redisclient.RedisService@2dafcbf 
redis has contianed lwj
add one new key-value : lwj 1234567 expire 15000 
1234567
通过redis-cli.exe查看对应数据写入成功,15000时间到达时lwj被清除。
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值