国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

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

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

服務(wù)器之家 - 編程語言 - Java教程 - springboot的緩存技術(shù)的實(shí)現(xiàn)

springboot的緩存技術(shù)的實(shí)現(xiàn)

2021-05-05 10:50雙斜杠少年 Java教程

這篇文章主要介紹了springboot的緩存技術(shù)的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

引子

我門知道一個(gè)程序的瓶頸在于數(shù)據(jù)庫,我門也知道內(nèi)存的速度是大大快于硬盤的速度的。當(dāng)我門需要重復(fù)的獲取相同的數(shù)據(jù)的時(shí)候,我門一次又一次的請(qǐng)求數(shù)據(jù)庫或者遠(yuǎn)程服務(wù),導(dǎo)致大量的時(shí)間耗費(fèi)在數(shù)據(jù)庫查詢或者遠(yuǎn)程方法的調(diào)用上,導(dǎo)致程序性能的惡化,這更是數(shù)據(jù)緩存要解決的問題。

spring 緩存支持

spring定義了 org.springframework.cache.cachemanager和org.springframework.cache.cache接口來統(tǒng)一不同的緩存技術(shù)。其中,cachemanager是spring提供的各種緩存技術(shù)抽象接口,cache接口包含了緩存的各種操作(增加、刪除獲得緩存,我門一般不會(huì)直接和此接口打交道)

spring 支持的cachemanager

針對(duì)不同的緩存技術(shù),需要實(shí)現(xiàn)不同的cachemanager ,spring 定義了如下表的cachemanager實(shí)現(xiàn)。

springboot的緩存技術(shù)的實(shí)現(xiàn)

實(shí)現(xiàn)任意一種cachemanager 的時(shí)候,需要注冊(cè)實(shí)現(xiàn)cachemanager的bean,當(dāng)然每種緩存技術(shù)都有很多額外的配置,但配置cachemanager 是必不可少的。

聲明式緩存注解

spring提供了4個(gè)注解來聲明緩存規(guī)則(又是使用注解式的aop的一個(gè)生動(dòng)例子),如表。

springboot的緩存技術(shù)的實(shí)現(xiàn)

開啟聲明式緩存

開啟聲明式緩存支持非常簡(jiǎn)單,只需要在配置類上使用@enabelcaching 注解即可。

springboot 的支持

在spring中國年使用緩存技術(shù)的關(guān)鍵是配置cachemanager 而springbok 為我門自動(dòng)配置了多個(gè)cachemanager的實(shí)現(xiàn)。在spring boot 環(huán)境下,使用緩存技術(shù)只需要在項(xiàng)目中導(dǎo)入相關(guān)緩存技術(shù)的依賴包,并配置類使用@enabelcaching開啟緩存支持即可。

小例子

小例子是使用 springboot+jpa +cache 實(shí)現(xiàn)的。

實(shí)例步驟目錄

  1. 創(chuàng)建maven項(xiàng)目
  2. 數(shù)據(jù)庫配置
  3. jpa配置和cache配置
  4. 編寫bean 和dao層
  5. 編寫service層
  6. 編寫controller
  7. 啟動(dòng)cache
  8. 測(cè)試校驗(yàn)

1.創(chuàng)建maven項(xiàng)目

新建maven 項(xiàng)目pom.xml文件如下內(nèi)容如下:

?
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/pom/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
   xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelversion>4.0.0</modelversion>
 
 <groupid>com.us</groupid>
 <artifactid>springboot-cache</artifactid>
 <version>1.0-snapshot</version>
 <parent>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-parent</artifactid>
  <version>1.3.0.release</version>
 </parent>
 
 <properties>
  <start-class>com.us.application</start-class>
 
  <maven.compiler.target>1.8</maven.compiler.target>
  <maven.compiler.source>1.8</maven.compiler.source>
 </properties>
 
 <!-- add typical dependencies for a web application -->
 <dependencies>
  <dependency>
   <groupid>org.springframework.boot</groupid>
   <artifactid>spring-boot-starter-cache</artifactid>
  </dependency>
  <dependency>
   <groupid>org.springframework.boot</groupid>
   <artifactid>spring-boot-starter-data-jpa</artifactid>
  </dependency>
  <dependency>
   <groupid>org.springframework.boot</groupid>
   <artifactid>spring-boot-starter-web</artifactid>
  </dependency>
 
  <dependency>
   <groupid>net.sf.ehcache</groupid>
   <artifactid>ehcache</artifactid>
  </dependency>
 
  <!--db-->
 
  <dependency>
   <groupid>mysql</groupid>
   <artifactid>mysql-connector-java</artifactid>
   <version>6.0.5</version>
  </dependency>
  <dependency>
   <groupid>com.mchange</groupid>
   <artifactid>c3p0</artifactid>
   <version>0.9.5.2</version>
   <exclusions>
    <exclusion>
     <groupid>commons-logging</groupid>
     <artifactid>commons-logging</artifactid>
    </exclusion>
   </exclusions>
  </dependency>
 
 </dependencies>
 
