spring 结合 redis 例子 (转)

好了费话不多说了,介绍下spring 结合redis是怎么操作数据的 这里我用了maven管理,由于简单嘛,依赖下包就行了..不用单独去依赖包,成了我的习惯


好了,下面是pom的代码

[xml] view plaincopy
 
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>redis</groupId>  
  5.     <artifactId>redis</artifactId>  
  6.     <version>0.0.1-SNAPSHOT</version>  
  7.     <build>  
  8.     </build>  
  9.     <dependencies>  
  10.         <dependency>  
  11.             <groupId>org.springframework.data</groupId>  
  12.             <artifactId>spring-data-redis</artifactId>  
  13.             <version>1.0.2.RELEASE</version>  
  14.         </dependency>  
  15.     </dependencies>  
  16. </project>  

 

 

 

 在pom里添加了redis spring客户端的依赖,那么所有的jar包都会帮你自动下载下来,是不是很方便啊,哈

 


下面是spring-context.xml代码

 

[xml] view plaincopy
 
  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" xmlns:p="http://www.springframework.org/schema/p"  
  4. xmlns:context="http://www.springframework.org/schema/context"  
  5. xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6. xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd  
  7. http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd  
  9. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">  
  10.   
  11. <!--注解说明 -->  
  12. <context:annotation-config />  
  13. <!-- 把标记了@Controller注解的类转换为bean -->  
  14. <context:component-scan base-package="com.mkfree.**" />  
  15. <!-- redis工厂 -->  
  16. <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"  
  17.     p:host-name="192.168.9.140" p:port="6379" p:password="87980879" />  
  18. <!-- redis服务封装 -->  
  19. <bean id="redisService" class="com.mkfree.redis.test.RedisService">  
  20. </bean>  

 

 

 

 这里配置了一个跟spring 集成的redis客户端,ip port password自己定哦,这里我在redis配置文件了定义了需求密码认证,你们先不要用吧,为了简单起见

 


下面是RedisService,里面包含了对redis的方法,还有更多的方法没有去使用,这里当一个入门的小例子吧

[java] view plaincopy
 
  1. package com.mkfree.redis.test;  
  2.   
  3. import java.util.Set;  
  4.   
  5. import org.springframework.beans.factory.annotation.Autowired;  
  6. import org.springframework.beans.factory.annotation.Qualifier;  
  7. import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;  
  8.   
  9. import redis.clients.jedis.Jedis;  
  10.   
  11. /** 
  12.  * 封装redis 缓存服务器服务接口 
  13.  * @author hk 
  14.  * 
  15.  * 2012-12-16 上午3:09:18 
  16.  */  
  17. public class RedisService {  
  18.   
  19.     /** 
  20.      * 通过key删除(字节) 
  21.      * @param key 
  22.      */  
  23.     public void del(byte [] key){  
  24.         this.getJedis().del(key);  
  25.     }  
  26.     /** 
  27.      * 通过key删除 
  28.      * @param key 
  29.      */  
  30.     public void del(String key){  
  31.         this.getJedis().del(key);  
  32.     }  
  33.   
  34.     /** 
  35.      * 添加key value 并且设置存活时间(byte) 
  36.      * @param key 
  37.      * @param value 
  38.      * @param liveTime 
  39.      */  
  40.     public void set(byte [] key,byte [] value,int liveTime){  
  41.         this.set(key, value);  
  42.         this.getJedis().expire(key, liveTime);  
  43.     }  
  44.     /** 
  45.      * 添加key value 并且设置存活时间 
  46.      * @param key 
  47.      * @param value 
  48.      * @param liveTime 
  49.      */  
  50.     public void set(String key,String value,int liveTime){  
  51.         this.set(key, value);  
  52.         this.getJedis().expire(key, liveTime);  
  53.     }  
  54.     /** 
  55.      * 添加key value 
  56.      * @param key 
  57.      * @param value 
  58.      */  
  59.     public void set(String key,String value){  
  60.         this.getJedis().set(key, value);  
  61.     }  
  62.     /**添加key value (字节)(序列化) 
  63.      * @param key 
  64.      * @param value 
  65.      */  
  66.     public void set(byte [] key,byte [] value){  
  67.         this.getJedis().set(key, value);  
  68.     }  
  69.     /** 
  70.      * 获取redis value (String) 
  71.      * @param key 
  72.      * @return 
  73.      */  
  74.     public String get(String key){  
  75.         String value = this.getJedis().get(key);  
  76.         return value;  
  77.     }  
  78.     /** 
  79.      * 获取redis value (byte [] )(反序列化) 
  80.      * @param key 
  81.      * @return 
  82.      */  
  83.     public byte[] get(byte [] key){  
  84.         return this.getJedis().get(key);  
  85.     }  
  86.   
  87.     /** 
  88.      * 通过正则匹配keys 
  89.      * @param pattern 
  90.      * @return 
  91.      */  
  92.     public Set<String> keys(String pattern){  
  93.         return this.getJedis().keys(pattern);  
  94.     }  
  95.   
  96.     /** 
  97.      * 检查key是否已经存在 
  98.      * @param key 
  99.      * @return 
  100.      */  
  101.     public boolean exists(String key){  
  102.         return this.getJedis().exists(key);  
  103.     }  
  104.     /** 
  105.      * 清空redis 所有数据 
  106.      * @return 
  107.      */  
  108.     public String flushDB(){  
  109.         return this.getJedis().flushDB();  
  110.     }  
  111.     /** 
  112.      * 查看redis里有多少数据 
  113.      */  
  114.     public long dbSize(){  
  115.         return this.getJedis().dbSize();  
  116.     }  
  117.     /** 
  118.      * 检查是否连接成功 
  119.      * @return 
  120.      */  
  121.     public String ping(){  
  122.         return this.getJedis().ping();  
  123.     }  
  124.     /** 
  125.      * 获取一个jedis 客户端 
  126.      * @return 
  127.      */  
  128.     private Jedis getJedis(){  
  129.         if(jedis == null){  
  130.             return jedisConnectionFactory.getShardInfo().createResource();  
  131.         }  
  132.         return jedis;  
  133.     }  
  134.     private RedisService (){  
  135.   
  136.     }  
  137.     //操作redis客户端  
  138.     private static Jedis jedis;  
  139.     @Autowired  
  140.     @Qualifier("jedisConnectionFactory")  
  141.     private JedisConnectionFactory jedisConnectionFactory;  
  142. }  


