宅男在线永久免费观看网直播,亚洲欧洲日产国码无码久久99,野花社区在线观看视频,亚洲人交乣女bbw,一本一本久久a久久精品综合不卡

全部
常見問題
產品動態(tài)
精選推薦

使用集中式緩存Redis

管理 管理 編輯 刪除

之前我們介紹了兩種進程內緩存的用法,包括Spring Boot默認使用的ConcurrentMap緩存以及緩存框架EhCache。雖然EhCache已經能夠適用很多應用場景,但是由于EhCache是進程內的緩存框架,在集群模式下時,各應用服務器之間的緩存都是獨立的,因此在不同服務器的進程間會存在緩存不一致的情況。即使EhCache提供了集群環(huán)境下的緩存同步策略,但是同步依然是需要一定的時間,短暫的緩存不一致依然存在。

在一些要求高一致性(任何數(shù)據變化都能及時的被查詢到)的系統(tǒng)和應用中,就不能再使用EhCache來解決了,這個時候使用集中式緩存就可以很好的解決緩存數(shù)據的一致性問題。接下來我們就來學習一下,如何在Spring Boot的緩存支持中使用Redis實現(xiàn)數(shù)據緩存。

動手試試

本篇的實現(xiàn)將基于上一篇的基礎工程來進行。先來回顧下上一篇中的程序要素:

User實體的定義

@Entity
@Data
@NoArgsConstructor
public class User implements Serializable {

    @Id
    @GeneratedValue
    private Long id;

    private String name;
    private Integer age;

    public User(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
}

User實體的數(shù)據訪問實現(xiàn)(涵蓋了緩存注解)

@CacheConfig(cacheNames = "users")
public interface UserRepository extends JpaRepository<User, Long> {

    @Cacheable
    User findByName(String name);

}

下面開始改造這個項目:

第一步pom.xml中增加相關依賴:

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

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>
在Spring Boot 1.x的早期版本中,該依賴的名稱為spring-boot-starter-redis,所以在Spring Boot 1.x基礎教程open in new window中與這里不同。

第二步:配置文件中增加配置信息,以本地運行為例,比如:

spring.redis.host=localhost
spring.redis.port=6379
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=-1ms
spring.redis.lettuce.pool.min-idle=0
spring.redis.lettuce.shutdown-timeout=100ms
關于連接池的配置,注意幾點:Redis的連接池配置在1.x版本中前綴為spring.redis.pool與Spring Boot 2.x有所不同。在1.x版本中采用jedis作為連接池,而在2.x版本中采用了lettuce作為連接池以上配置均為默認值,實際上生產需進一步根據部署情況與業(yè)務要求做適當修改.

再來試試單元測試:

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class Chapter54ApplicationTests {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private CacheManager cacheManager;

    @Test
    public void test() throws Exception {
        System.out.println("CacheManager type : " + cacheManager.getClass());

        // 創(chuàng)建1條記錄
        userRepository.save(new User("AAA", 10));

        User u1 = userRepository.findByName("AAA");
        System.out.println("第一次查詢:" + u1.getAge());

        User u2 = userRepository.findByName("AAA");
        System.out.println("第二次查詢:" + u2.getAge());
    }

}

執(zhí)行測試輸出可以得到:

CacheManager type : class org.springframework.data.redis.cache.RedisCacheManager
Hibernate: select next_val as id_val from hibernate_sequence for update
Hibernate: update hibernate_sequence set next_val= ? where next_val=?
Hibernate: insert into user (age, name, id) values (?, ?, ?)
2020-08-12 16:25:26.954  INFO 68282 --- [           main] io.lettuce.core.EpollProvider            : Starting without optional epoll library
2020-08-12 16:25:26.955  INFO 68282 --- [           main] io.lettuce.core.KqueueProvider           : Starting without optional kqueue library
Hibernate: select user0_.id as id1_0_, user0_.age as age2_0_, user0_.name as name3_0_ from user user0_ where user0_.name=?
第一次查詢:10
第二次查詢:10

可以看到:

  1. 第一行輸出的CacheManager type為org.springframework.data.redis.cache.RedisCacheManager,而不是上一篇中的EhCacheCacheManager
  2. 第二次查詢的時候,沒有輸出SQL語句,所以是走的緩存獲取

整合成功!

注:本文轉載自“程序猿DD”,如有侵權,請聯(lián)系刪除!

請登錄后查看

哈哈哈醬 最后編輯于2025-01-15 17:04:09

快捷回復
回復
回復
回復({{post_count}}) {{!is_user ? '我的回復' :'全部回復'}}
排序 默認正序 回復倒序 點贊倒序

{{item.user_info.nickname ? item.user_info.nickname : item.user_name}} LV.{{ item.user_info.bbs_level || item.bbs_level }}

作者 管理員 企業(yè)

{{item.floor}}# 同步到gitee 已同步到gitee {{item.is_suggest == 1? '取消推薦': '推薦'}}
{{item.is_suggest == 1? '取消推薦': '推薦'}}
沙發(fā) 板凳 地板 {{item.floor}}#
{{item.user_info.title || '暫無簡介'}}
附件

{{itemf.name}}

{{item.created_at}}  {{item.ip_address}}
打賞
已打賞¥{{item.reward_price}}
{{item.like_count}}
{{item.showReply ? '取消回復' : '回復'}}
刪除
回復
回復

{{itemc.user_info.nickname}}

{{itemc.user_name}}

回復 {{itemc.comment_user_info.nickname}}

附件

{{itemf.name}}

{{itemc.created_at}}
打賞
已打賞¥{{itemc.reward_price}}
{{itemc.like_count}}
{{itemc.showReply ? '取消回復' : '回復'}}
刪除
回復
回復
查看更多
打賞
已打賞¥{{reward_price}}
1300
{{like_count}}
{{collect_count}}
添加回復 ({{post_count}})

相關推薦

快速安全登錄

使用微信掃碼登錄
{{item.label}} 加精
{{item.label}} {{item.label}} 板塊推薦 常見問題 產品動態(tài) 精選推薦 首頁頭條 首頁動態(tài) 首頁推薦
取 消 確 定
回復
回復
問題:
問題自動獲取的帖子內容,不準確時需要手動修改. [獲取答案]
答案:
提交
bug 需求 取 消 確 定
打賞金額
當前余額:¥{{rewardUserInfo.reward_price}}
{{item.price}}元
請輸入 0.1-{{reward_max_price}} 范圍內的數(shù)值
打賞成功
¥{{price}}
完成 確認打賞

微信登錄/注冊

切換手機號登錄

{{ bind_phone ? '綁定手機' : '手機登錄'}}

{{codeText}}
切換微信登錄/注冊
暫不綁定
CRMEB客服

CRMEB咨詢熱線 咨詢熱線

400-8888-794

微信掃碼咨詢

CRMEB開源商城下載 源碼下載 CRMEB幫助文檔 幫助文檔
返回頂部 返回頂部
CRMEB客服