日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【过程记录】springboot整合redis/分别用redisRepository和redistemplate操作redis

發布時間:2024/9/30 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【过程记录】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

/** * 通過用戶id獲取菜單列表 * * @return */ @Override public List<Menu> getMenusByAdminId() { Integer adminId = ((Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId(); ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue(); //查詢緩存中是否有數據 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 {@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:

@Repository public interface StuRepository extends CrudRepository<stu,String> { stu findByName(String name); //自定義查詢方法,使用了@Indexed的name屬性查詢 }

調用:

@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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。