Spring Data+Redis缓存实现

一、所需jar包

spring-data-redis-1.7.2.RELEASE.jar

commons-pool2-2.4.2.jar

commons-logging-1.2.jar

jedis-2.8.2.jar

aopalliance-1.0.jar以及spring相关包

二、spring data连接redis

(1)创建Redis连接工厂

     JedisConnectionFactory   LettuceConnectionFactory

     JredisConnectionFactory 和  SrpConnectionFactory已过时。

默认使用JedisConnectionFactory。

(2)创建Redis模版

    RedisTemplate:极大简化了Redis的数据访问,能够让我们持久化各种类型的key和value

    StringRedisTemplate:扩展了RedisTemplate,只关注String类型

(3)Redis存储序列化

Redis存储时,key和value都会使用Redis的序列化器进行序列化。RedisTemplate会使用JdkSerializationRedisSerializer。StringRedisTemplate默认会使用StringRedisSerializer

当使用RedisTemplate,我们希望将Value序列化为JSON,而key是String类型时,通过keySerializer和valueSerializer改变序列化方式。如:

<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
          <property name="connectionFactory" ref="jedisConnectionFactory" />
          <property name="keySerializer" >
              <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
          </property>
          <property name="valueSerializer" >
              <bean class="org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer"/>
          </property>
      </bean>

Spring Data Redis提供多个序列化器:

    GenericToStringSerializer:使用spring转换服务进行序列化。

    JacksonJsonRedisSerializer:使用Jackson1,将对象序列化为JSON。

    Jackson2JsonRedisSerializer:使用Jackson2,将对象序列化为JSON。

    JdkSerializationRedisSerializer:使用Java序列化

    OxmSerializer:使用Spring O/X映射的编排器和解排器实现序列化,用于XML序列化。

    StringRedisSerializer:序列化String类型的key和value

三、示例

spring配置文件:

<?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:context="http://www.springframework.org/schema/context"  
     xmlns:cache="http://www.springframework.org/schema/cache"
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/cache
     http://www.springframework.org/schema/cache/spring-cache.xsd">   

  	
  	<context:component-scan base-package="com.learn" />
  	
  	<!-- 开启缓存 -->
  	<cache:annotation-driven/>
  	
  	<!-- redis连接池 -->
  	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">  
        <property name="maxIdle" value="6"></property>  
        <property name="minEvictableIdleTimeMillis" value="300000"></property>  
        <property name="numTestsPerEvictionRun" value="3"></property>  
        <property name="timeBetweenEvictionRunsMillis" value="60000"></property>
    </bean> 
  	
  	<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
  		<property name="hostName" value="127.0.0.1" />
  		<property name="port" value="6379" />
  		<property name="poolConfig" ref="jedisPoolConfig"></property>  
        <property name="timeout" value="2000"></property>  
        <property name="usePool" value="true"></property>  
  	</bean>
  	
  	<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
  		<property name="connectionFactory" ref="jedisConnectionFactory" />
  		<property name="keySerializer" >
  			<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
  		</property>
  		<property name="valueSerializer" >
  			<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
  		</property>
  	</bean>
  	
  	<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
		<constructor-arg name="redisOperations" ref="redisTemplate" />
  	</bean>
  	
</beans>
java类:

package com.learn.cache;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service("helloService")
public class HelloService {
	
	@Cacheable(value="messageCache" ,key="#name")
	public String getMessage(String name)
	{
		System.out.println("HELLO:" + name);
		return "hello:" + name +"!";
	}
}
测试类:
package com.learn.cache;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:/applicationContext.xml"); 
		HelloService helloService = (HelloService)ctx.getBean("helloService");
		
		System.out.println(helloService.getMessage("josh"));
		System.out.println("调用缓存,未调用方法--------------");
		System.out.println(helloService.getMessage("josh"));
		System.out.println();
		System.out.println(helloService.getMessage("josha"));
		System.out.println("调用缓存,未调用方法--------------");
		System.out.println(helloService.getMessage("josha"));

	}
}

启动Redis后运行测试类,结果:

HELLO:josh
hello:josh!
调用缓存,未调用方法--------------
hello:josh!

HELLO:josha
hello:josha!
调用缓存,未调用方法--------------
hello:josha!




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值