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!




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章