แนะนำ
เมื่อใช้ MyBatis คุณสามารถใช้แคชรองเพื่อเพิ่มความเร็วในการสืบค้นและปรับปรุงประสบการณ์การใช้งานของผู้ใช้
ใช้ Redis เพื่อสร้างแคชรองของ MyBatis แต่หน่วยความจำสามารถควบคุมได้ หากเซิร์ฟเวอร์แยกต่างหากสำหรับแคชทุติยภูมิ> จะจัดการได้ง่าย
1. แนะนำการพึ่งพา Redis ในไฟล์ pom.xml
<การพึ่งพา> <roupId> org.springframework.boot </groupId> <ratifactid> Spring-Boot-Starter-Data-Redis </artifactid>
2. กำหนดค่า REDIS ในแอปพลิเคชันไฟล์การกำหนดค่า properties
## Redis Spring.redis.database = 0spring.redis.host = 172.16.3.123spring.redis.port = 6379spring.redis.password = Spring.redis.pool.ma x-active = 8spring.redis.pool.max-wait = -1spring.redis.pool.max-idle = 8spring.redis.pool.min-idle = 0spring.redis.timeout = 0
3. สร้างแพ็คเกจแคชแล้วสร้างสองคลาส ApparchContextholder หนึ่งใช้อินเตอร์เฟส ApplicationContextaware เนื้อหาเฉพาะมีดังนี้
แพ็คเกจ com.ruijie.springbootandredis.cache; นำเข้า org.springframework.beans.beansexception; นำเข้า org.springframework.context.applicationContext; นำเข้า org.springframework.context.application ApplicationContexTholder ใช้ ApplicationContextAware {Private applicationcontext ApplicationContext; @Override โมฆะสาธารณะ setApplicationContext (ApplicationContext CTX) พ่น beansexception {ApplicationContext = CTX; } / ** * รับบริบทแอปพลิเคชันจากทุกที่ * * @return * / Public Static ApplicationContext GetApplicationContext () {return ApplicationContext; } / ** * รับถั่วตามคลาส * * @param clazz * @param <t> * @return * / สาธารณะคงที่ <t> t getBean (คลาส <t> clazz) {return applicationcontext.getBean (clazz); } / ** * รับถั่วตามชื่อคลาส * * @param ชื่อ * @param <t> * @return * / สาธารณะคงที่ <t> t getBean (ชื่อสตริง) {return (t) ApplicationContext.getBean (ชื่อ); -4. สร้างคลาส Rediscache เพื่อใช้งานอินเตอร์เฟสแคชเนื้อหาเฉพาะมีดังนี้:
แพ็คเกจ com.ruijie.springbootandredis.cache; นำเข้า org.apache.ibatis.cache.cache; นำเข้า org.slf4j.logger; นำเข้า org.slf4j.loggerfactory; นำเข้า org.springframework.data.redis.core.redistemplate; นำเข้า org.springframework.data.redis.core.valueoperations; นำเข้า java.util.concurrent.timeunit; java.util.concurrent.locks.reentRantReadWriteLock; Rediscache คลาสสาธารณะใช้แคช {ส่วนตัว Logger Logger สุดท้ายคงที่ = LoggerFactory.getLogger (REDISCACHE.CLASS); Private Final ReadWriteLock ReadWriteLock = ใหม่ reentRantReadWriteLock (); รหัสสตริงสุดท้ายส่วนตัว; // แคชอินสแตนซ์ ID ส่วนตัว redistemplate private redistemplate; ส่วนตัวคงที่สุดท้าย Long Expire_time_in_minutes = 30; // redis เวลาหมดอายุการลงโทษสาธารณะ (string id) {ถ้า (id == null) {โยน unlegalargumentException ใหม่ ("อินสแตนซ์แคชต้องใช้รหัส"); } this.id = id; } @Override สตริงสาธารณะ getId () {return id; } / ** * ใส่ผลลัพธ์การสืบค้นไปที่ Redis * * @param key * @param value * / @Override โมฆะสาธารณะ putoBject (คีย์วัตถุค่าวัตถุ) {ลอง {redistemplate redistemplate = getRedistemplate (); ValueOperations opsForValue = redistemplate.opSforValue (); opsforvalue.set (คีย์, ค่า, expire_time_in_minutes, timeunit.minutes); logger.debug ("ใส่ผลลัพธ์การสืบค้นกับ Redis"); } catch (throwable t) {logger.error ("redis ใส่ล้มเหลว", t); }} / ** * รับการสอบถามแคชจาก Redis * * @param key * @return * / @Override วัตถุสาธารณะ getObject (คีย์วัตถุ) {ลอง {redistemplate redistemplate = getRedistemplate (); ValueOperations opsForValue = redistemplate.opSforValue (); logger.debug ("รับการสอบถามแคชผลลัพธ์จาก Redis"); System.out.println ("****"+opsforvalue.get (คีย์) .tostring ()); return opsforvalue.get (คีย์); } catch (throwable t) {logger.error ("Redis ล้มเหลวล้มเหลวไปยัง db", t); คืนค่า null; }} / ** * ลบผลลัพธ์ที่ถูกแคชจาก Redis * * @param key * @return * / @Override @SuppressWarnings ("ไม่ได้ตรวจสอบ") วัตถุสาธารณะลบวุฒิการศึกษา (คีย์วัตถุ) {ลอง Redistemplate.delete (คีย์); logger.debug ("ลบผลการค้นหาแคชออกมาจาก redis"); } catch (throwable t) {logger.error ("redis ลบล้มเหลว", t); } return null; } / ** * ล้างอินสแตนซ์แคชนี้ * / @Override โมฆะสาธารณะล้าง () {redistemplate redistemplate = getRedistemplate (); REDISTEMPLATE.EXECUTE ((rediscallback) การเชื่อมต่อ -> {connection.flushdb (); return null;}); logger.debug ("ล้างการสืบค้นแคชทั้งหมดจาก Redis"); } / ** * วิธีนี้ไม่ได้ใช้ * * @return * / @Override สาธารณะ int getSize () {return 0; } @Override สาธารณะ readWriteLock getReadWriteLock () {return readWriteLock; } ส่วนตัว redistemplate getRedistemplate () {ถ้า (redistemplate == null) {redistemplate = applicationContextholder.getBean ("Redistemplate"); } return redistemplate; -5. อินเตอร์เฟส serializable ควรนำไปใช้ในคลาสเอนทิตีและหมายเลขลำดับควรได้รับการประกาศ
ส่วนตัวคงที่สุดท้าย Long SerialVersionUid = -2566441764189220519L;
6. เปิดแคชระดับ 2 mybatis '
กำหนดค่าในไฟล์กำหนดค่า pom.xml
mybatis.configuration.cache-enabled = true
เพิ่มลงในอินเทอร์เฟซ Mapper
@cachenamespace (emplemation = (com.demo.testdemo.cache.rediscache.class))
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น