</project>

2.數(shù)據(jù)庫配置

在src/main/esouces目錄下新建application.properties 文件,內(nèi)容為數(shù)據(jù)庫連接信息,如下:

application.properties

?
1
2
3
4
5
ms.db.driverclassname=com.mysql.jdbc.driver
ms.db.url=jdbc:mysql://localhost:3306/cache?prepstmtcachesize=517&cacheprepstmts=true&autoreconnect=true&useunicode=true&characterencoding=utf-8&usessl=false&allowmultiqueries=true
ms.db.username=root
ms.db.password=xxxxxx
ms.db.maxactive=500

新建dbconfig.java 配置文件,配置數(shù)據(jù)源

?
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
package com.us.example.config;
/**
 * created by yangyibo on 17/1/13.
 */
import java.beans.propertyvetoexception;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.core.env.environment;
import com.mchange.v2.c3p0.combopooleddatasource;
@configuration
public class dbconfig {
 @autowired
 private environment env;
 
 @bean(name="datasource")
 public combopooleddatasource datasource() throws propertyvetoexception {
  combopooleddatasource datasource = new combopooleddatasource();
  datasource.setdriverclass(env.getproperty("ms.db.driverclassname"));
  datasource.setjdbcurl(env.getproperty("ms.db.url"));
  datasource.setuser(env.getproperty("ms.db.username"));
  datasource.setpassword(env.getproperty("ms.db.password"));
  datasource.setmaxpoolsize(20);
  datasource.setminpoolsize(5);
  datasource.setinitialpoolsize(10);
  datasource.setmaxidletime(300);
  datasource.setacquireincrement(5);
  datasource.setidleconnectiontestperiod(60);
 
  return datasource;
 }
}

數(shù)據(jù)庫設(shè)計(jì),數(shù)據(jù)庫只有一張person表,設(shè)計(jì)如下:

springboot的緩存技術(shù)的實(shí)現(xiàn)

3.jpa配置

spring-data- jpa 配置文件如下:

?
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
45
46
47
48
49
50
package com.us.example.config;
import java.util.hashmap;
import java.util.map;
import javax.persistence.entitymanagerfactory;
import javax.sql.datasource;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.componentscan;
import org.springframework.context.annotation.configuration;
import org.springframework.data.jpa.repository.config.enablejparepositories;
import org.springframework.orm.jpa.jpatransactionmanager;
import org.springframework.orm.jpa.localcontainerentitymanagerfactorybean;
import org.springframework.orm.jpa.vendor.hibernatejpavendoradapter;
import org.springframework.transaction.platformtransactionmanager;
import org.springframework.transaction.annotation.enabletransactionmanagement;
/**
 * created by yangyibo on 17/1/13.
 */
@configuration
@enablejparepositories("com.us.example.dao")
@enabletransactionmanagement
@componentscan
public class jpaconfig {
 @autowired
 private datasource datasource;
 
 @bean
 public entitymanagerfactory entitymanagerfactory() {
  hibernatejpavendoradapter vendoradapter = new hibernatejpavendoradapter();
 
  localcontainerentitymanagerfactorybean factory = new localcontainerentitymanagerfactorybean();
  factory.setjpavendoradapter(vendoradapter);
  factory.setpackagestoscan("com.us.example.bean");
  factory.setdatasource(datasource);
 
  map<string, object> jpaproperties = new hashmap<>();
  jpaproperties.put("hibernate.ejb.naming_strategy","org.hibernate.cfg.improvednamingstrategy");
  jpaproperties.put("hibernate.jdbc.batch_size",50);
  factory.setjpapropertymap(jpaproperties);
  factory.afterpropertiesset();
  return factory.getobject();
 }
 
 @bean
 public platformtransactionmanager transactionmanager() {
  jpatransactionmanager txmanager = new jpatransactionmanager();
  txmanager.setentitymanagerfactory(entitymanagerfactory());
  return txmanager;
 }
}

4.編寫bean 和dao層

