SpringBoot+Redis

Redis

NoSql,非關(guān)系型數(shù)據(jù)庫,支持多種數(shù)據(jù)類型的存儲。

項目地址

https://github.com/Inverseli/SpringBoot-Learning/tree/master/learning3-2-4

note

  • 儲存對象需要進行序列化操作,可自定義序列化。
  • 序列化(Object->byte)對象儲存的一種方式
  • redis默認端口6379,默認密碼為空
  • redis常用配置
# REDIS (RedisProperties)
# Redis數(shù)據(jù)庫索引(默認為0)
spring.redis.database=0
# Redis服務(wù)器地址
spring.redis.host=localhost
# Redis服務(wù)器連接端口
spring.redis.port=6379
# Redis服務(wù)器連接密碼(默認為空)
spring.redis.password=
# 連接池最大連接數(shù)(使用負值表示沒有限制)
spring.redis.pool.max-active=8
# 連接池最大阻塞等待時間(使用負值表示沒有限制)
spring.redis.pool.max-wait=-1
# 連接池中的最大空閑連接
spring.redis.pool.max-idle=8
# 連接池中的最小空閑連接
spring.redis.pool.min-idle=0
# 連接超時時間(毫秒)
spring.redis.timeout=5000

RedisConfig

package com.inverseli.learning;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import com.inverseli.learning.domain.User;

/**  
 * @date:2018年9月26日 下午12:27:59    
 * @author liyuhao
 * @version 1.0   
 * @since JDK 1.8  
 * @description:  
 */
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String,User> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String,User> template = new RedisTemplate<String,User>();
        template.setConnectionFactory(redisConnectionFactory);
        RedisSerializer<String> stringSerializer = new StringRedisSerializer();
        template.setKeySerializer(stringSerializer);
        template.setValueSerializer(new RedisObjectSerializer());
        return template;
    }
}
 

踩過的坑

  • 注意依賴的starter版本
  • 對象要實現(xiàn) Serializable 接口
  • 項目開始之前一定要弄清楚版本問題(重要),要不然越往后越麻煩
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容