1 添加redis支持
在pom.xml中添加
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-redis</artifactId>
- </dependency>
2 redis配置
- package com.wisely.ij.config;
- import com.fasterxml.jackson.annotation.JsonAutoDetect;
- import com.fasterxml.jackson.annotation.PropertyAccessor;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import org.springframework.cache.CacheManager;
- import org.springframework.cache.annotation.CachingConfigurerSupport;
- import org.springframework.cache.annotation.EnableCaching;
- import org.springframework.cache.interceptor.KeyGenerator;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.data.redis.cache.RedisCacheManager;
- import org.springframework.data.redis.connection.RedisConnectionFactory;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.data.redis.core.StringRedisTemplate;
- import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
- import java.lang.reflect.Method;
- @Configuration
- @EnableCaching
- public class RedisConfig extends CachingConfigurerSupport{
- @Bean
- public KeyGenerator wiselyKeyGenerator(){
- return new KeyGenerator() {
- @Override
- public Object generate(Object target, Method method, Object... params) {
- StringBuilder sb = new StringBuilder();
- sb.append(target.getClass().getName());
- sb.append(method.getName());
- for (Object obj : params) {
- sb.append(obj.toString());
- }
- return sb.toString();
- }
- };
- }
- @Bean
- public CacheManager cacheManager(
- @SuppressWarnings("rawtypes") RedisTemplate redisTemplate) {
- return new RedisCacheManager(redisTemplate);
- }
- @Bean
- public RedisTemplate<String, String> redisTemplate(
- RedisConnectionFactory factory) {
- StringRedisTemplate template = new StringRedisTemplate(factory);
- Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
- ObjectMapper om = new ObjectMapper();
- om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
- om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
- jackson2JsonRedisSerializer.setObjectMapper(om);
- template.setValueSerializer(jackson2JsonRedisSerializer);
- template.afterPropertiesSet();
- return template;
- }
- }
3 redis服務器配置
- # REDIS (RedisProperties)
- spring.redis.database= # database name
- spring.redis.host=localhost # server host
- spring.redis.password= # server password
- spring.redis.port=6379 # connection port
- spring.redis.pool.max-idle=8 # pool settings ...
- spring.redis.pool.min-idle=0
- spring.redis.pool.max-active=8
- spring.redis.pool.max-wait=-1
- spring.redis.sentinel.master= # name of Redis server
- spring.redis.sentinel.nodes= # comma-separated list of host:port pairs
4 應用
測試兩個實體類
- package com.wisely.ij.domain;
- public class Address {
- private Long id;
- private String province;
- private String city;
- public Address(Long id,String province, String city) {
- this.id = id;
- this.province = province;
- this.city = city;
- }
- public Address() {
- }
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public String getProvince() {
- return province;
- }
- public void setProvince(String province) {
- this.province = province;
- }
- public String getCity() {
- return city;
- }
- public void setCity(String city) {
- this.city = city;
- }
- }
- package com.wisely.ij.domain;
- public class User {
- private Long id;
- private String firstName;
- private String lastName;
- public User(Long id,String firstName, String lastName) {
- this.id = id ;
- this.firstName = firstName;
- this.lastName = lastName;
- }
- public User() {
- }
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public String getFirstName() {
- return firstName;
- }
- public void setFirstName(String firstName) {
- this.firstName = firstName;
- }
- public String getLastName() {
- return lastName;
- }
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
- }
使用演示
- package com.wisely.ij.service;
- import com.wisely.ij.domain.Address;
- import com.wisely.ij.domain.User;
- import org.springframework.cache.annotation.Cacheable;
- import org.springframework.stereotype.Service;
- /**
- * Created by wisely on 2015/5/25.
- */
- @Service
- public class DemoService {
- @Cacheable(value = "usercache",keyGenerator = "wiselyKeyGenerator")
- public User findUser(Long id,String firstName,String lastName){
- System.out.println("無緩存的時候調用這里");
- return new User(id,firstName,lastName);
- }
- @Cacheable(value = "addresscache",keyGenerator = "wiselyKeyGenerator")
- public Address findAddress(Long id,String province,String city){
- System.out.println("無緩存的時候調用這里");
- return new Address(id,province,city);
- }
- }
- package com.wisely.ij.web;
- import com.wisely.ij.domain.Address;
- import com.wisely.ij.domain.User;
- import com.wisely.ij.service.DemoService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
- /**
- * Created by wisely on 2015/5/25.
- */
- @Controller
- public class DemoController {
- @Autowired
- DemoService demoService;
- @RequestMapping("/test")
- @ResponseBody
- public String putCache(){
- demoService.findUser(1l,"wang","yunfei");
- demoService.findAddress(1l,"anhui","hefei");
- System.out.println("若下面沒出現“無緩存的時候調用”字樣且能打印出數據表示測試成功");
- return "ok";
- }
- @RequestMapping("/test2")
- @ResponseBody
- public String testCache(){
- User user = demoService.findUser(1l,"wang","yunfei");
- Address address =demoService.findAddress(1l,"anhui","hefei");
- System.out.println("我這里沒執行查詢");
- System.out.println("user:"+"/"+user.getFirstName()+"/"+user.getLastName());
- System.out.println("address:"+"/"+address.getProvince()+"/"+address.getCity());
- return "ok";
- }
- }
5 檢驗
先訪問http://localhost:8080/test 保存緩存
再訪問http://localhost:8080/test2 調用緩存里的數據
http://wiselyman.iteye.com/blog/2184884
《整合 spring 4(包括mvc、context、orm) + mybatis 3 示例》一文簡要介紹了最新版本的 Spring MVC、IOC、MyBatis ORM 三者的整合以及聲明式事務處理。現在我們需要把緩存也整合進來,緩存我們選用的是 Redis,本文將在該文示例基礎上介紹 Redis 緩存 + Spring 的集成。關於 Redis 服務器的搭建請參考博客《Redhat5.8 環境下編譯安裝 Redis 並將其注冊為系統服務》。
1. 依賴包安裝
pom.xml 加入:
- <!-- redis cache related.....start -->
- <dependency>
- <groupId>org.springframework.data</groupId>
- <artifactId>spring-data-redis</artifactId>
- <version>1.6.0.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- <version>2.7.3</version>
- </dependency>
- <!-- redis cache related.....end -->
2. Spring 項目集成進緩存支持
要啟用緩存支持,我們需要創建一個新的 CacheManager bean。CacheManager 接口有很多實現,本文演示的是和 Redis 的集成,自然就是用 RedisCacheManager 了。Redis 不是應用的共享內存,它只是一個內存服務器,就像 MySql 似的,我們需要將應用連接到它並使用某種“語言”進行交互,因此我們還需要一個連接工廠以及一個 Spring 和 Redis 對話要用的 RedisTemplate,這些都是 Redis 緩存所必需的配置,把它們都放在自定義的 CachingConfigurerSupport 中:
- /**
- * File Name:RedisCacheConfig.java
- *
- * Copyright Defonds Corporation 2015
- * All Rights Reserved
- *
- */
- package com.defonds.bdp.cache.redis;
- import org.springframework.cache.CacheManager;
- import org.springframework.cache.annotation.CachingConfigurerSupport;
- import org.springframework.cache.annotation.EnableCaching;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.data.redis.cache.RedisCacheManager;
- import org.springframework.data.redis.connection.RedisConnectionFactory;
- import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
- import org.springframework.data.redis.core.RedisTemplate;
- /**
- *
- * Project Name:bdp
- * Type Name:RedisCacheConfig
- * Type Description:
- * Author:Defonds
- * Create Date:2015-09-21
- *
- * @version
- *
- */
- @Configuration
- @EnableCaching
- public class RedisCacheConfig extends CachingConfigurerSupport {
- @Bean
- public JedisConnectionFactory redisConnectionFactory() {
- JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();
- // Defaults
- redisConnectionFactory.setHostName("192.168.1.166");
- redisConnectionFactory.setPort(6379);
- return redisConnectionFactory;
- }
- @Bean
- public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) {
- RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
- redisTemplate.setConnectionFactory(cf);
- return redisTemplate;
- }
- @Bean
- public CacheManager cacheManager(RedisTemplate redisTemplate) {
- RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
- // Number of seconds before expiration. Defaults to unlimited (0)
- cacheManager.setDefaultExpiration(3000); // Sets the default expire time (in seconds)
- return cacheManager;
- }
- }
當然也別忘了把這些 bean 注入 Spring,不然配置無效。在 applicationContext.xml 中加入以下:
- <context:component-scan base-package="com.defonds.bdp.cache.redis" />
3. 緩存某些方法的執行結果
設置好緩存配置之后我們就可以使用 @Cacheable 注解來緩存方法執行的結果了,比如根據省份名檢索城市的 provinceCities 方法和根據 city_code 檢索城市的 searchCity 方法:
- // R
- @Cacheable("provinceCities")
- public List<City> provinceCities(String province) {
- logger.debug("province=" + province);
- return this.cityMapper.provinceCities(province);
- }
- // R
- @Cacheable("searchCity")
- public City searchCity(String city_code){
- logger.debug("city_code=" + city_code);
- return this.cityMapper.searchCity(city_code);
- }
4. 緩存數據一致性保證
CRUD (Create 創建,Retrieve 讀取,Update 更新,Delete 刪除) 操作中,除了 R 具備冪等性,其他三個發生的時候都可能會造成緩存結果和數據庫不一致。為了保證緩存數據的一致性,在進行 CUD 操作的時候我們需要對可能影響到的緩存進行更新或者清除。
- // C
- @CacheEvict(value = { "provinceCities"}, allEntries = true)
- public void insertCity(String city_code, String city_jb,
- String province_code, String city_name,
- String city, String province) {
- City cityBean = new City();
- cityBean.setCityCode(city_code);
- cityBean.setCityJb(city_jb);
- cityBean.setProvinceCode(province_code);
- cityBean.setCityName(city_name);
- cityBean.setCity(city);
- cityBean.setProvince(province);
- this.cityMapper.insertCity(cityBean);
- }
- // U
- @CacheEvict(value = { "provinceCities", "searchCity" }, allEntries = true)
- public int renameCity(String city_code, String city_name) {
- City city = new City();
- city.setCityCode(city_code);
- city.setCityName(city_name);
- this.cityMapper.renameCity(city);
- return 1;
- }
- // D
- @CacheEvict(value = { "provinceCities", "searchCity" }, allEntries = true)
- public int deleteCity(String city_code) {
- this.cityMapper.deleteCity(city_code);
- return 1;
- }
業務考慮,本示例用的都是 @CacheEvict 清除緩存。如果你的 CUD 能夠返回 City 實例,也可以使用 @CachePut 更新緩存策略。筆者推薦能用 @CachePut 的地方就不要用 @CacheEvict,因為后者將所有相關方法的緩存都清理掉,比如上面三個方法中的任意一個被調用了的話,provinceCities 方法的所有緩存將被清除。
5. 自定義緩存數據 key 生成策略
對於使用 @Cacheable 注解的方法,每個緩存的 key 生成策略默認使用的是參數名+參數值,比如以下方法:
- @Cacheable("users")
- public User findByUsername(String username)
這個方法的緩存將保存於 key 為 users~keys 的緩存下,對於 username 取值為 "趙德芳" 的緩存,key 為 "username-趙德芳"。一般情況下沒啥問題,二般情況如方法 key 取值相等然后參數名也一樣的時候就出問題了,如:
- @Cacheable("users")
- public Integer getLoginCountByUsername(String username)
這個方法的緩存也將保存於 key 為 users~keys 的緩存下。對於 username 取值為 "趙德芳" 的緩存,key 也為 "username-趙德芳",將另外一個方法的緩存覆蓋掉。
解決辦法是使用自定義緩存策略,對於同一業務(同一業務邏輯處理的方法,哪怕是集群/分布式系統),生成的 key 始終一致,對於不同業務則不一致:
- @Bean
- public KeyGenerator customKeyGenerator() {
- return new KeyGenerator() {
- @Override
- public Object generate(Object o, Method method, Object... objects) {
- StringBuilder sb = new StringBuilder();
- sb.append(o.getClass().getName());
- sb.append(method.getName());
- for (Object obj : objects) {
- sb.append(obj.toString());
- }
- return sb.toString();
- }
- };
- }
於是上述兩個方法,對於 username 取值為 "趙德芳" 的緩存,雖然都還是存放在 key 為 users~keys 的緩存下,但由於 key 分別為 "類名-findByUsername-username-趙德芳" 和 "類名-getLoginCountByUsername-username-趙德芳",所以也不會有問題。
這對於集群系統、分布式系統之間共享緩存很重要,真正實現了分布式緩存。
筆者建議:緩存方法的 @Cacheable 最好使用方法名,避免不同的方法的 @Cacheable 值一致,然后再配以以上緩存策略。
6. 緩存的驗證
6.1 緩存的驗證
為了確定每個緩存方法到底有沒有走緩存,我們打開了 MyBatis 的 SQL 日志輸出,並且為了演示清楚,我們還清空了測試用 Redis 數據庫。
先來驗證 provinceCities 方法緩存,Eclipse 啟動 tomcat 加載項目完畢,使用 JMeter 調用 /bdp/city/province/cities.json 接口:
Eclipse 控制台輸出如下:
說明這一次請求沒有命中緩存,走的是 db 查詢。JMeter 再次請求,Eclipse 控制台輸出:
標紅部分以下是這一次請求的 log,沒有訪問 db 的 log,緩存命中。查看本次請求的 Redis 存儲情況:
同樣可以驗證 city_code 為 1492 的 searchCity 方法的緩存是否有效:
圖中標紅部分是 searchCity 的緩存存儲情況。
6.2 緩存一致性的驗證
先來驗證 insertCity 方法的緩存配置,JMeter 調用 /bdp/city/create.json 接口:
之后看 Redis 存儲:
可以看出 provinceCities 方法的緩存已被清理掉,insertCity 方法的緩存奏效。
然后驗證 renameCity 方法的緩存配置,JMeter 調用 /bdp/city/rename.json 接口:
之后再看 Redis 存儲:
searchCity 方法的緩存也已被清理,renameCity 方法的緩存也奏效。
7. 注意事項
- 要緩存的 Java 對象必須實現 Serializable 接口,因為 Spring 會將對象先序列化再存入 Redis,比如本文中的 com.defonds.bdp.city.bean.City 類,如果不實現 Serializable 的話將會遇到類似這種錯誤:nested exception is java.lang.IllegalArgumentException: DefaultSerializer requires a Serializable payload but received an object of type [com.defonds.bdp.city.bean.City]]。
- 緩存的生命周期我們可以配置,然后托管 Spring CacheManager,不要試圖通過 redis-cli 命令行去管理緩存。比如 provinceCities 方法的緩存,某個省份的查詢結果會被以 key-value 的形式存放在 Redis,key 就是我們剛才自定義生成的 key,value 是序列化后的對象,這個 key 會被放在 key 名為 provinceCities~keys key-value 存儲中,參考下圖"provinceCities 方法在 Redis 中的緩存情況"。可以通過 redis-cli 使用 del 命令將 provinceCities~keys 刪除,但每個省份的緩存卻不會被清除。
- CacheManager 必須設置緩存過期時間,否則緩存對象將永不過期,這樣做的原因如上,避免一些野數據“永久保存”。此外,設置緩存過期時間也有助於資源利用最大化,因為緩存里保留的永遠是熱點數據。
- 緩存適用於讀多寫少的場合,查詢時緩存命中率很低、寫操作很頻繁等場景不適宜用緩存。
后記
本文完整 Eclipse 下的開發項目示例已上傳 CSDN 資源,有興趣的朋友可以去下載下來參考:http://download.csdn.net/detail/defonds/9137505。
參考資料
http://blog.csdn.net/defonds/article/details/48716161
本文介紹了如何使用注解的方式,將Redis緩存整合到你的Spring項目。
首先我們將使用jedis驅動,進而開始配置我們的Gradle。
group 'com.gkatzioura.spring' version '1.0-SNAPSHOT' apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'idea' apply plugin: 'spring-boot' buildscript { repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE") } } jar { baseName = 'gs-serving-web-content' version = '0.1.0' } sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { compile "org.springframework.boot:spring-boot-starter-thymeleaf" compile 'org.slf4j:slf4j-api:1.6.6' compile 'ch.qos.logback:logback-classic:1.0.13' compile 'redis.clients:jedis:2.7.0' compile 'org.springframework.data:spring-data-redis:1.5.0.RELEASE' testCompile group: 'junit', name: 'junit', version: '4.11' } task wrapper(type: Wrapper) { gradleVersion = '2.3' }
緊接着我們將使用Spring注解,繼續執行Redis裝載配置。
package com.gkatzioura.spring.config; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport { @Bean public JedisConnectionFactory redisConnectionFactory() { JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(); jedisConnectionFactory.setUsePool(true); return jedisConnectionFactory; } @Bean public RedisSerializer redisStringSerializer() { StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); return stringRedisSerializer; } @Bean(name="redisTemplate") public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf,RedisSerializer redisSerializer) { RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>(); redisTemplate.setConnectionFactory(cf); redisTemplate.setDefaultSerializer(redisSerializer); return redisTemplate; } @Bean public CacheManager cacheManager() { return new RedisCacheManager(redisTemplate(redisConnectionFactory(),redisStringSerializer())); } }
下一步將創建緩存接口CacheService。
package com.gkatzioura.spring.cache; import java.util.Date; import java.util.List; public interface CacheService { public void addMessage(String user,String message); public List<String> listMessages(String user); }
當然用戶既可以增加一條消息也能取回一條消息。因此,在實現過程中,用戶相關信息的存在時間將默認設為一分鍾。
我們用Redis來繼承實現CacheService接口。
package com.gkatzioura.spring.cache.impl; import com.gkatzioura.spring.cache.CacheService; import org.springframework.data.redis.core.ListOperations; import org.springframework.data.redis.core.RedisOperations; import org.springframework.data.redis.core.SetOperations; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.time.ZonedDateTime; import java.time.temporal.ChronoUnit; import java.util.Date; import java.util.List; @Service("cacheService") public class RedisService implements CacheService { @Resource(name = "redisTemplate") private ListOperations<String, String> messageList; @Resource(name = "redisTemplate") private RedisOperations<String,String> latestMessageExpiration; @Override public void addMessage(String user,String message) { messageList.leftPush(user,message); ZonedDateTime zonedDateTime = ZonedDateTime.now(); Date date = Date.from(zonedDateTime.plus(1, ChronoUnit.MINUTES).toInstant()); latestMessageExpiration.expireAt(user,date); } @Override public List<String> listMessages(String user) { return messageList.range(user,0,-1); } }
我們的緩存機制將保留每個用戶發送的消息列表。為了實現這個功能我們將調用ListOperations接口,同時將每個user作為一個key鍵值。通過RedisOperations接口,我們可以為key設置特定存在時長。在本例中,主要使用的是 user key。
下一步我們將創建一個controller注入緩存服務。
package com.gkatzioura.spring.controller; import com.gkatzioura.spring.cache.CacheService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController public class MessageController { @Autowired private CacheService cacheService; @RequestMapping(value = "/message",method = RequestMethod.GET) @ResponseBody public List<String> greeting(String user) { List<String> messages = cacheService.listMessages(user); return messages; } @RequestMapping(value = "/message",method = RequestMethod.POST) @ResponseBody public String saveGreeting(String user,String message) { cacheService.addMessage(user,message); return "OK"; } }
最后完成類Application的創建。
package com.gkatzioura.spring; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
經過如上步驟,接下來直接運行Application即可。
原文鏈接:Integrate Redis into a Spring Project( 譯者/丘志鵬 審校/朱正貴 責編/仲浩)
http://www.csdn.net/article/2015-09-01/2825600
使用Spring Cache + Redis + Jackson Serializer緩存數據庫查詢結果中序列化問題的解決
應用場景
我們希望通過緩存來減少對關系型數據庫的查詢次數,減輕數據庫壓力。在執行DAO類的select***()
, query***()
方法時,先從Redis
中查詢有沒有緩存數據,如果有則直接從Redis
拿到結果,如果沒有再向數據庫發起查詢請求取數據。
序列化問題
要把domain object做為key-value對保存在redis中,就必須要解決對象的序列化問題。Spring Data Redis給我們提供了一些現成的方案:
JdkSerializationRedisSerializer
. 使用JDK提供的序列化功能。 優點是反序列化時不需要提供類型信息(class
),但缺點是序列化后的結果非常龐大,是JSON格式的5倍左右,這樣就會消耗redis服務器的大量內存。Jackson2JsonRedisSerializer
. 使用Jackson
庫將對象序列化為JSON字符串。優點是速度快,序列化后的字符串短小精悍。
但缺點也非常致命,那就是此類的構造函數中有一個類型參數,必須提供要序列化對象的類型信息(.class
對象)。 通過查看源代碼,發現其只在反序列化過程中用到了類型信息。
如果用方案一,就必須付出緩存多占用4倍內存的代價,實在承受不起。如果用方案二,則必須給每一種domain對象都配置一個Serializer,即如果我的應用里有100種domain對象,那就必須在spring配置文件中配置100個Jackson2JsonRedisSerializer
,這顯然是不現實的。
通過google, 發現spring data redis項目中有一個#145 pull request, 而這個提交請求的內容正是解決Jackson
必須提供類型信息的問題。然而不幸的是這個請求還沒有被merge
。但我們可以把代碼copy一下放到自己的項目中:
/** * @author Christoph Strobl * @since 1.6 */ public class GenericJackson2JsonRedisSerializer implements RedisSerializer<Object> { private final ObjectMapper mapper; /** * Creates {@link GenericJackson2JsonRedisSerializer} and configures {@link ObjectMapper} for default typing. */ public GenericJackson2JsonRedisSerializer() { this((String) null); } /** * Creates {@link GenericJackson2JsonRedisSerializer} and configures {@link ObjectMapper} for default typing using the * given {@literal name}. In case of an {@literal empty} or {@literal null} String the default * {@link JsonTypeInfo.Id#CLASS} will be used. * * @param classPropertyTypeName Name of the JSON property holding type information. Can be {@literal null}. */ public GenericJackson2JsonRedisSerializer(String classPropertyTypeName) { this(new ObjectMapper()); if (StringUtils.hasText(classPropertyTypeName)) { mapper.enableDefaultTypingAsProperty(DefaultTyping.NON_FINAL, classPropertyTypeName); } else { mapper.enableDefaultTyping(DefaultTyping.NON_FINAL, As.PROPERTY); } } /** * Setting a custom-configured {@link ObjectMapper} is one way to take further control of the JSON serialization * process. For example, an extended {@link SerializerFactory} can be configured that provides custom serializers for * specific types. * * @param mapper must not be {@literal null}. */ public GenericJackson2JsonRedisSerializer(ObjectMapper mapper) { Assert.notNull(mapper, "ObjectMapper must not be null!"); this.mapper = mapper; } /* * (non-Javadoc) * @see org.springframework.data.redis.serializer.RedisSerializer#serialize(java.lang.Object) */ @Override public byte[] serialize(Object source) throws SerializationException { if (source == null) { return SerializationUtils.EMPTY_ARRAY; } try { return mapper.writeValueAsBytes(source); } catch (JsonProcessingException e) { throw new SerializationException("Could not write JSON: " + e.getMessage(), e); } } /* * (non-Javadoc) * @see org.springframework.data.redis.serializer.RedisSerializer#deserialize(byte[]) */ @Override public Object deserialize(byte[] source) throws SerializationException { return deserialize(source, Object.class); } /** * @param source can be {@literal null}. * @param type must not be {@literal null}. * @return {@literal null} for empty source. * @throws SerializationException */ public <T> T deserialize(byte[] source, Class<T> type) throws SerializationException { Assert.notNull(type, "Deserialization type must not be null! Pleaes provide Object.class to make use of Jackson2 default typing."); if (SerializationUtils.isEmpty(source)) { return null; } try { return mapper.readValue(source, type); } catch (Exception ex) { throw new SerializationException("Could not read JSON: " + ex.getMessage(), ex); } } }
然后在配置文件中使用這個GenericJackson2JsonRedisSerializer
:
<bean id="jacksonSerializer" class="com.fh.taolijie.component.GenericJackson2JsonRedisSerializer"> </bean>
重新構建部署,我們發現這個serializer可以同時支持多種不同類型的domain對象,問題解決。
http://www.myexception.cn/database/1958643.html
spring-data-redis提供了多種serializer策略,這對使用jedis的開發者而言,實在是非常便捷。sdr提供了4種內置的serializer:
- JdkSerializationRedisSerializer:使用JDK的序列化手段(serializable接口,ObjectInputStrean,ObjectOutputStream),數據以字節流存儲
- StringRedisSerializer:字符串編碼,數據以string存儲
- JacksonJsonRedisSerializer:json格式存儲
- OxmSerializer:xml格式存儲
其中JdkSerializationRedisSerializer和StringRedisSerializer是最基礎的序列化策略,其中“JacksonJsonRedisSerializer”與“OxmSerializer”都是基於stirng存儲,因此它們是較為“高級”的序列化(最終還是使用string解析以及構建java對象)。
RedisTemplate中需要聲明4種serializer,默認為“JdkSerializationRedisSerializer”:
1) keySerializer :對於普通K-V操作時,key采取的序列化策略
2) valueSerializer:value采取的序列化策略
3) hashKeySerializer: 在hash數據結構中,hash-key的序列化策略
4) hashValueSerializer:hash-value的序列化策略
無論如何,建議key/hashKey采用StringRedisSerializer。
接下來,通過實例描述如何使用它們,可以首先參考“spring-data-redis特性”: