การวิจัยหลักในบทความนี้คือการใช้ interceptor ของฤดูใบไม้ผลิเพื่อการใช้งานแคชที่กำหนดเองและรหัสการใช้งานเฉพาะมีดังนี้
Memcached เป็นระบบแคชวัตถุที่มีประสิทธิภาพสูงสำหรับการจัดทำเว็บแอปพลิเคชันแบบไดนามิกเพื่อลดการโหลดฐานข้อมูล มันลดจำนวนครั้งที่ฐานข้อมูลถูกอ่านโดยข้อมูลแคชและวัตถุในหน่วยความจำซึ่งจะเป็นการเพิ่มความเร็วของเว็บไซต์ไดนามิกที่ขับเคลื่อนด้วยฐานข้อมูล บทความนี้ใช้อินสแตนซ์ memcacheed และสปริงสกัดกั้นเพื่อใช้การปรับแต่งแคช ใช้ Interceptor เพื่ออ่านแท็กแคชแบบกำหนดเองและกลยุทธ์การสร้างคีย์
แพ็คเกจ com.jeex.sci; @target (ElementType.method) @retention (RetentionPolicy.runtime) @inherited @documented สาธารณะ @interface cacheable {string namespace () {คีย์เริ่มต้น หมดอายุ () ค่าเริ่มต้น 1800;} แพ็คเกจ com.jeex.sci; @target (ElementType.method) @retention (RetentionPolicy.runtime) @inherited @documented สาธารณะ @interface cacheeVict {สตริง namespace () (คีย์ string} () - การเรียกใช้วัตถุสาธารณะ (methodInvocation invoction) พ่น {วิธีการ = method = invoction.getMethod (); cacheable c = method.getannotation (cacheable.class); ถ้า (c! = null) {ส่งคืน handlecacheable handlecacheevict (Invoction, ce);} return injection.proceed ();} HandleCacheble ของวัตถุส่วนตัว (วิธีการใช้วิธีการวิธีการวิธีการ, cacheable c) โยนได้ {คีย์สตริง = getKey (การ invoction, keyinfo.fromcaceable (c)); ถ้า (key.equals (")) {ถ้า (log.isdebugenabled (log.warn nstag = (ยาว) memcachedget (c.namespace ()); ถ้า (nstag == null) {nstag = long.valueof (system.currenttimeMillis ()); memcachedset (c.namespace (), 24*3600, long.valueof (nstag)) = null; o = memcachedget (key); if (o! = null) {ถ้า (log.isdebugenabled ()) {log.debug ("แคช Hit: แคชคีย์ =" + คีย์);}} อื่น {if (log.isdebugenabled Invoction.proceed (); memcachedset (คีย์, c.expires (), o);} return o;} วัตถุส่วนตัว HandleCacheEvict (MethodInvocation Invoction, CacheeVict CE) พ่นได้ {String key = getKey (Invoction, KeyInfo.FromCacheEvict (CE)); if (key.equals ("")) {ถ้า (log.isdebugenabled ()) {log.debug ("evicting" + ce.namespace ()); } memcacheddelete (ce.namespace ()); } else {long nstag = (ยาว) memcachedget (ce.namespace ()); if (nstag! = null) {key = makeMemcachedkey (ce.namespace (), nstag, key); if (log.isdebugenabled ()) {log.debug ("evicting" + คีย์); } memcacheddelete (คีย์); }} return injection.proceed (); - // ใช้พารามิเตอร์ที่ถูกดักจับกับวิธีการสร้างพารามิเตอร์สตริงส่วนตัว getKeyWithArgs (Object [] args, int [] argIndex) {StringBuilder key = new StringBuilder (); บูลีนแรก = true; สำหรับ (int index: argIndex) {ถ้า (ดัชนี <0 || index> = args.length) {โยน unlegalargumentException ใหม่ ("ดัชนีออกจากขอบเขต"); } if (! ก่อน) {key.append (':'); } else {first = false; } key = key.append (args [index]); } return key.toString (); - สตริงส่วนตัว getKeyWithProperties (Object O, String Props []) โยนข้อยกเว้น {StringBuilder key = new StringBuilder (); บูลีนแรก = true; สำหรับ (String prop: อุปกรณ์ประกอบฉาก) {// แปลงคุณสมบัติของถั่วเป็นชื่อของเมธอดสตริงเมธอดชื่อ = "รับ" + prop.substring (0, 1) .touppercase () + prop.substring (1); วิธี m = o.getClass (). getMethod (ชื่อวิธี); วัตถุ r = m.invoke (o, (วัตถุ []) null); if (! ก่อน) {key.append (':'); } else {first = false; } key = key.append (r); } return key.toString (); - // ใช้เครื่องกำเนิดไฟฟ้าเพื่อสร้างคีย์ส่วนตัวสตริง getKeyWithGenerator (การฉีดเมธอดการตั้งค่า Keygenerator สตริง) โยนข้อยกเว้น {คลาส <?> ckg = class.forName (keygenerator); CACHEKENGENERATOR IKG = (CACHEYGENERATOR) CKG.NEWINSTANCE (); return ikg.generate (invoction.getarguments ()); - คลาสคงที่คลาสส่วนตัว keyinfo {คีย์สตริง; int [] keyargs; สตริง keyproperties []; keygenerator String; keyinfo แบบคงที่จาก cacheable (cacheable c) {keyinfo ki = keyinfo ใหม่ (); ki.key = c.key (); ki.keyargs = c.keyargs () C.KeyGenerator (); ki.keyproperties = C.KeyProperties (); return ki;} keyinfo แบบคงที่จาก cacheevict (cacheevict ce) {keyinfo ki = keyinfo ใหม่ (); ki.key = ce.key (); ki.keyargs = ce.keyargs (); Ce.KeyGenerator (); ki.keyProperties = ce.keyproperties (); return ki;} คีย์สตริง () {return key;} int [] keyargs () {return keyargs;} สตริง [] keyproperties () {return keyproperties; // set key ด้วยพารามิเตอร์ @cacheable (namespace = "Blacklist", keyargs = {0, 1}) สาธารณะ int othermethond (int a, int b) {return 100; - แพ็คเกจ com.jeex.sci.test; นำเข้า net.spy.memcached.memcachedclient; นำเข้า org.junit.test; นำเข้า org.springframework.context.applicationContext; นำเข้า org.springframework.context InterruptedException {ApplicationContext ctx = ใหม่ filesystemxmlapplicationContext ("/src/test/resources/beans.xml"); memcachedClient MC = (memcachedClient) ctx.getBean ("memcachedClient"); blacklistdaoimpl (Blacklistdaoimpl) ctx.getBean ("Blacklistdaoimpl"); ในขณะที่ (จริง) {system.out.println ("#################################################################################################### - - - เริ่ม #################################################################################### = New BlackListQuery (1, "123.231.23.14"); dao.anothermethond (333, 444); dao.searchblacklistcount2 (query2); dao.searchblacklistcount3 (query2); dao.evict (คำถาม); dao.searchblacklistcount2 (คำถาม); dao.evictall (); dao.searchblacklistcount3 (query2);ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้เกี่ยวกับรหัสตัวอย่างการใช้งานสำหรับแคชที่กำหนดเองโดยใช้ Interceptor ของ Spring และฉันหวังว่ามันจะเป็นประโยชน์กับทุกคน เพื่อนที่สนใจสามารถอ้างถึงหัวข้ออื่น ๆ ที่เกี่ยวข้องในเว็บไซต์นี้ต่อไป หากมีข้อบกพร่องใด ๆ โปรดฝากข้อความไว้เพื่อชี้ให้เห็น ขอบคุณเพื่อนที่ให้การสนับสนุนเว็บไซต์นี้!