实现Redis作二级缓存
<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
<version>2.9.0version>
dependency>
<dependency>
<groupId>org.springframework.datagroupId>
<artifactId>spring-data-redisartifactId>
<version>2.1.3.RELEASEversion>
dependency>
通过定义的类实现Cache接口,来对Redis中的缓存进行管理
public class RedisCache implements Cache {
//mybatis需要一个id,我们通过构造方法给id赋值
private final String id;
//创建一个读写锁对象
private static ReadWriteLock rw = new ReentrantReadWriteLock();
// redis的连接工厂
public static JedisConnectionFactory jf;
public RedisCache(String id) {
this.id = id;
}
@Override
public String getId() {
return id;
}
@Override
public void putObject(Object o, Object o1) {
RedisConnection con = jf.getConnection();
RedisSerializer<Object> rs = new JdkSerializationRedisSerializer();
con.set(rs.serialize(o),rs.serialize(o1));
con.close();
System.out.println("-------------------添加二级缓存成功-----------------");
}
@Override
public Object getObject(Object o) {
Lock lock = rw.readLock();
lock.lock();
RedisConnection con = jf.getConnection();
RedisSerializer<Object> rs = new JdkSerializationRedisSerializer();
Object object = rs.deserialize(con.get(rs.serialize(o)));
if (object!=null){
System.out.println("-------------------命中二级缓存成功-----------------");
}else {
System.out.println("-------------------命中二级缓存失败-----------------");
}
con.close();
lock.unlock();
return object;
}
@Override
public Object removeObject(Object o) {
RedisConnection con = jf.getConnection();
RedisSerializer<Object> rs = new JdkSerializationRedisSerializer();
// con.del(rs.serialize(o));
Boolean expire = con.expire(rs.serialize(o), 0);
con.close();
return expire;
}
@Override
public void clear() {
RedisConnection con = jf.getConnection();
con.flushAll();
System.out.println("-------------------清空二级缓存-----------------");
con.close();
}
@Override
public int getSize() {
RedisConnection con = jf.getConnection();
Integer i = Integer.valueOf(con.dbSize().toString());
return i;
}
@Override
public ReadWriteLock getReadWriteLock() {
return rw;
}
public static void setJf(JedisConnectionFactory jf){
RedisCache.jf=jf;
}
}
package com.zhiyou.redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
/**
* 中间类 主要是为了实现从ioc容器中获取redis
*/
public class RedisStaticDi {
@Autowired
public void setJf(JedisConnectionFactory jf){
RedisCache.jf = jf;
}
}
<util:properties location="classpath:redis.properties" id="redis"/>
<bean id="pool" class="redis.clients.jedis.JedisPoolConfig"/>
<bean id="con" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="poolConfig" ref="pool"/>
<property name="hostName" value="#{redis.host}"/>
<property name="port" value="#{redis.port}"/>
<property name="password" value="#{redis.password}"/>
bean>
<bean id="di" class="com.zhiyou.redis.RedisStaticDi">
<property name="jf" ref="con"/>
bean>
host=192..***.130 # ip地址
port=6379 # 连接的端口 默认redis端口 6379
password=****** # 连接Redis的密码
在需要做缓存的mappe.xml中配置,在项目中调用到相对应的SQL时,二级缓存的缓存数据将会被添加到Redis数据库中被管理
<cache type="com.zhiyou.redis.RedisCache"/>
<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
<version>2.9.0version>
dependency>
<dependency>
<groupId>org.springframework.datagroupId>
<artifactId>spring-data-redisartifactId>
<version>1.7.2.RELEASEversion>
dependency>
/**
* redis 缓存配置类
*/
@Configuration //表示这个是一个配置类 相当于xml的作用
@EnableCaching
public class RedisCacheConfig extends JCacheConfigurerSupport {
//redis 连接工厂
private volatile JedisConnectionFactory jedisConnectionFactory;
//reids 操作模板
private volatile RedisTemplate<String,String> redisTemplate;
//redis 缓存管理器
private volatile RedisCacheManager redisCacheManager;
public RedisCacheConfig() {
}
public RedisCacheConfig(JedisConnectionFactory jedisConnectionFactory, RedisTemplate<String, String> redisTemplate, RedisCacheManager redisCacheManager) {
this.jedisConnectionFactory = jedisConnectionFactory;
this.redisTemplate = redisTemplate;
this.redisCacheManager = redisCacheManager;
}
public JedisConnectionFactory getJedisConnectionFactory() {
return jedisConnectionFactory;
}
public RedisTemplate<String, String> getRedisTemplate() {
return redisTemplate;
}
public RedisCacheManager getRedisCacheManager() {
return redisCacheManager;
}
}
在application.xml 配置Redis的相关属性 让其支持二级缓存
<util:properties location="classpath:redis.properties" id="redis"/>
<bean id="pool" class="redis.clients.jedis.JedisPoolConfig"/>
<bean id="con" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="poolConfig" ref="pool"/>
<property name="hostName" value="#{redis.host}"/>
<property name="port" value="#{redis.port}"/>
<property name="password" value="#{redis.password}"/>
bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="con"/>
bean>
<bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
<constructor-arg name="redisOperations" ref="redisTemplate"/>
<property name="defaultExpiration" value="3000"/>
bean>
<bean id="redisCacheConfig" class="com.zhiyou.redis.RedisCacheConfig">
<constructor-arg ref="con"/>
<constructor-arg ref="redisTemplate"/>
<constructor-arg ref="redisCacheManager"/>
bean>
在service层的实现类 加入注解 实现对二级缓存的管理
@Cacheable(value = "user")
@Override
public List<User> selectAll() {
return userMapper.selectAll();
}
@Cacheable注解详解
key 的值不能乱写 可以使用方法的形参当作key 例如方法体是select(int id)那么key就可以写成#id
或者方法体是select(User user) key可以写成 #user.id 或者 #0.id
方法没有形参的时候,Spring提给我们一个root对象用来操作,可以直接通过#root找到对象
#root.methodName 方法名称
#root.method 当前方法
#root.target 代表当前被调用的对象
#root.targetClass 代表当前被调用对象的class
#root.args 代表当前方法组成的数组
#root.caches 代表当前被调用的方法使用的缓存
我们在使用root里面的属性的时候,可以省略#root