Caching berarti bahwa objek yang oleh program atau sistem sering disebut disimpan dalam memori, dan dapat dipanggil dengan cepat ketika digunakan sekali, tanpa membuat instance duplikat baru. Melakukan hal itu dapat mengurangi overhead sistem dan meningkatkan efisiensi sistem.
Cache dapat dibagi menjadi dua kategori:
1. Melalui caching file, seperti namanya, caching file mengacu pada menyimpan data pada disk, apakah Anda berada dalam format XML, format DAT file serial atau format file lainnya;
2. Cache memori, yaitu, mengimplementasikan peta statis di kelas, dan melakukan penambahan, penghapusan, dan pencarian peta ini secara teratur.
impor java.util.*; // Deskripsi: Kelola cache // fungsi yang dapat diperluas: Ketika chche mencapai memori meluap, beberapa objek cache paling awal harus dibersihkan, yang membutuhkan penghematan waktu pembuatan untuk setiap objek cache kelas publik cacheemanager {private static hashmap cacheMap = new hashMap (); // instance tunggal konstruktor cacheManager () {super (); } // Dapatkan boolean cache public static boolean getsimpleflag (tombol string) {coba {return (boolean) cachemap.get (key); } catch (nullpointerException e) {return false; }} public static long getServerStartDT (tombol string) {coba {return (long) cachemap.get (key); } catch (Exception ex) {return 0; }} // Setel Boolean Cache Public Public Sinkronisasi Static Boolean SetSimpleFlag (tombol string, bendera boolean) {if (flag && getSimpleFlag (key)) {// Jika benar, timpa tidak diperbolehkan dikembalikan false; } else {cachemap.put (tombol, flag); Kembali Benar; }} public Sinkronisasi statis boolean setSimpleFlag (tombol string, serverbegrundt panjang) {if (cachemap.get (key) == null) {cachemap.put (tombol, serverbegrundt); Kembali Benar; } else {return false; }} // Dapatkan cache. Metode statis yang disinkronkan privat Sinkronisasi statis cache getCache (tombol string) {return (cache) cachemap.get (key); } // menilai apakah ada cache private private yang disinkronkan boolean hascache (tombol string) {return cachemap.containskey (key); } // Bersihkan semua cache public yang disinkronkan statis void clearAll () {cachemap.clear (); } // Bersihkan jenis cache tertentu, dan tentukan apakah kunci dan tipe yang masuk cocok dengan void void clearAll (tipe string) {iterator i = cachemap.entryset (). Iterator (); Tombol string; Arraylist arr = new arraylist (); coba {while (i.hasnext ()) {java.util.map.entry entri = (java.util.map.entry) i.next (); key = (string) entri.getKey (); if (key.startswith (type)) {// jika cocok, hapus arr.add (key); }} untuk (int k = 0; k <arr.size (); k ++) {clearOnly (arr.get (k)); }} catch (exception ex) {ex.printstacktrace (); }} // hapus cache public public yang disinkronkan statis void clearOnly (tombol string) {cachemap.remove (key); } // Muat cache public public disinkronkan static void putcache (tombol string, cache obj) {cachemap.put (tombol, obj); } // Dapatkan informasi cache public static cache getCacheInfo (tombol string) {if (hascache (key)) {cache cache = getCache (key); if (CacheExPired (cache)) {// hubungi untuk menentukan apakah akan mengakhiri metode cache.setExpired (true); } mengembalikan cache; } lain return null; } // Muat informasi cache public static void putcacheInfo (tombol string, cache obj, panjang DT, boolean kedaluwarsa) {cache cache = cache baru (); cache.setkey (key); cache.setTimeout (dt + system.currentTimemillis ()); // Setel berapa lama untuk memperbarui cache cache.setValue (OBJ); cache.setExired (kedaluwarsa); // Ketika cache dimuat secara default, status terminasi adalah false cacheMap.put (tombol, cache); } // Tulis ulang metode informasi load cache statis public static putcacheInfo (tombol string, cache obj, panjang dt) {cache cache = cache baru (); cache.setkey (key); cache.setTimeout (dt+system.currentTimemillis ()); cache.setValue (OBJ); cache.setExired (false); cachemap.put (kunci, cache); } // menilai apakah cache menghentikan public static boolean cacheExpired (cache cache) {if (null == cache) {// cache yang masuk tidak ada pengembalian false; } long nowdt = system.currentTimeMillis (); // Jumlah milidetik saat ini dalam sistem long cacachedt = cache.getTimeout (); // Jumlah milidetik yang kadaluwarsa dalam cache IF (cacheedt <= 0 || Cachedt> nowdt) {// Ketika waktu kedaluwarsa kurang dari atau sama dengan nol, atau waktu kedaluwarsa lebih besar dari waktu saat ini, itu adalah False Return False; } else {// Waktu kedaluwarsa lebih besar dari waktu kedaluwarsa, yaitu, kadaluwarsa kadaluwarsa true; }} // Dapatkan ukuran dalam cache public static int getCachesize () {return cachemap.size (); } // Dapatkan ukuran tipe public static int getCachesize (tipe string) {int k = 0; Iterator I = cachemap.entryset (). Iterator (); Tombol string; coba {while (i.hasnext ()) {java.util.map.entry entri = (java.util.map.entry) i.next (); key = (string) entri.getKey (); if (key.indexof (type)! = -1) {// Jika cocok, hapus k ++; }}} catch (exception ex) {ex.printstacktrace (); } return k; } // Dapatkan semua nama nilai kunci dalam objek cache public static arrayList getCacheallKey () {arraylist a = new arraylist (); coba {iterator i = cachemap.entryset (). iterator (); while (i.hasnext ()) {java.util.map.entry entri = (java.util.map.entry) i.next (); a.add ((string) entri.getKey ()); }} catch (Exception ex) {} akhirnya {return a; }} // Dapatkan nama nilai kunci dari tipe yang ditentukan dalam objek cache Public Static ArrayList getCachelistKey (tipe string) {arraylist a = new arraylist (); Tombol string; coba {iterator i = cachemap.entryset (). iterator (); while (i.hasnext ()) {java.util.map.entry entri = (java.util.map.entry) i.next (); key = (string) entri.getKey (); if (key.indexof (type)! = -1) {a.add (key); }}} catch (exception ex) {} akhirnya {return a; }}} paket lhm.hcy.guge.frameset.cache; Kelas Publik Cache {Private String Key; // Cache ID Private Object Nilai; // Cache Data Private Long Timeout; // Perbarui Waktu Private Boolean kedaluwarsa; // jangan hentikan cache publik () {super (); } cache publik (tombol string, nilai objek, waktu lama, boolean kedaluwarsa) {this.key = key; this.value = nilai; this.timeout = timeout; this.Expired = kedaluwarsa; } public String getKey () {return key; } public long getTimeout () {return timeout; } objek publik getValue () {nilai pengembalian; } public void setKey (string string) {key = string; } public void setTimeout (long l) {timeout = l; } public void setValue (objek objek) {value = objek; } public boolean isExired () {return kadaluwarsa; } public void setExpired (boolean b) {kedaluwarsa = b; }} // Kelas tes, tes kelas {public static void main (string [] args) {System.out.println (cacheManager.getsimpleFlag ("alksd")); // cachemanager.putcache ("ABC", Cache baru ()); // cacheManager.putcache ("def", cache baru ()); // cachemanager.putcache ("ccc", cache baru ()); // cachemanager.clearonly (""); // cache c = cache baru (); // untuk (int i = 0; i <10; i ++) {// cachemanager.putcache (""+i, c); //} // cacheManager.putcache ("aaaaaaaa", c); // cachemanager.putcache ("abchcy; alskd", c); // cachemanager.putcache ("cccccccc", c); // cachemanager.putcache ("abcoqiwhcy", c); // System.out.println ("Ukuran sebelum dihapus:"+cacheManager.getCachesize ()); // cachemanager.getCacheallkey (); // cachemanager.clearall ("aaaa"); // System.out.println ("Ukuran Setelah Penghapusan:"+CacheManager.getCachesize ()); // cachemanager.getCacheallkey (); }}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.