spring 結合redis的例子

好了,下面是pom的代碼

  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代碼

 

  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的方法,還有更多的方法沒有去使用,這裏當一個入門的小例子吧

  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

  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. }  

好了入門例子就先這麼多了... 

項目源代碼下載: http://blog.mkfree.com/posts/12

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章