(1) pom.xml แนะนำแพ็คเกจ JAR ดังนี้:
<การพึ่งพา> <roupId> org.springframework.boot </groupId> <ratifactid> Spring-Boot-Starter-Data-Redis </artifactid>
(2) แก้ไขคลาสเริ่มต้นโครงการเพิ่มคำอธิบายประกอบ @EnableCaching และเปิดใช้งานฟังก์ชันแคชดังต่อไปนี้:
Package Springboot; นำเข้า org.springframework.boot.springapplication; นำเข้า org.springframework.boot.autoconfigure.springbootapplication; นำเข้า org.springframework.cache.annotation.enablecaching; org.springframework.scheduling.annotation.enablescheduling;@springbootapplication@enablecheduling@enablecachingpublic คลาส springbootapplication {โมฆะคงที่สาธารณะ (สตริง [] args) {springapplication.run -(3) กำหนดค่าข้อมูลการเชื่อมต่อ REDIS ใน Application.properties ดังนี้:
# ดัชนีฐานข้อมูล Redis (ค่าเริ่มต้นคือ 0) Spring.redis.database = 0# ที่อยู่เซิร์ฟเวอร์ Redis Server Spring.redis.host = 172.31.19.222# พอร์ตการเชื่อมต่อเซิร์ฟเวอร์เซิร์ฟเวอร์ Redis spring.redis.port = 6379# รหัสผ่านการเชื่อมต่อเซิร์ฟเวอร์ การปิดกั้นสูงสุดเวลารอสำหรับพูลการเชื่อมต่อ (การใช้ค่าลบหมายถึงไม่มีขีด จำกัด ) spring.redis.pool.max-wait = -1# การเชื่อมต่อที่ไม่ได้ใช้งานสูงสุดในการเชื่อมต่อสปริงสปริง Redis.pool.max-idle = 8# การเชื่อมต่อขั้นต่ำในการเชื่อมต่อสปริง
(4) สร้างคลาสการกำหนดค่าแคช REDIS ใหม่ REDISCONFIG ดังต่อไปนี้:
Package Springboot.Config; นำเข้า org.springframework.beans.factory.annotation.value; นำเข้า org.springframework.cache.cachemanager นำเข้า org.springframework.cache.annotation.cachingconfigurersupport; org.springframework.context.annotation.bean; นำเข้า org.springframework.context.annotation.configuration; นำเข้า org.springframework.data.redis.cache.rediscachemanger; org.springframework.data.redis.core.redistemplate; นำเข้า org.springframework.data.redis.core.stringredistemplate; นำเข้า org.springframework.data.redis.serializer.jackson2jsonredisserializer; com.fasterxml.jackson.annotation.propertyaccessor; นำเข้า com.fasterxml.jackson.databind.objectmapper;/** * คลาสการกำหนดค่าแคช Redis * @author szekinwin * */@การกำหนดค่า@enablecachingpublic @Value ("$ {spring.redis.host}") โฮสต์สตริงส่วนตัว; @Value ("$ {Spring.redis.port}") พอร์ต int ส่วนตัว; @Value ("$ {spring.redis.timeout}") หมดเวลา int ส่วนตัว; // กลยุทธ์การสร้างคีย์คีย์ที่กำหนดเอง // @bean // Keygenerator Keygenerator () {// ส่งคืน Keygenerator ใหม่ () {// @override // การสร้างวัตถุสาธารณะ (เป้าหมายวัตถุ, java.lang.reflect.method, วัตถุ ... พารามิเตอร์) {// stringbuffer sb = new Stringbuffer sb.append (target.getClass (). getName ()); // sb.append (method.getName ()); // สำหรับ (object obj: params) {// sb.append (obj.toString ()); //} // ส่งคืน sb.tostring (); Cache Manager @Bean Public CacheManager CacheManager (@SuppressWarnings ("Rawtypes") redistemplate redistemplate) {rediscachemanager cachemanager = ใหม่ rediscachemanager (redistemplate); // ตั้งค่าแคชเวลาหมดอายุ cachemanager.setDefaultExpiration (10,000); กลับ cachemanager; } @Bean Public Redistemplate <String, String> redistemplate (โรงงาน RedisconnectionFactory) {template Stringredistemplate = new Stringredistemplate (โรงงาน); SetSerializer (เทมเพลต); // ตั้งค่าเทมเพลตเครื่องมือการทำให้เป็นอนุกรม AFTERPROPERTIESSET (); เทมเพลตกลับ; } โมฆะส่วนตัว setSerializer (เทมเพลต Stringredistemplate) {@suppresswarnings ({"rawtypes", "unchecked"}) jackson2jsonredisserializer Jackson2jsonredisserializer = new Jackson2jsonredisserializer ObjectMapper OM = New ObjectMapper (); OM.SetVisability (PropertyAccessor.All, jsonautodetect.visability.any); OM.EnabledefaultTyping (ObjectMapper.defaultTyping.non_final); jackson2jsonredisserializer.setObjectmapper (OM); template.setValueserializer (Jackson2jsonredisserializer); -(5) สร้าง USERMAPPER ใหม่ดังนี้:
Package Springboot.dao; นำเข้า org.apache.ibatis.annotations.delete; นำเข้า org.apache.ibatis.annotations.insert; นำเข้า org.apache.ibatis.annotations.mapper; นำเข้า org.apache.ibatis.annotations.param; org.apache.ibatis.annotations.update; นำเข้า org.springframework.cache.annotation.cacheconfig; นำเข้า org.springframework.cache.annotation.cachevict; นำเข้า org.springframework.cache.annotation.cacheput; springboot.domain.user;@mapper@cacheconfig (cachenames = "ผู้ใช้") อินเตอร์เฟสสาธารณะ usermapper {@insert ("แทรกลงในผู้ใช้ (ชื่อ, อายุ) ค่า (#{ชื่อ},#{อายุ})") intuterser (@param ("ชื่อ" @Select ("เลือก * จากผู้ใช้ที่ id =#{id}") @Cacheable (key = "#p0") ผู้ใช้ findById (@param ("id") id id); @CachePut (key = "#p0") @update ("อัปเดตชื่อผู้ใช้ชื่อ =#{ชื่อ} โดยที่ id =#{id}") void updateById (@param ("id") id id,@param ("ชื่อ") ชื่อสตริง); // ถ้าระบุเป็นจริงแคชทั้งหมดจะถูกล้างออกทันทีหลังจากวิธีการที่เรียกว่า @cacheevict (key = "#p0", allentries = true) @delete ("ลบจากผู้ใช้ที่ id =#{id}") เป็นโมฆะ deletebyId (@param ("id")@cacheable แคชผลลัพธ์การสืบค้นเป็น Redis (key = "#p0") ระบุพารามิเตอร์แรกที่ส่งผ่านเป็นกุญแจของ Redis
@CachePut ระบุคีย์ซิงโครไนซ์ผลลัพธ์ที่อัปเดตเป็น REDIS
@CacheEvict, ระบุคีย์, ลบข้อมูลแคช, allentries = true, แคชจะถูกล้างทันทีหลังจากวิธีการเรียก
(6) เลเยอร์บริการและเลเยอร์คอนโทรลเลอร์ถูกรวมเข้าด้วยกันเช่นบทความก่อนหน้า เริ่มเซิร์ฟเวอร์ Redis คุณสามารถอ้างถึงบล็อกก่อนหน้าสำหรับการติดตั้งและเริ่มต้นเซิร์ฟเวอร์ Redis ที่อยู่มีดังนี้:
http://www.cnblogs.com/gdpuzxs/p/6623171.html
(7) กำหนดค่าข้อมูลบันทึกดังนี้:
## log4j การกำหนดค่า log4j.rootCategory = debug, stdout ## เอาต์พุตคอนโซล log4j.appender.stdout = org.apache.log4j.consoleAppenderLog4j.appender.stdout.layout = org.apache.log4j.patternlayoutlog4j.appender.stdout.layout.conversionPattern = %D %c {1}: %l - %m %n(8) ตรวจสอบแคช Redis
ก่อนอื่นเราแทรกชิ้นส่วนของข้อมูลลงในตารางผู้ใช้และฐานข้อมูลจะแสดงดังนี้:
ตอนนี้ลองสอบถามข้อมูลด้วย ID = 24 ในตารางผู้ใช้และเอาต์พุตข้อมูลจากคอนโซล Guanzi มีดังนี้:
ผ่านข้อมูลเอาต์พุตคอนโซลเราสามารถรู้ได้ว่าครั้งนี้มีการดำเนินการสืบค้นฐานข้อมูลและผลลัพธ์การสืบค้นแคช Redis ถูกเปิดใช้งาน ต่อไปเราสอบถามข้อมูลด้วย ID = 24 ในตารางผู้ใช้อีกครั้งและสังเกตคอนโซลดังนี้:
ผ่านข้อมูลเอาต์พุตคอนโซลเราสามารถรู้ได้ว่าในครั้งนี้เราไม่ได้เรียกใช้การสืบค้นฐานข้อมูล แต่แทนที่จะค้นหาจากแคช Redis และส่งคืนผลลัพธ์การสืบค้น เราตรวจสอบข้อมูลใน Redis ดังนี้:
วิธีการค้นหาวิธีการใช้คำอธิบายประกอบ @Cacheable (key = "#p0") ซึ่งหมายความว่าใช้ ID เป็นค่าคีย์ใน Redis เมื่อเราอัปเดตข้อมูลเราควรใช้ @cacheput (key = "#p0") เพื่ออัปเดตข้อมูลแคชมิฉะนั้นข้อมูลสกปรกจะถูกสอบถาม
สรุป
ข้างต้นเป็นวิธีการใช้งานของ Springboot โดยใช้ Redis Cache แนะนำให้คุณรู้จักโดย Editor ฉันหวังว่ามันจะเป็นประโยชน์กับคุณ หากคุณมีคำถามใด ๆ โปรดฝากข้อความถึงฉันและบรรณาธิการจะตอบกลับคุณทันเวลา!