springboot使用redisRepository和redistemplate操作redis的過(guò)程解析
導(dǎo)入依賴
菜單大部分情況下不會(huì)出現(xiàn)變化,我們可以將其放入Redis 加快加載速度
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- commons-pool2 對(duì)象池依賴 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency>
基本配置
redis: timeout: 10000ms # 連接超時(shí)時(shí)間 host: 192.168.10.100 # Redis服務(wù)器地址 port: 6379 # Redis服務(wù)器端口 database: 0 # 選擇哪個(gè)庫(kù),默認(rèn)0庫(kù) lettuce: pool: max-active: 1024 # 最大連接數(shù),默認(rèn) 8 max-wait: 10000ms # 最大連接阻塞等待時(shí)間,單位毫秒,默認(rèn) -1 max-idle: 200 # 最大空閑連接,默認(rèn) 8 min-idle: 5 # 最小空閑連接,默認(rèn) 0
使用RedisTemplate訪問(wèn)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設(shè)置序列器 redisTemplate.setKeySerializer(new StringRedisSerializer()); //為string類型value設(shè)置序列器 redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); //為hash類型key設(shè)置序列器 redisTemplate.setHashKeySerializer(new StringRedisSerializer()); //為hash類型value設(shè)置序列器 redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.setConnectionFactory(redisConnectionFactory); return redisTemplate; } }
修改菜單方法:
MenuServiceImpl.java
/** * 通過(guò)用戶id獲取菜單列表 * * @return */ @Override public List<Menu> getMenusByAdminId() { Integer adminId = ((Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId(); ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue(); //查詢緩存中是否有數(shù)據(jù) List<Menu> menus = (List<Menu>) valueOperations.get("menu_" + adminId); if (CollectionUtils.isEmpty(menus)){ menus = menuMapper.getMenusByAdminId(adminId); valueOperations.set("menu_"+adminId,menus); } return menus; }
使用Redisrepository訪問(wèn)redis
需要聲明一配置項(xiàng)用于啟用Repository以及模板
public class RedisConfig { @Bean public LettuceConnectionFactory redisConnectionFactory() { RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration("127.0.0.1", 6567); redisStandaloneConfiguration.setPassword("password");//如果有密碼需要通過(guò)這個(gè)函數(shù)設(shè)置密碼 return new LettuceConnectionFactory(redisStandaloneConfiguration); } @Bean public JedisConnectionFactory jedisConnectionFactory(){ //如果使用jedis作為客戶端也需要聲明該bean 使用與上面的類似 } @Bean public 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; } }
實(shí)例:
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; @Indexed private String name; private Gender gender; //RedisHash注解用于聲明該實(shí)體將被存儲(chǔ)與RedisHash中,如果需要使用repository的數(shù)據(jù)訪問(wèn)形式,這個(gè)注解是必須用到的 timetoLive為實(shí)體對(duì)象在數(shù)據(jù)庫(kù)的有效期 //@Indexed用于標(biāo)注需要作為查詢條件的屬性 }
寫(xiě)接口
repository:
@Repository public interface StuRepository extends CrudRepository<stu,String> { stu findByName(String name); //自定義查詢方法,使用了@Indexed的name屬性查詢 }
調(diào)用:
@SpringBootTest public class RedisApplicationTests { @Autowired StuRepository stuRepository; @Test void testSave(){ stu student = new stu().setId("0002").setName("xiaoming").setGender(stu.Gender.FEMALE); stuRepository.save(student);//根據(jù)id更新或者新增記錄 } @Test void testFindBy(){ //使用主鍵查詢 assert stuRepository.findById("0002").isPresent(); //根據(jù)自定義方法查詢 assert stuRepository.findByName("xiaoming") !=null; } }
到此這篇關(guān)于springboot使用redisRepository和redistemplate操作redis的過(guò)程解析的文章就介紹到這了,更多相關(guān)springboot整合redis內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot整合Redis使用RedisTemplate和StringRedisTemplate
- springboot使用redisTemplate操作lua腳本
- SpringBoot整合Redis使用@Cacheable和RedisTemplate
- Redis6搭建集群并在SpringBoot中使用RedisTemplate的實(shí)現(xiàn)
- 詳解SpringBoot使用RedisTemplate操作Redis的5種數(shù)據(jù)類型
- SpringBoot使用RedisTemplate.delete刪除指定key失敗的解決辦法
- Spring Boot單元測(cè)試中使用mockito框架mock掉整個(gè)RedisTemplate的示例
- Spring Boot中RedisTemplate的使用示例詳解
相關(guān)文章
SpringBoot+Ajax+redis實(shí)現(xiàn)隱藏重要接口地址的方法
這篇文章主要介紹了SpringBoot+Ajax+redis實(shí)現(xiàn)隱藏重要接口地址,本篇文章主要講訴使用SpringBoot項(xiàng)目配合Ajax和redis實(shí)現(xiàn)隱藏重要接口地址,這里我以隱藏秒殺地址為例,需要的朋友可以參考下2024-03-03Jpa?Specification如何實(shí)現(xiàn)and和or同時(shí)使用查詢
這篇文章主要介紹了Jpa?Specification如何實(shí)現(xiàn)and和or同時(shí)使用查詢,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11java實(shí)現(xiàn)基于UDP協(xié)議網(wǎng)絡(luò)Socket編程(C/S通信)
這篇文章主要介紹了java實(shí)現(xiàn)基于UDP協(xié)議網(wǎng)絡(luò)Socket編程(C/S通信),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10Map如何根據(jù)key指定條件進(jìn)行過(guò)濾篩選
這篇文章主要介紹了Map如何根據(jù)key指定條件進(jìn)行過(guò)濾篩選問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09SpringBoot使用Jackson配置全局時(shí)間日期格式
本文主要介紹了SpringBoot使用Jackson配置全局時(shí)間日期格式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05