【过程记录】springboot整合redis/分别用redisRepository和redistemplate操作redis
生活随笔
收集整理的這篇文章主要介紹了
【过程记录】springboot整合redis/分别用redisRepository和redistemplate操作redis
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
- 導入依賴
- 基本配置
- 使用RedisTemplate訪問redis
- 使用Redisrepository訪問redis
- 實例:
導入依賴
菜單大部分情況下不會出現變化,我們可以將其放入Redis 加快加載速度
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- commons-pool2 對象池依賴 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency>基本配置
redis:timeout: 10000ms # 連接超時時間host: 192.168.10.100 # Redis服務器地址port: 6379 # Redis服務器端口database: 0 # 選擇哪個庫,默認0庫lettuce:pool:max-active: 1024 # 最大連接數,默認 8max-wait: 10000ms # 最大連接阻塞等待時間,單位毫秒,默認 -1max-idle: 200 # 最大空閑連接,默認 8min-idle: 5 # 最小空閑連接,默認 0使用RedisTemplate訪問redis
RedisConfig.java
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; /** * Redis配置類 * * @author zhoubin * @since 1.0.0 */ @Configuration public class RedisConfig { @Bean public RedisTemplate<String,Object> redisTemplate(LettuceConnectionFactory redisConnectionFactory){ RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>(); //為string類型key設置序列器 redisTemplate.setKeySerializer(new StringRedisSerializer()); //為string類型value設置序列器 redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); //為hash類型key設置序列器 redisTemplate.setHashKeySerializer(new StringRedisSerializer()); //為hash類型value設置序列器 redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.setConnectionFactory(redisConnectionFactory); return redisTemplate; } }修改菜單方法:
MenuServiceImpl.java
使用Redisrepository訪問redis
需要聲明一配置項用于啟用Repository以及模板
public class RedisConfig {@Beanpublic LettuceConnectionFactory redisConnectionFactory() {RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration("127.0.0.1", 6567);redisStandaloneConfiguration.setPassword("password");//如果有密碼需要通過這個函數設置密碼return new LettuceConnectionFactory(redisStandaloneConfiguration);}@Beanpublic JedisConnectionFactory jedisConnectionFactory(){//如果使用jedis作為客戶端也需要聲明該bean 使用與上面的類似 } @Beanpublic RedisTemplate<?,?> redisTemplate(){RedisTemplate<String,Object> template = new RedisTemplate<>();RedisSerializer<String> stringSerializer = new StringRedisSerializer();JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();template.setConnectionFactory(redisConnectionFactory());template.setKeySerializer(stringSerializer);template.setHashKeySerializer(stringSerializer);template.setValueSerializer(stringSerializer);template.setHashValueSerializer(jdkSerializationRedisSerializer);template.setEnableTransactionSupport(true);template.afterPropertiesSet();return template;} }實例:
entity
import lombok.Data; import lombok.experimental.Accessors; import org.springframework.data.redis.core.RedisHash; import org.springframework.data.redis.core.index.Indexed;@Accessors(chain = true) @Data @RedisHash(value="Student",timeToLive = 10) public class stu implements Serializable {public enum Gender{MALE,FEMALE}private String id;@Indexedprivate String name;private Gender gender;//RedisHash注解用于聲明該實體將被存儲與RedisHash中,如果需要使用repository的數據訪問形式,這個注解是必須用到的 timetoLive為實體對象在數據庫的有效期//@Indexed用于標注需要作為查詢條件的屬性 }寫接口
repository:
調用:
@SpringBootTest public class RedisApplicationTests {@AutowiredStuRepository stuRepository;@Testvoid testSave(){stu student = new stu().setId("0002").setName("xiaoming").setGender(stu.Gender.FEMALE);stuRepository.save(student);//根據id更新或者新增記錄}@Testvoid testFindBy(){//使用主鍵查詢assert stuRepository.findById("0002").isPresent();//根據自定義方法查詢assert stuRepository.findByName("xiaoming") !=null;} } 與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的【过程记录】springboot整合redis/分别用redisRepository和redistemplate操作redis的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【学习笔记】mybatis中的缓存介绍和
- 下一篇: 【学习笔记】springboot中的全局