1.redis为ä»ä¹è¦åºåå
2.javaå¦ä½å¯¼å
¥å
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 redux 源码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æ°æ®åºä½¿ç¨å ¥é¨æç¨æ ç®è¿è¡å¦ä¹ ï¼
javaå¦ä½å¯¼å ¥å
1ãé¦å å¨é¡¹ç®ä¸å建ä¸ä¸ªæ°çæ件夹ï¼ç¨æ¥ä¿åjarå ãå¨é¡¹ç®åä¸ç¹å»é¼ æ å³é®ï¼æ顺åºç¹å»ãNewãâãFloderãï¼æå¼æ°å»ºæ件夹ççªå£2ãè¾å ¥æ件夹å称ãlibãï¼ç¹å»ãokããé常å¨libæ件夹ä¸åæ¾ä»å¤é¨å¼å ¥çjarå
3ãæ¾å°è¦å¼å ¥çjarå ï¼é¼ æ éä¸éè¦ç¨çjarå ï¼ç¶åæä½é¼ æ å·¦é®ä¸æ¾ï¼æjarå æå¨å°libæ件夹ä¸ãåæè æ¯å å¤å¶jarå ï¼æ¥çå¨libæ件夹ä¸å³å»ï¼éæ©å¤å¶ãæå¼éæ©æ¡ï¼å¨å¼¹åºçéæ©æ¡ä¸éæ©é»è®¤çãcopy filesãï¼ç¹å»ãOKãå ³éãæ¥çå°±å¯ä»¥å¨libæ件夹ä¸çå°å¤å¶æåçjarå ã
4ãè¿æ¶ï¼åªæ¯æjarå å¤å¶å°é¡¹ç®ä¸ï¼è¿ä¸è½å¤ä½¿ç¨ãéè¦åå¨é¡¹ç®åä¸ç¹å»é¼ æ å³é®ï¼æ顺åºéæ©
ãBuild PathãâãConfigure Build Path...ãã
5ãå¨æå¼ççªå£ä¸ï¼éä¸ãLibrariesã页ï¼ä»å³è¾¹ä¸æ çæé®ä¸ç¹å»ãadd JARs...ã
6ãå¨æå¼ççªå£ä¸ï¼æç §é¡ºåºå±å¼æ¬é¡¹ç®ålibæ件夹ï¼ç¶åéä¸ååå¤å¶å°é¡¹ç®ä¸çjarå ï¼ç¹å»ãOKãå ³éçªå£
7ãå¨ååæå¼çãLibrariesã页é¢ä¸ï¼å¯ä»¥çå°ååå¼å ¥çjarå å称ãç¹å»ãOKã确认ã
8ãè¿ä¸ªæ¶åï¼å¨ãEclipseãä¸ï¼å°±å¯ä»¥æ¾å°å¹¶ä¸å¼å§ä½¿ç¨è¿ä¸ªjarå äºã