springboot使用redisRepository和redistemplate操作redis的過程解析
導入依賴
菜單大部分情況下不會出現(xiàn)變化,我們可以將其放入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 # 最大連接數(shù),默認 8 max-wait: 10000ms # 最大連接阻塞等待時間,單位毫秒,默認 -1 max-idle: 200 # 最大空閑連接,默認 8 min-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
/** * 通過用戶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訪問redis
需要聲明一配置項用于啟用Repository以及模板
public class RedisConfig { @Bean public LettuceConnectionFactory redisConnectionFactory() { RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration("127.0.0.1", 6567); redisStandaloneConfiguration.setPassword("password");//如果有密碼需要通過這個函數(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; } }
實例:
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注解用于聲明該實體將被存儲與RedisHash中,如果需要使用repository的數(shù)據(jù)訪問形式,這個注解是必須用到的 timetoLive為實體對象在數(shù)據(jù)庫的有效期 //@Indexed用于標注需要作為查詢條件的屬性 }
寫接口
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; } }
到此這篇關于springboot使用redisRepository和redistemplate操作redis的過程解析的文章就介紹到這了,更多相關springboot整合redis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- SpringBoot整合Redis使用RedisTemplate和StringRedisTemplate
- springboot使用redisTemplate操作lua腳本
- SpringBoot整合Redis使用@Cacheable和RedisTemplate
- Redis6搭建集群并在SpringBoot中使用RedisTemplate的實現(xiàn)
- 詳解SpringBoot使用RedisTemplate操作Redis的5種數(shù)據(jù)類型
- SpringBoot使用RedisTemplate.delete刪除指定key失敗的解決辦法
- Spring Boot單元測試中使用mockito框架mock掉整個RedisTemplate的示例
- Spring Boot中RedisTemplate的使用示例詳解
相關文章
SpringBoot+Ajax+redis實現(xiàn)隱藏重要接口地址的方法
這篇文章主要介紹了SpringBoot+Ajax+redis實現(xiàn)隱藏重要接口地址,本篇文章主要講訴使用SpringBoot項目配合Ajax和redis實現(xiàn)隱藏重要接口地址,這里我以隱藏秒殺地址為例,需要的朋友可以參考下2024-03-03Jpa?Specification如何實現(xiàn)and和or同時使用查詢
這篇文章主要介紹了Jpa?Specification如何實現(xiàn)and和or同時使用查詢,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11java實現(xiàn)基于UDP協(xié)議網(wǎng)絡Socket編程(C/S通信)
這篇文章主要介紹了java實現(xiàn)基于UDP協(xié)議網(wǎng)絡Socket編程(C/S通信),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-10-10