下载地址: demo
1. pom.xml
<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>a</groupId>
<artifactId>b</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
2. applicationContext.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.**" />
<context:property-placeholder
location="classpath:redis.properties" />
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxActive" value="${redis.maxActive}" />
<property name="maxWait" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<!-- p:password="${redis.pass}" -->
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}" p:port="${redis.port}"
p:pool-config-ref="poolConfig" />
<!-- <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory" /> </bean> -->
<bean id="stringRedisSerializer"
class="org.springframework.data.redis.serializer.StringRedisSerializer" />
<bean id='redisWriteTemplate'
class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="keySerializer" ref="stringRedisSerializer" />
<property name="hashKeySerializer"
ref="stringRedisSerializer" />
</bean>
<!-- redis服务封装 -->
<bean id="redisService" class="com.mkfree.redis.test.RedisService">
</bean>
</beans>
3. redis.prpperties
# Redis settings
redis.host=127.0.0.1
redis.port=6379
#redis.pass=java2000_wl
redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true
4. RedisService.java和TestRedis.java
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 doop
*@version 1.0 测试版,联系使用
*/
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("connectionFactory")
private JedisConnectionFactory jedisConnectionFactory;
}
/**
*
*/
/**
* @author root
*
*/
package com.mkfree.redis.test;
import java.util.Set;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* redis spring 简单例子
* @author doop
*
*/
public class TestRedis {
public static void main(String[] args) throws InterruptedException {
ApplicationContext app = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//这里已经配置好,属于一个redis的服务接口
RedisService redisService = (RedisService) app.getBean("redisService");
StringRedisSerializer stringRedisSerializer = (StringRedisSerializer) app.getBean("stringRedisSerializer");
RedisTemplate redisWriteTemplate=(RedisTemplate) app.getBean("redisWriteTemplate");
redisWriteTemplate.setKeySerializer(stringRedisSerializer);
redisWriteTemplate.setValueSerializer(stringRedisSerializer);
ValueOperations x = redisWriteTemplate.opsForValue();
x.set("liuyan", "you wii make it");
System.out.println(x.get("liuyan"));
String ping = redisService.ping();//测试是否连接成功,连接成功输出PONG
System.out.println(ping);
//首先,我们看下redis服务里是否有数据
long dbSizeStart = redisService.dbSize();
System.out.println(dbSizeStart);
redisService.set("username", "liuyan");//设值(查看了源代码,默认存活时间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);
exist=redisService.exists("sb");
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();
}
}