實(shí)體類 person.java

?
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
45
46
47
48
49
50
51
52
53
54
55
56
package com.us.example.bean;
import javax.persistence.entity;
import javax.persistence.generatedvalue;
import javax.persistence.id;
import javax.persistence.table;
/**
 * created by yangyibo on 17/1/13.
 */
@entity
@table(name = "person")
public class person {
 @id
 @generatedvalue
 private long id;
 
 private string name;
 
 private integer age;
 
 private string address;
 
 public person() {
  super();
 }
 public person(long id, string name, integer age, string address) {
  super();
  this.id = id;
  this.name = name;
  this.age = age;
  this.address = address;
 }
 public long getid() {
  return id;
 }
 public void setid(long id) {
  this.id = id;
 }
 public string getname() {
  return name;
 }
 public void setname(string name) {
  this.name = name;
 }
 public integer getage() {
  return age;
 }
 public void setage(integer age) {
  this.age = age;
 }
 public string getaddress() {
  return address;
 }
 public void setaddress(string address) {
  this.address = address;
 }
}

dao層,personrepository.java

?
1
2
3
4
5
6
7
8
9
10
package com.us.example.dao;
import com.us.example.bean.person;
import org.springframework.data.jpa.repository.jparepository;
 
/**
 * created by yangyibo on 17/1/13.
 */
public interface personrepository extends jparepository<person, long> {
 
}

5.編寫service層

service 接口

?
1
2
3
4
5
6
7
8
9
10
11
12
package com.us.example.service;
import com.us.example.bean.person;
 
/**
 * created by yangyibo on 17/1/13.
 */
public interface demoservice {
 public person save(person person);
 public void remove(long id);
 public person findone(person person);
 
}

實(shí)現(xiàn):(重點(diǎn),此處加緩存)

?
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
package com.us.example.service.impl;
import com.us.example.bean.person;
import com.us.example.dao.personrepository;
import com.us.example.service.demoservice;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.cache.annotation.cacheevict;
import org.springframework.cache.annotation.cacheput;
import org.springframework.cache.annotation.cacheable;
import org.springframework.stereotype.service;
/**
 * created by yangyibo on 17/1/13.
 */
@service
public class demoserviceimpl implements demoservice {
 @autowired
 private personrepository personrepository;
 @override
 //@cacheput緩存新增的或更新的數(shù)據(jù)到緩存,其中緩存名字是 people 。數(shù)據(jù)的key是person的id
 @cacheput(value = "people", key = "#person.id")
 public person save(person person) {
  person p = personrepository.save(person);
  system.out.println("為id、key為:"+p.getid()+"數(shù)據(jù)做了緩存");
  return p;
 }
 
 @override
 //@cacheevict 從緩存people中刪除key為id 的數(shù)據(jù)
 @cacheevict(value = "people")
 public void remove(long id) {
  system.out.println("刪除了id、key為"+id+"的數(shù)據(jù)緩存");
  //這里不做實(shí)際刪除操作
 }
 
 @override
 //@cacheable緩存key為person 的id 數(shù)據(jù)到緩存people 中,如果沒有指定key則方法參數(shù)作為key保存到緩存中。
 @cacheable(value = "people", key = "#person.id")
 public person findone(person person) {
  person p = personrepository.findone(person.getid());
  system.out.println("為id、key為:"+p.getid()+"數(shù)據(jù)做了緩存");
  return p;
 }
}

6.編寫controller

為了測(cè)試方便請(qǐng)求方式都用了get

?
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
package com.us.example.controller;
import com.us.example.bean.person;
import com.us.example.service.demoservice;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.responsebody;
import org.springframework.web.bind.annotation.restcontroller;
/**
 * created by yangyibo on 17/1/13.
 */
@restcontroller
public class cachecontroller {
 
 @autowired
 private demoservice demoservice;
 //http://localhost:8080/put?name=abel&age=23&address=shanghai
 @requestmapping("/put")
 public person put(person person){
  return demoservice.save(person);
 
 }
 
 //http://localhost:8080/able?id=1
 @requestmapping("/able")
 @responsebody
 public person cacheable(person person){
  return demoservice.findone(person);
 
 }
 
 //http://localhost:8080/evit?id=1
 @requestmapping("/evit")
 public string evit(long id){
  demoservice.remove(id);
  return "ok";
 
 }
}

7.啟動(dòng)cache

