memperkenalkan
Saat menggunakan mybatis, Anda dapat menggunakan cache sekunder untuk meningkatkan kecepatan kueri dan dengan demikian meningkatkan pengalaman pengguna.
Gunakan Redis untuk membuat cache sekunder Mybatis, tetapi memori dapat dikendalikan. Jika server terpisah digunakan untuk cache sekunder>, mudah dikelola.
1.Tentroduce redis dependensi dalam file pom.xml
<dependency> <GroupId> org.springframework.boot </groupid> <ArTifactId> Spring-boot-starter-data-redis </artifactid> </dependency>
2. Mengkonfigurasi REDIS di file konfigurasi application.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. Buat paket cache, dan kemudian buat dua kelas, satu ApplicationContextholder mengimplementasikan antarmuka ApplicationContextAware. Konten spesifiknya adalah sebagai berikut
package com.ruijie.SpringBootandRedis.cache;import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.stereotype.Component;@Componentpublic class ApplicationContExTholder mengimplementasikan ApplicationContextAware {private static applicationContext applicationContext; @Override public void setApplicationContext (ApplicationContext CTX) melempar beansException {applicationContext = ctx; } / ** * Dapatkan konteks aplikasi dari mana -mana * * @return * / public static applicationContext getApplicationContext () {return applicationContext; } / ** * Dapatkan bean dengan kelas * * @param clazz * @param <t> * @return * / public static <t> t getbean (class <t> clazz) {return applicationContext.getBean (clazz); } / ** * Dapatkan name bean dengan kelas * * @param nama * @param <t> * @return * / public static <t> t getbean (nama string) {return (t) applicationContext.getBean (name); }}4. Buat kelas Rediscache untuk mengimplementasikan antarmuka cache, konten spesifik adalah sebagai berikut:
Paket com.ruijie.springbootandredis.cache; impor org.apache.ibatis.cache.cache; impor org.slf4j.logger; impor org.slf4j.loggerfactory; impor org.springframework.data.redis.core.core.core.coreBack; org.springframework.data.redis.core.redistemplate; impor org.springframework.data.redis.core.valueoperations; impor java.util.concurrent.timeunit; impor java.util.concurrent.locks.readwriture; java.util.concurrent.locks.reentrantreadwritelock; kelas publik Rediscache mengimplementasikan cache {private static final Logger Logger = loggerFactory.getLogger (rediscache.class); Private Final ReadWritelock ReadWritelock = baru reentrantreadwritelock (); ID string final pribadi; // Cache Instance ID Private Redistemplate Redistemplate; private static final long expire_time_in_minutes = 30; // redis waktu kedaluwarsa rediscache publik (string id) {if (id == null) {lempar baru illegalArgumentException ("Cache instance memerlukan id"); } this.id = id; } @Override Public String getId () {return id; } / ** * Masukkan hasil kueri ke redis * * @param key * @param nilai * / @Override public void putoBject (tombol objek, nilai objek) {coba {redistemplate redistemplate = getReDistemplate (); Valueoperations opsforvalue = redistemplate.opsforvalue (); opsforvalue.set (kunci, nilai, kedaluwarsa_time_in_minutes, timeunit.minutes); logger.debug ("Letakkan hasil kueri ke redis"); } catch (throwable t) {logger.error ("redis put faging", t); }} / ** * Dapatkan hasil kueri yang di -cache dari redis * * @param key * @return * / @Override objek publik getObject (tombol objek) {coba {redistemplate redistemplate = getReDistemplate (); Valueoperations opsforvalue = redistemplate.opsforvalue (); Logger.debug ("Dapatkan hasil kueri yang di -cache dari redis"); System.out.println ("****"+opsforvalue.get (key) .toString ()); return opsforvalue.get (key); } catch (Throwable t) {logger.error ("Redis Get Fail, Gagal ke DB", T); kembali nol; }} / ** * Hapus hasil kueri yang di -cache dari redis * * @param key * @return * / @override @suppresswarnings ("uncecked") Objek publik RemoptObject (kunci objek) {coba {redistemplate redistemplate = getRedistemplate (); redistemplate.delete (kunci); logger.debug ("Hapus hasil kueri yang di -cache dari redis"); } catch (throwable t) {logger.error ("redis dihapus gagal", t); } return null; } / ** * menghapus instance cache ini * / @Override public void clear () {redistemplate redistemplate = getredistemplate (); redistemplate.execute ((rediscallback) koneksi -> {connection.flushdb (); return null;}); logger.debug ("Hapus semua hasil kueri yang di -cache dari Redis"); } / ** * Metode ini tidak digunakan * * @return * / @override int int getSize () {return 0; } @Override public readwritelock getReadWritelock () {return readWritelock; } private redistemplate getredistemplate () {if (redistemplate == null) {redistemplate = applicationContextholder.getBean ("redistemplate"); } return redistemplate; }}5. Antarmuka serial harus diimplementasikan di kelas entitas dan nomor urutan harus dinyatakan.
SerialVersionuid Long Statis Pribadi = -2566441764189220519L;
6. Nyalakan cache level 2 mybatis
Konfigurasikan dalam file konfigurasi pom.xml
mybatis.configuration.cache-enabled = true
Tambahkan ke antarmuka mapper
@Cachenamespace (implementasi = (com.demo.testdemo.cache.rediscache.class))
Di atas adalah semua konten artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang dan saya harap semua orang akan lebih mendukung wulin.com.