好了,下面是pom的代碼
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>redis</groupId>
- <artifactId>redis</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <build>
- </build>
- <dependencies>
- <dependency>
- <groupId>org.springframework.data</groupId>
- <artifactId>spring-data-redis</artifactId>
- <version>1.0.2.RELEASE</version>
- </dependency>
- </dependencies>
- </project>
在pom裏添加了redis spring客戶端的依賴,那麼所有的jar包都會幫你自動下載下來,是不是很方便啊,哈
下面是spring-context.xml代碼
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
- <!--註解說明 -->
- <context:annotation-config />
- <!-- 把標記了@Controller註解的類轉換爲bean -->
- <context:component-scan base-package="com.mkfree.**" />
- <!-- redis工廠 -->
- <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
- p:host-name="192.168.9.140" p:port="6379" p:password="87980879" />
- <!-- redis服務封裝 -->
- <bean id="redisService" class="com.mkfree.redis.test.RedisService">
- </bean>
這裏配置了一個跟spring 集成的redis客戶端,ip port password自己定哦,這裏我在redis配置文件了定義了需求密碼認證,你們先不要用吧,爲了簡單起見
下面是RedisService,裏面包含了對redis的方法,還有更多的方法沒有去使用,這裏當一個入門的小例子吧
- package com.mkfree.redis.test;
- import java.util.Set;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
- import redis.clients.jedis.Jedis;
- /**
- * 封裝redis 緩存服務器服務接口
- * @author hk
- *
- * 2012-12-16 上午3:09:18
- */
- public class RedisService {
- /**
- * 通過key刪除(字節)
- * @param key
- */
- public void del(byte [] key){
- this.getJedis().del(key);
- }
- /**
- * 通過key刪除
- * @param key
- */
- public void del(String key){
- this.getJedis().del(key);
- }
- /**
- * 添加key value 並且設置存活時間(byte)
- * @param key
- * @param value
- * @param liveTime
- */
- public void set(byte [] key,byte [] value,int liveTime){
- this.set(key, value);
- this.getJedis().expire(key, liveTime);
- }
- /**
- * 添加key value 並且設置存活時間
- * @param key
- * @param value
- * @param liveTime
- */
- public void set(String key,String value,int liveTime){
- this.set(key, value);
- this.getJedis().expire(key, liveTime);
- }
- /**
- * 添加key value
- * @param key
- * @param value
- */
- public void set(String key,String value){
- this.getJedis().set(key, value);
- }
- /**添加key value (字節)(序列化)
- * @param key
- * @param value
- */
- public void set(byte [] key,byte [] value){
- this.getJedis().set(key, value);
- }
- /**
- * 獲取redis value (String)
- * @param key
- * @return
- */
- public String get(String key){
- String value = this.getJedis().get(key);
- return value;
- }
- /**
- * 獲取redis value (byte [] )(反序列化)
- * @param key
- * @return
- */
- public byte[] get(byte [] key){
- return this.getJedis().get(key);
- }
- /**
- * 通過正則匹配keys
- * @param pattern
- * @return
- */
- public Set<String> keys(String pattern){
- return this.getJedis().keys(pattern);
- }
- /**
- * 檢查key是否已經存在
- * @param key
- * @return
- */
- public boolean exists(String key){
- return this.getJedis().exists(key);
- }
- /**
- * 清空redis 所有數據
- * @return
- */
- public String flushDB(){
- return this.getJedis().flushDB();
- }
- /**
- * 查看redis裏有多少數據
- */
- public long dbSize(){
- return this.getJedis().dbSize();
- }
- /**
- * 檢查是否連接成功
- * @return
- */
- public String ping(){
- return this.getJedis().ping();
- }
- /**
- * 獲取一個jedis 客戶端
- * @return
- */
- private Jedis getJedis(){
- if(jedis == null){
- return jedisConnectionFactory.getShardInfo().createResource();
- }
- return jedis;
- }
- private RedisService (){
- }
- //操作redis客戶端
- private static Jedis jedis;
- @Autowired
- @Qualifier("jedisConnectionFactory")
- private JedisConnectionFactory jedisConnectionFactory;
- }
下面是測試代碼TestRedis.java
- package com.mkfree.redis.test;
- import java.util.Set;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- /**
- * redis spring 簡單例子
- * @author hk
- *
- * 2012-12-22 上午10:40:15
- */
- public class TestRedis {
- public static void main(String[] args) throws InterruptedException {
- ApplicationContext app = new ClassPathXmlApplicationContext("classpath:spring-context.xml");
- //這裏已經配置好,屬於一個redis的服務接口
- RedisService redisService = (RedisService) app.getBean("redisService");
- String ping = redisService.ping();//測試是否連接成功,連接成功輸出PONG
- System.out.println(ping);
- //首先,我們看下redis服務裏是否有數據
- long dbSizeStart = redisService.dbSize();
- System.out.println(dbSizeStart);
- redisService.set("username", "oyhk");//設值(查看了源代碼,默認存活時間30分鐘)
- String username = redisService.get("username");//取值
- System.out.println(username);
- redisService.set("username1", "oyhk1", 1);//設值,並且設置數據的存活時間(這裏以秒爲單位)
- String username1 = redisService.get("username1");
- System.out.println(username1);
- Thread.sleep(2000);//我睡眠一會,再去取,這個時間超過了,他的存活時間
- String liveUsername1 = redisService.get("username1");
- System.out.println(liveUsername1);//輸出null
- //是否存在
- boolean exist = redisService.exists("username");
- System.out.println(exist);
- //查看keys
- Set<String> keys = redisService.keys("*");//這裏查看所有的keys
- System.out.println(keys);//只有username username1(已經清空了)
- //刪除
- redisService.set("username2", "oyhk2");
- String username2 = redisService.get("username2");
- System.out.println(username2);
- redisService.del("username2");
- String username2_2 = redisService.get("username2");
- System.out.println(username2_2);//如果爲null,那麼就是刪除數據了
- //dbsize
- long dbSizeEnd = redisService.dbSize();
- System.out.println(dbSizeEnd);
- //清空reids所有數據
- //redisService.flushDB();
- }
- }
好了入門例子就先這麼多了...
項目源代碼下載: http://blog.mkfree.com/posts/12