1.redis为ä»ä¹è¦åºåå
redis为ä»ä¹è¦åºåå
åºååæç»çç®çæ¯ä¸ºäºå¯¹è±¡å¯ä»¥è·¨å¹³å°åå¨ï¼åè¿è¡ç½ç»ä¼ è¾ãèæ们è¿è¡è·¨å¹³å°åå¨åç½ç»ä¼ è¾çæ¹å¼å°±æ¯IOï¼èæ们çIOæ¯æçæ°æ®æ ¼å¼å°±æ¯åèæ°ç»ã ï¼æ¨èå¦ä¹ ï¼Redisè§é¢æç¨ï¼
éè¿ä¸é¢ææ³ä½ å·²ç»ç¥éäºå¡æ¯éè¦è¿è¡â跨平å°åå¨âåâç½ç»ä¼ è¾âçæ°æ®ï¼é½éè¦è¿è¡åºååã
æ¬è´¨ä¸åå¨åç½ç»ä¼ è¾ é½éè¦ç»è¿ æä¸ä¸ªå¯¹è±¡ç¶æä¿åæä¸ç§è·¨å¹³å°è¯å«çåèæ ¼å¼ï¼ç¶åå ¶ä»çå¹³å°æå¯ä»¥éè¿åèä¿¡æ¯è§£æè¿å对象信æ¯ã
redisåºååæ¹å¼å¯¹æ¯ï¼
redisçé»è®¤æ¹å¼æ¯JdkSerializationRedisSerializer
JdkSerializationRedisSerializer: 使ç¨JDKæä¾çåºåååè½ã
ä¼ç¹æ¯ååºååæ¶ä¸éè¦æä¾ç±»åä¿¡æ¯(class)ï¼ä½ç¼ºç¹æ¯éè¦å®ç°Serializableæ¥å£ï¼è¿æåºåååçç»æé常åºå¤§ï¼æ¯JSONæ ¼å¼ç5åå·¦å³ï¼è¿æ ·å°±ä¼æ¶èredisæå¡å¨ç大éå åã
Jackson2JsonRedisSerializerï¼ ä½¿ç¨Jacksonåºå°å¯¹è±¡åºåå为JSONå符串ã
ä¼ç¹æ¯é度快ï¼åºåååçå符串çå°ç²¾æï¼ä¸éè¦å®ç°Serializableæ¥å£ã
ä½ç¼ºç¹ä¹é常è´å½ï¼é£å°±æ¯æ¤ç±»çæé å½æ°ä¸æä¸ä¸ªç±»ååæ°ï¼å¿ é¡»æä¾è¦åºåå对象çç±»åä¿¡æ¯(.class对象)ã éè¿æ¥çæºä»£ç ï¼åç°å ¶åªå¨ååºååè¿ç¨ä¸ç¨å°äºç±»åä¿¡æ¯ã
é®é¢ï¼ä½¿ç¨é»è®¤çJDKåºååæ¹å¼ï¼å¨RDMå·¥å ·ä¸æ¥çk-vå¼æ¶ä¼åºç°âä¹±ç âï¼ä¸æ¹ä¾¿æ¥çã
解å³ï¼èªå®ä¹ç³»ååæ¹å¼ï¼ä½¿ç¨Jackson2JsonRedisSerializer
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/
*** Redisé ç½®
*
* @author LinJie
*/
@Configuration
public class RedisConfig {
/
*** Redis 码分游戏源码gm开发repository redis repository.
*
* @param redisTemplate the redis template
* @return the redis repository
*/
@Bean
public RedisRepository redisRepository(RedisTemplate redisTemplate) {
// 使ç¨Jackson2JsonRedisSerialize æ¿æ¢é»è®¤åºåå
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// keyéç¨Stringçåºååæ¹å¼
redisTemplate.setKeySerializer(stringRedisSerializer);
// hashçkeyä¹éç¨Stringçåºååæ¹å¼
redisTemplate.setHashKeySerializer(stringRedisSerializer);
// valueåºååæ¹å¼éç¨jackson
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
// hashçvalueåºååæ¹å¼éç¨jackson
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return new RedisRepository(redisTemplate);
}
}æ´å¤Redisç¸å ³ææ¯æç« ï¼è¯·è®¿é®Redisæ°æ®åºä½¿ç¨å ¥é¨æç¨æ ç®è¿è¡å¦ä¹ ï¼