下面是测试代码TestRedis.java

[java] view plaincopy
 
    1. package com.mkfree.redis.test;  
    2.   
    3. import java.util.Set;  
    4.   
    5. import org.springframework.context.ApplicationContext;  
    6. import org.springframework.context.support.ClassPathXmlApplicationContext;  
    7.   
    8. /** 
    9.  * redis spring 简单例子 
    10.  * @author hk 
    11.  * 
    12.  * 2012-12-22 上午10:40:15 
    13.  */  
    14. public class TestRedis {  
    15.   
    16.     public static void main(String[] args) throws InterruptedException {  
    17.         ApplicationContext app = new ClassPathXmlApplicationContext("classpath:spring-context.xml");  
    18.         //这里已经配置好,属于一个redis的服务接口  
    19.         RedisService redisService = (RedisService) app.getBean("redisService");  
    20.   
    21.         String ping = redisService.ping();//测试是否连接成功,连接成功输出PONG  
    22.         System.out.println(ping);  
    23.   
    24.         //首先,我们看下redis服务里是否有数据  
    25.         long dbSizeStart = redisService.dbSize();  
    26.         System.out.println(dbSizeStart);  
    27.   
    28.         redisService.set("username", "oyhk");//设值(查看了源代码,默认存活时间30分钟)  
    29.         String username = redisService.get("username");//取值   
    30.         System.out.println(username);  
    31.         redisService.set("username1", "oyhk1", 1);//设值,并且设置数据的存活时间(这里以秒为单位)  
    32.         String username1 = redisService.get("username1");  
    33.         System.out.println(username1);  
    34.         Thread.sleep(2000);//我睡眠一会,再去取,这个时间超过了,他的存活时间  
    35.         String liveUsername1 = redisService.get("username1");  
    36.         System.out.println(liveUsername1);//输出null  
    37.   
    38.         //是否存在  
    39.         boolean exist = redisService.exists("username");  
    40.         System.out.println(exist);  
    41.   
    42.         //查看keys  
    43.         Set<String> keys = redisService.keys("*");//这里查看所有的keys  
    44.         System.out.println(keys);//只有username username1(已经清空了)  
    45.   
    46.         //删除  
    47.         redisService.set("username2", "oyhk2");  
    48.         String username2 = redisService.get("username2");  
    49.         System.out.println(username2);  
    50.         redisService.del("username2");  
    51.         String username2_2 = redisService.get("username2");  
    52.         System.out.println(username2_2);//如果为null,那么就是删除数据了  
    53.   
    54.         //dbsize  
    55.         long dbSizeEnd = redisService.dbSize();  
    56.         System.out.println(dbSizeEnd);  
    57.   
    58.         //清空reids所有数据  
    59.         //redisService.flushDB();  
    60.     }  
    61. }
posted @ 2015-02-04 15:12  flay  阅读(516)  评论(0编辑  收藏  举报