一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - 使用Spring Data Redis實現(xiàn)數(shù)據(jù)緩存的方法

使用Spring Data Redis實現(xiàn)數(shù)據(jù)緩存的方法

2021-06-09 14:10JavaQ Java教程

目前在系統(tǒng)架構(gòu)設(shè)計中使用Redis實現(xiàn)緩存,這篇文章主要介紹了使用Spring Data Redis實現(xiàn)數(shù)據(jù)緩存的方法,具有一定的參考價值,需要的朋友可以參考下

引言

目前很多系統(tǒng)為了解決數(shù)據(jù)讀寫的性能瓶頸,在系統(tǒng)架構(gòu)設(shè)計中使用redis實現(xiàn)緩存,spring框架為了讓開發(fā)人員更加方便快捷的使用redis實現(xiàn)緩存,對redis的操作進行了包裝。

0.緩存

個人理解的緩存是指用于存儲頻繁使用的數(shù)據(jù)的空間,關(guān)注點是存儲數(shù)據(jù)的空間和使用頻繁的數(shù)據(jù)。緩存技術(shù),簡單的說就是先從緩存中查詢數(shù)據(jù)是否存在,存在則直接返回,不存在再執(zhí)行相應(yīng)的操作獲取數(shù)據(jù),并將獲取的數(shù)據(jù)存儲到緩存中,它是一種提升系統(tǒng)性能的重要方法。

1.redis

redis是一個開源的、內(nèi)存存儲key-value類型的數(shù)據(jù)結(jié)構(gòu)服務(wù)器,可用作數(shù)據(jù)庫、高速緩存和消息隊列代理。它支持的數(shù)據(jù)類型有字符串、哈希表、列表、集合、有序集合等,同時通過redis sentinel提供高可用,通過redis cluster提供分區(qū)功能。

2.jedis

jedis是redis的java版客戶端實現(xiàn),也是官方推薦的java版客戶端。它封裝了對redis的各種操作,并且支持事務(wù)、管道及有jedis自身實現(xiàn)的分布式。

3.spring data redis

spring data是spring框架中的一個主要項目,目的是為了簡化構(gòu)建基于spring框架應(yīng)用的數(shù)據(jù)訪問,包括非關(guān)系數(shù)據(jù)庫、map-reduce框架、云數(shù)據(jù)服務(wù)等,另外也包含對關(guān)系數(shù)據(jù)庫的訪問支持。
spring data redis是spring data項目中的一個主要模塊,實現(xiàn)了對jedis客戶端api的高度封裝,使對redis的操作更加便捷。

4.關(guān)系圖

redis、jedis、spring data redis三者之間的關(guān)系圖如下所示。

使用Spring Data Redis實現(xiàn)數(shù)據(jù)緩存的方法

5.spring cache

從spring3.1開始,spring框架提供了對cache的支持,提供了一個對緩存使用的抽象,通過在既有代碼中添加少量它定義的各種 annotation,即能夠達到緩存方法的返回對象的作用。提供的主要注解有@cacheable、@cacheput、@cacheevict和@caching,具體見表1。

使用Spring Data Redis實現(xiàn)數(shù)據(jù)緩存的方法

@cacheable的常用屬性及說明如表2所示。

使用Spring Data Redis實現(xiàn)數(shù)據(jù)緩存的方法

使用Spring Data Redis實現(xiàn)數(shù)據(jù)緩存的方法

@cacheevict的常用屬性見表4。@cacheput的常用屬性同@cacheable。

使用Spring Data Redis實現(xiàn)數(shù)據(jù)緩存的方法

當需要在類上或方法上同時使用多個注解時,可以使用@caching,如@caching(cacheable = @cacheable("user"), evict = {@cacheevict("member"), @cacheevict(value = "customer", allentries = true)})

6.使用示例

下面使用spring data reds、redis和jedis實現(xiàn)一個簡單的數(shù)據(jù)緩存。

1)依賴配置

示例使用了gradle,所以需要在build.gradle中加入如下依賴配置來管理所需要的jar。

?
1
2
3
compile "org.springframework.data:spring-data-redis:1.7.2.release"
compile "redis.clients:jedis:2.7.2"
testcompile "junit:junit:4.12"

2)redis配置

示例連接的是本地的redis,redis.properties配置如下。

?
1
2
3
4
5
6
7
8
9
10
# redis settings
redis.host=127.0.0.1
redis.port=6379
redis.pass=
redis.dbindex=0
redis.expiration=3000
redis.maxidle=300
redis.maxactive=600
redis.maxwait=1000
redis.testonborrow=true

3)spring配置

