Skip to content

Commit 8ae8499

Browse files
committed
��
1 parent d802780 commit 8ae8499

4 files changed

Lines changed: 228 additions & 0 deletions

File tree

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
<artifactId>jedis</artifactId>
3333
<version>${jedis.version}</version>
3434
</dependency>
35+
<dependency>
36+
<groupId>org.springframework.data</groupId>
37+
<artifactId>spring-data-redis</artifactId>
38+
<version>1.0.2.RELEASE</version>
39+
</dependency>
3540

3641
</dependencies>
3742
<!--<repositories>
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package com.xf.redis.demo;
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 SpringRedisHello {
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 SpringRedisHello (){
135+
136+
}
137+
//操作redis客户端
138+
private static Jedis jedis;
139+
@Autowired
140+
@Qualifier("jedisConnectionFactory")
141+
private JedisConnectionFactory jedisConnectionFactory;
142+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.xf.redis.demo;
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 TestSpringRedis {
15+
16+
public static void main(String[] args) throws InterruptedException {
17+
ApplicationContext app = new ClassPathXmlApplicationContext("classpath:spring-context.xml");
18+
//这里已经配置好,属于一个redis的服务接口
19+
SpringRedisHello redisService = (SpringRedisHello) 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+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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.xf.redis.demo" />
15+
<!-- redis工厂 -->
16+
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
17+
p:host-name="10.100.54.31" p:port="8379"/>
18+
<!-- redis服务封装 -->
19+
<bean id="redisService" class="com.xf.redis.demo.SpringRedisHello"/>
20+
</beans>

0 commit comments

Comments
 (0)