最近需要用到redis之前也搭过几次,但总是记不太清,网上找了下资料总不是自己想要的效果,这边分享一下加深一下自己的印象。(适合对spring框架有点基础的人)
绪论
目前官方推荐的Java版客户端是jedis,非常强大和稳定,支持事务、管道及有jedis自身实现。我们对redis数据的操作,都可以通过jedis来完成。
准备工作
操作redis的必须包
<properties>
<!-- redis 版本 -->
<redis.version>2.9.0</redis.version>
<spring.redis.version>1.8.4.RELEASE</spring.redis.version>
</properties>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${redis.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>${spring.redis.version}</version>
</dependency>
spring-redis.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- session设置 -->
<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
<property name="maxInactiveIntervalInSeconds" value="3600"></property>
</bean>
<!-- redis连接池 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大连接数 -->
<property name="maxTotal" value="30" />
<!-- 最大空闲连接数 -->
<property name="maxIdle" value="10" />
<!-- 每次释放连接的最大数目 -->
<property name="numTestsPerEvictionRun" value="1024" />
<!-- 释放连接的扫描间隔(毫秒) -->
<property name="timeBetweenEvictionRunsMillis" value="30000" />
<!-- 连接最小空闲时间 -->
<property name="minEvictableIdleTimeMillis" value="1800000" />
<!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
<property name="softMinEvictableIdleTimeMillis" value="10000" />
<!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
<property name="maxWaitMillis" value="1500" />
<!-- 在获取连接的时候检查有效性, 默认false -->
<property name="testOnBorrow" value="true" />
<!-- 在空闲时检查有效性, 默认false -->
<property name="testWhileIdle" value="true" />
<!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
<property name="blockWhenExhausted" value="false"/> </bean>
<!-- redis连接工厂 -->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="localhost"/>
<property name="port" value="6379"/>
<property name="password" value="123456"/>
<property name="timeout" value="20000"/>
<property name="poolConfig" ref="poolConfig"></property>
</bean>
<!-- 缓存序列化方式 -->
<bean id="keySerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
<bean id="valueSerializer" class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
<bean name="hashKeySerializer" class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"></bean>
<bean name="hashValueSerializer" class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"></bean>
<!-- 缓存 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="keySerializer" ref="keySerializer" />
<property name="valueSerializer" ref="valueSerializer" />
<property name="hashKeySerializer" ref="hashKeySerializer" />
<property name="hashValueSerializer" ref="hashValueSerializer" />
<!--开启事务-->
<property name="enableTransactionSupport" value="true"></property>
</bean>
<!--操作的工具类-->
<bean name="jRedisHelper" class="com.project.common.cache.JRedisHelper" >
<property name="redisTemplate" ref="redisTemplate" />
</bean>
</beans>
工具类
Spring封装了RedisTemplate对象来进行对Redis的各种操作,它支持所有的Redis原生的api。RedisTemplate位于spring-data-redis包下。详情麻烦自己看下api
package com.project.common.cache;
import com.project.common.PropertiesUtils;
import org.springframework.data.redis.core.RedisTemplate;
import java.io.Serializable;
import java.util.Date;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* Redis缓存辅助类
*
*/
public final class JRedisHelper {
private RedisTemplate<Serializable, Serializable> redisTemplate;
private final Integer EXPIRE = 60*60;
public void setRedisTemplate(RedisTemplate<Serializable, Serializable> redisTemplate) {
this.redisTemplate = redisTemplate;
}
public final Object get(final String key) {
expire(key, EXPIRE);
return redisTemplate.boundValueOps(key).get();
}
/*public final Set<Object> getAll(final String pattern) {
Set<Object> values = InstanceUtils.newHashSet();
Set<Serializable> keys = redisTemplate.keys(pattern);
for (Serializable key : keys) {
expire(key.toString(), EXPIRE);
values.add(redisTemplate.opsForValue().get(key));
}
return values;
}*/
public final void set(final String key, final Serializable value, int seconds) {
redisTemplate.boundValueOps(key).set(value);
expire(key, seconds);
}
public final void update(final String key, final Serializable value, int seconds) {
redisTemplate.opsForValue().set(key.toString(), value);
expire(key, seconds);
}
public final void set(final String key, final Serializable value) {
redisTemplate.boundValueOps(key).set(value);
expire(key, EXPIRE);
}
public final Boolean exists(final String key) {
return redisTemplate.hasKey(key);
}
public final void del(final String key) {
redisTemplate.delete(key);
}
public final void delAll(final String pattern) {
redisTemplate.delete(redisTemplate.keys(pattern));
}
public final String type(final String key) {
expire(key, EXPIRE);
return redisTemplate.type(key).getClass().getName();
}
/**
* 在某段时间后失效
*
* @return
*/
public final Boolean expire(final String key, final int seconds) {
return redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
}
}
测试
测试的话只需要在你需要加入缓存的地方注入 JRedisHelper 这个类然后调用对应的set get 方法就是了
正常情况只要你项目能正常启动就OK