spring的配置文件如下。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<beans xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemalocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
  <context:component-scan base-package="redis.cache"/>
  <context:annotation-config/>
  <cache:annotation-driven cache-manager="rediscachemanager"/>
  <bean class="org.springframework.beans.factory.config.propertyplaceholderconfigurer">
    <property name="locations">
      <list>
        <value>classpath:redis.properties</value>
      </list>
    </property>
  </bean>
  <!-- 配置jedispoolconfig實例 -->
  <bean id="poolconfig" class="redis.clients.jedis.jedispoolconfig">
    <property name="maxidle" value="${redis.maxidle}"/>
    <property name="maxtotal" value="${redis.maxactive}"/>
    <property name="maxwaitmillis" value="${redis.maxwait}"/>
    <property name="testonborrow" value="${redis.testonborrow}"/>
  </bean>
  <!-- 配置jedisconnectionfactory -->
  <bean id="jedisconnectionfactory" class="org.springframework.data.redis.connection.jedis.jedisconnectionfactory">
    <property name="hostname" value="${redis.host}"/>
    <property name="port" value="${redis.port}"/>
    <property name="password" value="${redis.pass}"/>
    <property name="database" value="${redis.dbindex}"/>
    <property name="poolconfig" ref="poolconfig"/>
  </bean>
  <!-- 配置redistemplate -->
  <bean id="redistemplate" class="org.springframework.data.redis.core.redistemplate">
    <property name="connectionfactory" ref="jedisconnectionfactory"/>
  </bean>
  <!-- 配置rediscachemanager -->
  <bean id="rediscachemanager" class="org.springframework.data.redis.cache.rediscachemanager">
    <constructor-arg name="redisoperations" ref="redistemplate"/>
    <property name="defaultexpiration" value="${redis.expiration}"/>
  </bean>
</beans>

4)service

示例代碼的servicer如下。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@service("userservice")
public class userservice {
  @cacheable(value = "user", key = "'userid_' + #id",condition = "#id<=110")
  public string queryfullnamebyid(long id) {
    system.out.println("execute queryfullnamebyid method");
    return "zhangsanfeng";
  }
 
  @cacheevict(value = "user", key = "'userid_' + #id")
  public void deletebyid(long id) {
    system.out.println("execute deletebyid method");
  }
 
  @cacheput(value = "user", key = "'userid_' + #id")
  public string modifyfullnamebyid(long id, string newname) {
    system.out.println("execute modifyfullnamebyid method");
    return newname;
  }
}

5)測試

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@test
public void test() {
  applicationcontext context = new classpathxmlapplicationcontext("rediscachecontext.xml");
  userservice userservice = (userservice) context.getbean("userservice");
  system.out.println("第一次執(zhí)行查詢:" + userservice.queryfullnamebyid(110l));
  system.out.println("----------------------------------");
 
  system.out.println("第二次執(zhí)行查詢:" + userservice.queryfullnamebyid(110l));
  system.out.println("----------------------------------");
 
  userservice.deletebyid(110l);
  system.out.println("----------------------------------");
 
  system.out.println("清除緩存后查詢:" + userservice.queryfullnamebyid(110l));
  system.out.println("----------------------------------");
 
  system.out.println(userservice.modifyfullnamebyid(110l, "zhangjunbao"));
  system.out.println("----------------------------------");
 
  system.out.println("修改數(shù)據(jù)后查詢:" + userservice.queryfullnamebyid(110l));
  system.out.println("----------------------------------");
 
  system.out.println("第一次執(zhí)行查詢:" + userservice.queryfullnamebyid(112l));
  system.out.println("----------------------------------");
 
  system.out.println("第二次執(zhí)行查詢:" + userservice.queryfullnamebyid(112l));
  system.out.println("----------------------------------");
}

6)測試結(jié)果

輸出結(jié)果如下。

execute queryfullnamebyid method
第一次執(zhí)行查詢:zhangsanfeng
----------------------------------
第二次執(zhí)行查詢:zhangsanfeng
----------------------------------
execute deletebyid method
----------------------------------
execute queryfullnamebyid method
清除緩存后查詢:zhangsanfeng
----------------------------------
execute modifyfullnamebyid method
zhangjunbao
----------------------------------
修改數(shù)據(jù)后查詢:zhangjunbao
----------------------------------
execute queryfullnamebyid method
第一次執(zhí)行查詢:zhangsanfeng
----------------------------------
execute queryfullnamebyid method
第二次執(zhí)行查詢:zhangsanfeng
----------------------------------

從結(jié)果可以看到,使用緩存后,第二次查詢沒有執(zhí)行查詢方法體,直接返回了緩存中的數(shù)據(jù);清除緩存后,再次查詢就執(zhí)行了查詢方法體;修改數(shù)據(jù)后,相應(yīng)的緩存數(shù)據(jù)也被修改了;不符合緩存條件的數(shù)據(jù)沒有被緩存。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://www.jianshu.com/p/0dbe0a616898

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 海绵宝宝第二季全集免费观看 | 日本强不卡在线观看 | 亚洲精品第一国产综合 | 亚洲高清在线视频 | 精品在线播放视频 | 忘忧草高清 | 亚洲色导航 | 美女张开双腿让男人捅 | 九九久久国产精品大片 | 日韩精品欧美高清区 | 国产在线一区二区视频 | 国产成人精品999在线 | 日本免费一区二区三区a区 日本免费三片在线观看 | 亲爱的客栈第二季免费观看完整版 | 亚洲spank男男实践网站 | 日本老头4569gay | 国产精品乱码高清在线观看 | 精品视频一区二区 | 免费亚洲一区 | 亚洲AV久久久久久久无码 | 免费视频精品一区二区 | 女性全身裸露无遮挡 | 2018天天拍拍拍免费视频 | melody中文字幕 | 青青久在线视频免费观看 | 国产肥老上视频 | 91色资源网在线观看 | 91噜噜噜噜色 | 日本videossexx日本人 | 狠狠香蕉| 亚洲免费闲人蜜桃 | 三极片在线观看 | 亚洲AV无码国产精品色午夜情 | 久久精品中文字幕 | 国产小视频网站 | 欧洲久久 | 亚洲欧美日韩成人一区在线 | 九九艹 | 亚洲AV久久无码精品九号软件 | 99视频导航| 人人爽人人香蕉 |