การแนะนำ
ความรู้ของเราคือคอขวดของโปรแกรมอยู่ในฐานข้อมูลและความรู้ของเราก็รู้ว่าความเร็วของหน่วยความจำนั้นเร็วกว่าฮาร์ดดิสก์มาก เมื่อแผนกของเราจำเป็นต้องได้รับข้อมูลเดียวกันซ้ำ ๆ แผนกของเราขอฐานข้อมูลหรือบริการระยะไกลอีกครั้งและอีกครั้งส่งผลให้ใช้เวลามากในการสืบค้นฐานข้อมูลหรือการโทรระยะไกลทำให้เกิดการเสื่อมสภาพของประสิทธิภาพของโปรแกรมซึ่งเป็นปัญหาที่แคชข้อมูลจำเป็นต้องแก้ไข
สนับสนุนการแคชฤดูใบไม้ผลิ
ฤดูใบไม้ผลิกำหนด org.springframework.cache.cachemanager และ org.springframework.cache.cache อินเตอร์เฟสเพื่อรวมเทคโนโลยีการแคชที่แตกต่างกัน ในหมู่พวกเขา CacheManager เป็นอินเทอร์เฟซนามธรรมสำหรับเทคโนโลยีแคชต่างๆที่จัดทำโดยฤดูใบไม้ผลิ อินเทอร์เฟซแคชรวมถึงการดำเนินการแคชต่าง ๆ (การเพิ่มและลบเพื่อรับแคชระบบของเราโดยทั่วไปไม่ได้จัดการโดยตรงกับอินเทอร์เฟซนี้)
CacheManager ได้รับการสนับสนุนในฤดูใบไม้ผลิ
สำหรับเทคโนโลยีการแคชที่แตกต่างกันจำเป็นต้องใช้ cachemanagers ที่แตกต่างกัน ฤดูใบไม้ผลิกำหนดการใช้งาน CacheManager ในตารางต่อไปนี้
เมื่อใช้ cachemanager ใด ๆ คุณจะต้องลงทะเบียนถั่วที่ใช้ cachemanager แน่นอนว่าเทคโนโลยีการแคชแต่ละอย่างมีการกำหนดค่าเพิ่มเติมมากมาย แต่การกำหนดค่า CacheManager เป็นสิ่งจำเป็น
คำอธิบายประกอบแคชที่ประกาศ
ฤดูใบไม้ผลิมีคำอธิบายประกอบ 4 รายการเพื่อประกาศกฎแคช (ตัวอย่างที่ชัดเจนของ AOP โดยใช้สูตรคำอธิบายประกอบ) ดังแสดงในตาราง
เปิดแคชประกาศ
การเปิดการสนับสนุนการแคชที่ประกาศนั้นง่ายมากคุณจะต้องใช้คำอธิบายประกอบ @EnabelCaching ในคลาสการกำหนดค่า
สนับสนุน Springboot
กุญแจสำคัญในการใช้เทคโนโลยีแคชในฤดูใบไม้ผลิจีนคือการกำหนดค่า CacheManager Springbok กำหนดค่าการใช้งาน CacheManager หลายรายการโดยอัตโนมัติสำหรับประตูของเรา ในสภาพแวดล้อมการบูตฤดูใบไม้ผลิการใช้เทคโนโลยีแคชต้องใช้เฉพาะการนำเข้าแพ็คเกจการพึ่งพาของเทคโนโลยีแคชที่เกี่ยวข้องในโครงการและการกำหนดค่าคลาสเพื่อเปิดใช้งานการสนับสนุนแคชโดยใช้ @enabelcaching
ตัวอย่างเล็ก ๆ
มีการใช้ตัวอย่างเล็ก ๆ โดยใช้ Springboot+JPA+Cache
ไดเรกทอรีตัวอย่างขั้นตอน
1. สร้างโครงการ Maven
สร้างไฟล์ Maven Project ใหม่ pom.xml ดังนี้:
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns: xsi = "http://www.w3.org/2001/ XSI: schemalocation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" <Sersion> 1.0-SNAPSHOT </เวอร์ชัน> <carent> <roupId> org.springframework.boot </groupId> <ratifactid> Spring-Boot-Starter-Parent </artifactid> <version> 1.3.0.Release </เวอร์ชัน> </parent> <properties> <maven.compiler.target> 1.8 </maven.compiler.target> <maven.compiler.source> 1.8 </maven.compiler.source> </properties> <!-เพิ่มการพึ่งพาทั่วไปสำหรับเว็บแอปพลิเคชัน-> <การพึ่งพา <ArtIfactId> Spring-Boot-Starter-Cache </artifactid> </serdency> <การพึ่งพาอาศัย> <roupId> org.springframework.boot </groupId> <ratifactid> Spring-Boot-Starter-Data-JPA </artifactid> <ArtIfactId> Spring-Boot-Starter-Web </artifactId> </การพึ่งพาอาศัย> <การพึ่งพา> <roupId> net.sf.ehcache </groupid> <ratifactid> ehcache </artifactid> </การพึ่งพา> <! <ArtIfactId> mysql-connector-java </artifactid> <persion> 6.0.5 </เวอร์ชัน> </การพึ่งพา> <การพึ่งพา> <roupid> com.mchange </groupId> <ArtifactId> c3p0 </artifactid> <ARTIFACTID> Commons-Logging </artifactId> </excusion> </การยกเว้น> </การพึ่งพาอาศัย> </pendencies> </project>
2. การกำหนดค่าฐานข้อมูล
สร้างไฟล์ Application.Properties ใหม่ในไดเรกทอรี SRC/Main/Esouches และเนื้อหาคือข้อมูลการเชื่อมต่อฐานข้อมูลดังต่อไปนี้:
Application.properties
ms.db.driverclassName = com.mysql.jdbc.drivererms.db.url = jdbc: mysql: // localhost: 3306/cache? t = true & useUnicode = true & characterencoding = utf-8 & usessl = false & lewalmultiqueries = truems.db.username = rootms.db.password = xxxxxxms.db.maxactive = 500
สร้างไฟล์กำหนดค่า dbconfig.java ใหม่และกำหนดค่าแหล่งข้อมูล
แพ็คเกจ com.us.example.config;/*** สร้างโดย Yangyibo เมื่อวันที่ 17/1/13 */นำเข้า java.beans.propertyvetoexception; นำเข้า org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.context.annotation.bean; นำเข้า com.mchange.v2.c3p0.combopooledDataSource; @configurationPublic คลาส dbconfig {@autowired สภาพแวดล้อมส่วนตัว env; @Bean (name = "DataSource") Public CombopooledDataSource DataSource () พ่น PropertyVetoException {combopooledDataSource dataSource = ใหม่ 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); ส่งคืนแหล่งข้อมูล; -การออกแบบฐานข้อมูลฐานข้อมูลมีตารางบุคคลเพียงคนเดียวและการออกแบบมีดังนี้:
3.JPA การกำหนดค่า
ไฟล์กำหนดค่า Spring-Data-JPA มีดังนี้:
แพ็คเกจ com.us.example.config; นำเข้า java.util.hashmap; นำเข้า java.util.map; นำเข้า Javax.persistence.entityManagerfactory; นำเข้า Javax.sql.datasource; นำเข้า org.springframework.beans. org.springframework.context.annotation.bean; นำเข้า org.springframework.context.annotation.configuration; นำเข้า org.springframework.data.jpa.repository.config.enablejparepositories; org.springframework.orm.jpa.localcontainerentityManagerFactoryBean นำเข้า org.springframework.orm.jpa.vendor.hibernatejpavendoradapter; นำเข้า org.springframework.transaction.platformtransactionmanger; org.springframework.transaction.annotation.enabletransactionManagement;/*** สร้างโดย Yangyibo เมื่อวันที่ 17/1/13 */@configuration@enablejparepositories ("com.us.example.dao")@enabletransactionManagement@componentscanpublic คลาส jpaconfig {@autowired DataSource DataSource; @Bean Public EntityManagerFactory EntityManagerFactory () {hibernatejpavendoradapter Vendoradapter = ใหม่ HibernateJPavendorAdapter (); LocalContainerentityManagerFactoryBean โรงงาน = ใหม่ LocalContainerentityManagerFactoryBean (); Factory.SetJPavendorAdapter (VendorAdapter); Factory.SetPackagestoscan ("com.us.example.bean"); Factory.SetDataSource (DataSource); แผนที่ <สตริงวัตถุ> 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 ()); กลับ txManager; -4. เขียนถั่วและชั้น Dao
บุคคลชั้นเรียน Java
แพ็คเกจ com.us.example.bean; นำเข้า Javax.persistence.entity; นำเข้า Javax.persistence.generatedValue; นำเข้า Javax.persistence.id; นำเข้า Javax.persistence.table;/*** สร้างโดย Yangyibo เมื่อวันที่ 17/1/13 */ @entity @table (name = "person") บุคคลในชั้นเรียนสาธารณะ {@id @IneratedValue ส่วนตัว Long ID; ชื่อสตริงส่วนตัว; อายุจำนวนเต็มส่วนตัว ที่อยู่สตริงส่วนตัว บุคคลสาธารณะ () {super (); } บุคคลสาธารณะ (ID ยาว, ชื่อสตริง, อายุจำนวนเต็ม, ที่อยู่สตริง) {super (); this.id = id; this.name = ชื่อ; this.age = อายุ; this.address = ที่อยู่; } สาธารณะ Long getId () {return id; } โมฆะสาธารณะ setId (Long id) {this.id = id; } สตริงสาธารณะ getName () {ชื่อคืน; } โมฆะสาธารณะ setName (ชื่อสตริง) {this.name = name; } Public Integer Getage () {return Age; } การตั้งค่าโมฆะสาธารณะ (อายุจำนวนเต็ม) {this.age = อายุ; } สตริงสาธารณะ getAddress () {return address; } โมฆะสาธารณะ setAddress (ที่อยู่สตริง) {this.address = ที่อยู่; -Dao Layer, personrepository.java
แพ็คเกจ com.us.example.dao; นำเข้า com.us.example.bean.person; นำเข้า org.springframework.data.jpa.repository.jparepository;/*** สร้างโดย Yangyibo เมื่อวันที่ 17/1/13 */Public Interface Personrepository ขยาย jparepository <person, long> {}5. เขียนเลเยอร์บริการ
อินเทอร์เฟซบริการ
แพ็คเกจ com.us.example.service; นำเข้า com.us.example.bean.person;/*** สร้างโดย Yangyibo เมื่อวันที่ 17/1/13 */Demoservice ส่วนต่อประสานสาธารณะ {บุคคลสาธารณะบันทึก (บุคคลบุคคล); โมฆะสาธารณะลบ (ID ยาว); บุคคลสาธารณะ Findone (บุคคลบุคคล);}การใช้งาน: (คีย์เพิ่มแคชที่นี่)
แพ็คเกจ com.us.example.service.impl; นำเข้า com.us.example.bean.person; นำเข้า com.us.example.dao.personrepository; นำเข้า com.us.example.service.demoservice; นำเข้า org.springframework.beans. org.springframework.cache.annotation.cacheevict; นำเข้า org.springframework.cache.annotation.cacheput; นำเข้า org.springframework.cache.annotation.cacheable; นำเข้า org.springframework.stereotype.service; */ @ServicePublic Class DemoserviceImpl ใช้ Demoservice {@autowired ส่วนตัวบุคคลที่มีบุคคล @Override //@cacheput แคชที่เพิ่มหรืออัปเดตข้อมูลใหม่ไปยังแคชซึ่งชื่อแคชคือคน กุญแจสำคัญของข้อมูลคือ ID ของบุคคล @cacheput (value = "people", key = "#person.id") บุคคลสาธารณะบันทึก (บุคคลบุคคล) {person p = personrepository.save (บุคคล); System.out.println ("สำหรับ ID, คีย์คือ:"+p.getId ()+"ข้อมูลแคช"); กลับ P; } @Override // @cacheeVict ลบข้อมูลด้วยรหัสคีย์จากแคชคน @cacheevict (value = "คน") โมฆะสาธารณะลบ (id ยาว) {system.out.println ("ลบแคชข้อมูลด้วยรหัสและคีย์"+id+"); ระบุพารามิเตอร์วิธีการจะถูกบันทึกไว้ในคีย์6. เขียนคอนโทรลเลอร์
เพื่อความสะดวกในการทดสอบได้รับการใช้งาน
แพ็คเกจ com.us.example.controller; นำเข้า com.us.example.bean.person; นำเข้า com.us.example.service.demoservice; นำเข้า org.springframework.beans.factory.annotation.autoWired; นำเข้า org.springframework.stereotype.controller. org.springframework.web.bind.annotation.requestmapping; นำเข้า org.springframework.web.bind.annotation.responsebody; นำเข้า org.springframework.web.bind.annotation.restcontroller; */ @restcontrollerpublic คลาส cachecontroller {@autoWired Demoservice Demoservice; // http: // localhost: 8080/ใส่? name = abel & age = 23 & address = shanghai @requestmapping ("/put") บุคคลสาธารณะใส่ (บุคคลบุคคล) {return demoservice.save (บุคคล); } // http: // localhost: 8080/able? id = 1 @requestmapping ("/able") @responsebody บุคคลสาธารณะ cacheable (บุคคลบุคคล) {return demoservice.findone (บุคคล); } // http: // localhost: 8080/evit? id = 1 @requestmapping ("/evit") สตริงสาธารณะ evit (Long id) {demoservice.remove (id); กลับ "ตกลง"; -7. เริ่มแคช
อย่าลืมเปิดใช้งานการกำหนดค่าแคชในคลาสเริ่มต้น
แพ็คเกจ com.us.example; นำเข้า org.springframework.boot.autoconfigure.springbootapplication; นำเข้า org.springframework.cache.annotation.enablecaching; นำเข้า org.springframework.conetext.configurableapplication; org.springframework.boot.springapplication.*;/*** สร้างโดย Yangyibo เมื่อวันที่ 17/1/13 */@componentscan (basepackages = "com.us.example")@springbootapplication@enablecachingpublic คลาสแอปพลิเคชัน {โมฆะคงที่สาธารณะหลัก (สตริง [] args) {configurableapplicationContext Run = run (application.class, args); -8. การทดสอบการตรวจสอบและความสามารถในการตรวจสอบ:
เริ่มคลาสแอปพลิเคชันและหลังจากเริ่มต้นให้ป้อน: http: // localhost: 8080/Able? id = 1 ในเบราว์เซอร์ (ก่อนอื่นเพื่อเริ่มต้นข้อมูลหลายชิ้นในฐานข้อมูล)
เอาต์พุตคอนโซล:
"ข้อมูลของ ID และคีย์คือ: 1" ได้รับการแคชสำหรับแบบสอบถามนี้ หากคุณสอบถามข้อมูลอีกครั้งคำสั่งนี้จะไม่ปรากฏขึ้นนั่นคือฐานข้อมูลจะไม่ถูกสอบถาม
การตรวจสอบ
ป้อนในเบราว์เซอร์: http: // localhost: 8080/put? name = abel & age = 23 & address = Shanghai (แทรกข้อมูลลงในฐานข้อมูลและใส่ข้อมูลลงในแคช)
ในเวลานี้คอนโซลเอาท์พุทแคชบันทึก:
จากนั้นเรียกใช้วิธี ABLE อีกครั้งเพื่อสอบถามข้อมูล ฐานข้อมูลจะไม่ถูกสอบถามอีกต่อไปและข้อมูลจะถูกอ่านโดยตรงจากแคช
ทดสอบ Evit
Enter: http: // localhost: 8080/evit? id = 1 ในเบราว์เซอร์ (ล้างบันทึกจากแคชหลังจากล้างบันทึกจะถูกใส่ลงในแคชอีกครั้งหลังจากเข้าถึงบันทึก)
เอาต์พุตคอนโซล:
สลับแคช
1. เปลี่ยนเป็น ehcache เป็นแคช
เพิ่มการพึ่งพาในไฟล์ pom.xml
<Ederency> <roupId> net.sf.ehcache </groupId> <ratifactid> ehcache </artifactid> </dercendency>
สร้างไฟล์การกำหนดค่า ehcache.xml ในโฟลเดอร์ทรัพยากร เนื้อหาของไฟล์กำหนดค่า EHCACHE ใหม่มีดังนี้ File Spring Boot นี้จะสแกนโดยอัตโนมัติ
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <ehcache> <!-ใช้เมื่อเปลี่ยนไปใช้แคช ehcache-> <cache name = "people" maxelementsInmemory = "1000" /> </ehcache>
2. เปลี่ยนเป็น Guava เป็นแคช
เพียงเพิ่มการพึ่งพาใน POM
<Effercy> <mergelId> com.google.guava </groupId> <ratifactid> Guava </artifactId> <version> 18.0 </Servive>
3. เปลี่ยนเป็น Redis เป็นแคช
โปรดอ่านบล็อกถัดไป
การอ้างอิงสำหรับบทความนี้: "ผู้ทำลายการพัฒนา Javaee: Spring Boot Battle Battle"
ซอร์สโค้ดที่มาของบทความนี้: https://github.com/527515025/springboot.git
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น