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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

redis使用@Cacheable等注解为接口添加缓存

發布時間:2025/3/16 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 redis使用@Cacheable等注解为接口添加缓存 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

緩存處理方式應該是

  • 先從緩存中拿數據,如果有,直接返回。
  • 如果拿到的為空,則數據庫查詢,然后將查詢結果存到緩存中。
  • 由此實現方式應該如下:

    private String baseKey = "category";public CmfCategories selectByPrimaryKey(Long id) {//1. 先從緩存中取CmfCategories cmfCategories = redisUtils.get(baseKey + id, CmfCategories.class);if (cmfCategories == null) { //如果取值為空//2. 從數據中查詢cmfCategories = cmfCategoriesMapper.selectByPrimaryKey(id);//3. 將查詢結果存入緩存redisUtils.set(baseKey + id, cmfCategories, DEFAULT_EXPIRE * 7);}return cmfCategories;}


    這種方式是沒錯的,但就是實現起來,每個接口都要做一遍重復的操作,下面演示一種簡潔的使用注解實現方式:

    @Cacheable(value = "newsCategory", key = "'newsCategory:'+#id", unless = "#result==null")public CmfCategories selectByPrimaryKey(Long id) {return cmfCategoriesMapper.selectByPrimaryKey(id);}


    明顯簡單多了,而且**對代碼無侵入**!

    實現步驟

    1、添加maven依賴

    <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId> </dependency>

    ?

    2、添加配置

    /*** Redis緩存配置。*/ @Configuration @EnableCaching public class RedisCacheConfig {@Autowiredprivate RedisConnectionFactory factory;@Beanpublic CacheManager cacheManager() {RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate());// 默認緩存一天 86400秒redisCacheManager.setDefaultExpiration(86400L);return redisCacheManager;}@Beanpublic RedisTemplate<String, Object> redisTemplate() {RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(factory);// 字符串Key序列化StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();redisTemplate.setKeySerializer(stringRedisSerializer);redisTemplate.setHashKeySerializer(stringRedisSerializer);// 對象值序列化ObjectRedisSerializer objectRedisSerializer = new ObjectRedisSerializer();redisTemplate.setValueSerializer(objectRedisSerializer);redisTemplate.setHashValueSerializer(objectRedisSerializer);return redisTemplate;}}


    3、具體使用


    在需要緩存的接口上添加注解

    @Cacheable(value = "newsCategory", key = "'newsCategory:'+#id", unless = "#result==null")public CmfCategories selectByPrimaryKey(Long id) {return cmfCategoriesMapper.selectByPrimaryKey(id);}


    當被緩存的數據被更新的時候,可以使用@CacheEvict來清除緩存,則可以保證緩存的數據是最新的

    @CacheEvict(value = "User", key = "'User:'+#userParam.userId", condition = "#userParam!=null")public long setUserBasicInfo(UserBasicInfo userParam, String token) {//do something}


    簡單講解

    ? ??參考鏈接

    ? ? 緩存數據

    ? ? 對于緩存的操作,主要有:@Cacheable、@CachePut、@CacheEvict。

    • @Cacheable

    ? ? Spring 在執行 @Cacheable 標注的方法前先查看緩存中是否有數據,如果有數據,則直接返回緩存數據;若沒有數據,執行該方法并將方法返回值放進緩存。 參數: value緩存名、 key緩存鍵值、 condition滿足緩存條件、unless否決緩存條件

    @Cacheable(value = "user", key = "#id") public User findById(final Long id) { System.out.println("cache miss, invoke find by id, id:" + id); for (User user : users) { if (user.getId().equals(id)) { return user; } } return null; }

    ?

    • @CachePut

    ? ? 和 @Cacheable 類似,但會把方法的返回值放入緩存中, 主要用于數據新增和修改方法。

    @CachePut(value = "user", key = "#user.id") public User save(User user) { users.add(user); return user; }

    ?

    • @CacheEvict

    方法執行成功后會從緩存中移除相應數據。 參數: value緩存名、 key緩存鍵值、 condition滿足緩存條件、 unless否決緩存條件、 allEntries是否移除所有數據(設置為true時會移除所有緩存)

    @CacheEvict(value = "user", key = "#user.id") // 移除指定key的數據 public User delete(User user) { users.remove(user); return user; } @CacheEvict(value = "user", allEntries = true) // 移除所有數據 public void deleteAll() { users.clear(); }

    原文鏈接:https://blog.csdn.net/sinstar1/article/details/82151249

    總結

    以上是生活随笔為你收集整理的redis使用@Cacheable等注解为接口添加缓存的全部內容,希望文章能夠幫你解決所遇到的問題。

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