啟動(dòng)類中要記得開啟緩存配置。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.us.example;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.cache.annotation.enablecaching;
import org.springframework.context.configurableapplicationcontext;
import org.springframework.context.annotation.componentscan;
import static org.springframework.boot.springapplication.*;
/**
 * created by yangyibo on 17/1/13.
 */
 
@componentscan(basepackages ="com.us.example")
@springbootapplication
@enablecaching
public class application {
 public static void main(string[] args) {
  configurableapplicationcontext run = run(application.class, args);
 }
}

8.測(cè)試校驗(yàn)檢驗(yàn)able:

啟動(dòng)application 類,啟動(dòng)后在瀏覽器輸入:http://localhost:8080/able?id=1(首先要在數(shù)據(jù)庫中初始化幾條數(shù)據(jù)。)

springboot的緩存技術(shù)的實(shí)現(xiàn)

控制臺(tái)輸出:

“為id、key為:1數(shù)據(jù)做了緩存“ 此時(shí)已經(jīng)為此次查詢做了緩存,再次查詢?cè)摋l數(shù)據(jù)將不會(huì)出現(xiàn)此條語句,也就是不查詢數(shù)據(jù)庫了。

檢驗(yàn)put

在瀏覽器輸入:http://localhost:8080/put?name=abel&age=23&address=shanghai(向數(shù)據(jù)庫插入一條數(shù)據(jù),并將數(shù)據(jù)放入緩存。)

springboot的緩存技術(shù)的實(shí)現(xiàn)

此時(shí)控制臺(tái)輸出為該條記錄做了緩存:

springboot的緩存技術(shù)的實(shí)現(xiàn)

然后再次調(diào)用able 方法,查詢?cè)摋l數(shù)據(jù),將不再查詢數(shù)據(jù)庫,直接從緩存中讀取數(shù)據(jù)。

測(cè)試evit

在瀏覽器輸入:http://localhost:8080/evit?id=1(將該條記錄從緩存中清楚,清除后,在次訪問該條記錄,將會(huì)重新將該記錄放入緩存。)

控制臺(tái)輸出:

springboot的緩存技術(shù)的實(shí)現(xiàn)

切換緩存

1.切換為ehcache作為緩存

pom.xml 文件中添加依賴

?
1
2
3
4
<dependency>
   <groupid>net.sf.ehcache</groupid>
   <artifactid>ehcache</artifactid>
  </dependency>

在resource 文件夾下新建ehcache的配置文件ehcache.xml 內(nèi)容如下,此文件spring boot 會(huì)自動(dòng)掃描

?
1
2
3
4
5
<?xml version="1.0" encoding="utf-8"?>
<ehcache>
 <!--切換為ehcache 緩存時(shí)使用-->
<cache name="people" maxelementsinmemory="1000" />
</ehcache>

2.切換為guava作為緩存

只需要在pom中添加依賴

?
1
2
3
4
5
<dependency>
  <groupid>com.google.guava</groupid>
  <artifactid>guava</artifactid>
  <version>18.0</version>
 </dependency>

3.切換為redis作為緩存

請(qǐng)看下篇博客

本文參考:《javaee開發(fā)的顛覆者:spring boot實(shí)戰(zhàn) 》

本文源代碼:https://github.com/527515025/springboot.git

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

原文鏈接:https://blog.csdn.net/u012373815/article/details/54564076/

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美性大战久久久 | 国产在线一二三区 | 欧美国产激情二区三区 | 成人在线免费观看 | 亚洲高清在线视频 | 亚洲精品乱码久久久久膏 | 一区二区精品视频 | 日本久久精品 | 免费毛片a线观看 | 奇米影视7777 | 成人免费观看高清视频 | 欧美激情精品久久久久久 | 久久伊人久久 | 一区二区三区日韩 | 亚洲欧美高清 | 99热婷婷| 国产精品区一区二区三含羞草 | 国产高清视频一区 | 久久99精品国产99久久6尤 | 中文字幕一区二区三区日韩精品 | 欧美一级全黄 | 亚洲国产精品成人 | 日韩超碰| 视频一区中文字幕 | 亚洲国产精品激情在线观看 | 毛片一级在线观看 | 美女视频一区二区三区 | 日韩不卡一区二区三区 | 韩日毛片 | 国产偷久久9977 | 日韩小视频网站 | 欧美老妇交乱视频 | www.一区 | 久久天堂电影 | 国产一区二区三区四区二区 | 日韩91| 日本在线观看一区二区 | 韩国精品一区 | 色中色av | 亚洲福利在线观看 | 亚洲一区二区三区在线 |