溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

如何使用JDK來(lái)實(shí)現(xiàn)自己的緩存

發(fā)布時(shí)間:2021-11-24 14:18:55 來(lái)源:億速云 閱讀:254 作者:柒染 欄目:編程語(yǔ)言

這篇文章給大家介紹如何使用JDK來(lái)實(shí)現(xiàn)自己的緩存,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

需求分析

項(xiàng)目中經(jīng)常會(huì)遇到這種場(chǎng)景:一份數(shù)據(jù)需要在多處共享,有些數(shù)據(jù)還有時(shí)效性,過(guò)期自動(dòng)失效。比如手機(jī)驗(yàn)證碼,發(fā)送之后需要緩存起來(lái),然后處于安全性考慮,一般還要設(shè)置有效期,到期自動(dòng)失效。我們?cè)趺磳?shí)現(xiàn)這樣的功能呢?

解決方案

  1.  使用現(xiàn)有的緩存技術(shù)框架,比如redis,ehcache。優(yōu)點(diǎn):成熟,穩(wěn)定,功能強(qiáng)大;缺點(diǎn),項(xiàng)目需要引入對(duì)應(yīng)的框架,不夠輕量。

  2.  如果不考慮分布式,只是在單線程或者多線程間作數(shù)據(jù)緩存,其實(shí)完全可以自己手寫(xiě)一個(gè)緩存工具。下面就來(lái)簡(jiǎn)單實(shí)現(xiàn)一個(gè)這樣的工具。

先上代碼:

import java.util.HashMap;  import java.util.Map;  import java.util.concurrent.*;  /**   * @Author: lixk   * @Date: 2018/5/9 15:03   * @Description: 簡(jiǎn)單的內(nèi)存緩存工具類(lèi)   */  public class Cache {   //鍵值對(duì)集合   private final static Map<String, Entity> map = new HashMap<>();   //定時(shí)器線程池,用于清除過(guò)期緩存   private final static ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();   /**   * 添加緩存   *   * @param key 鍵   * @param data 值   */   public synchronized static void put(String key, Object data) {   Cache.put(key, data, 0);   }   /**   * 添加緩存   *   * @param key 鍵   * @param data 值   * @param expire 過(guò)期時(shí)間,單位:毫秒, 0表示***長(zhǎng)   */   public synchronized static void put(String key, Object data, long expire) {   //清除原鍵值對(duì)   Cache.remove(key);   //設(shè)置過(guò)期時(shí)間   if (expire > 0) {   Future future = executor.schedule(new Runnable() {   @Override   public void run() {   //過(guò)期后清除該鍵值對(duì)   synchronized (Cache.class) {   map.remove(key);   }   }   }, expire, TimeUnit.MILLISECONDS);   map.put(key, new Entity(data, future));   } else {   //不設(shè)置過(guò)期時(shí)間   map.put(key, new Entity(data, null));   }   }   /**   * 讀取緩存   *   * @param key 鍵   * @return   */   public synchronized static Object get(String key) {   Entity entity = map.get(key);   return entity == null ? null : entity.getValue();   }   /**   * 讀取緩存   *   * @param key 鍵   * * @param clazz 值類(lèi)型   * @return   */   public synchronized static <T> T get(String key, Class<T> clazz) {   return clazz.cast(Cache.get(key));   }   /**   * 清除緩存   *   * @param key   * @return   */   public synchronized static Object remove(String key) {   //清除原緩存數(shù)據(jù)   Entity entity = map.remove(key);   if (entity == null) return null;   //清除原鍵值對(duì)定時(shí)器   Future future = entity.getFuture();   if (future != null) future.cancel(true);   return entity.getValue();   }   /**   * 查詢(xún)當(dāng)前緩存的鍵值對(duì)數(shù)量   *   * @return   */   public synchronized static int size() {   return map.size();   }   /**   * 緩存實(shí)體類(lèi)   */   private static class Entity {   //鍵值對(duì)的value   private Object value;   //定時(shí)器Future    private Future future;   public Entity(Object value, Future future) {   this.value = value;   this.future = future;   }   /**   * 獲取值   *   * @return   */   public Object getValue() {   return value;   }   /**   * 獲取Future對(duì)象   *   * @return   */   public Future getFuture() {   return future;   }   }  }

本工具類(lèi)主要采用 HashMap+定時(shí)器線程池 實(shí)現(xiàn),map 用于存儲(chǔ)鍵值對(duì)數(shù)據(jù),map的value是 Cache 的內(nèi)部類(lèi)對(duì)象 Entity,Entity 包含 value 和該鍵值對(duì)的生命周期定時(shí)器 Future。Cache 類(lèi)對(duì)外只提供了 put(key, value), put(key, value, expire), get(key), get(key, class), remove(key), size()幾個(gè)同步方法。

當(dāng)添加鍵值對(duì)數(shù)據(jù)的時(shí)候,首先會(huì)調(diào)用remove()方法,清除掉原來(lái)相同 key 的數(shù)據(jù),并取消對(duì)應(yīng)的定時(shí)清除任務(wù),然后添加新數(shù)據(jù)到 map 中,并且,如果設(shè)置了有效時(shí)間,則添加對(duì)應(yīng)的定時(shí)清除任務(wù)到定時(shí)器線程池。

測(cè)試

import java.util.concurrent.ExecutionException;  import java.util.concurrent.ExecutorService;  import java.util.concurrent.Executors;  import java.util.concurrent.Future;  /**   * @Author: lixk   * @Date: 2018/5/9 16:40   * @Description: 緩存工具類(lèi)測(cè)試   */  public class CacheTest {   /**   * 測(cè)試   *   * @param args   */   public static void main(String[] args) throws InterruptedException, ExecutionException {   String key = "id";   //不設(shè)置過(guò)期時(shí)間   System.out.println("***********不設(shè)置過(guò)期時(shí)間**********");   Cache.put(key, 123);   System.out.println("key:" + key + ", value:" + Cache.get(key));   System.out.println("key:" + key + ", value:" + Cache.remove(key));   System.out.println("key:" + key + ", value:" + Cache.get(key));   //設(shè)置過(guò)期時(shí)間   System.out.println("  ***********設(shè)置過(guò)期時(shí)間**********");   Cache.put(key, "123456", 1000);   System.out.println("key:" + key + ", value:" + Cache.get(key));   Thread.sleep(2000);   System.out.println("key:" + key + ", value:" + Cache.get(key));   /******************并發(fā)性能測(cè)試************/   System.out.println("  ***********并發(fā)性能測(cè)試************");   //創(chuàng)建有10個(gè)線程的線程池,將1000000次操作分10次添加到線程池   ExecutorService executorService = Executors.newFixedThreadPool(10);   Future[] futures = new Future[10];   /********添加********/   {   long start = System.currentTimeMillis();   for (int j = 0; j < 10; j++) {   futures[j] = executorService.submit(() -> {   for (int i = 0; i < 100000; i++) {   Cache.put(Thread.currentThread().getId() + key + i, i, 300000);   }   });   }   //等待全部線程執(zhí)行完成,打印執(zhí)行時(shí)間   for (Future future : futures) {   future.get();   }   System.out.printf("添加耗時(shí):%dms  ", System.currentTimeMillis() - start);   }   /********查詢(xún)********/   {   long start = System.currentTimeMillis();   for (int j = 0; j < 10; j++) {   futures[j] = executorService.submit(() -> {   for (int i = 0; i < 100000; i++) {   Cache.get(Thread.currentThread().getId() + key + i);   }   });   }   //等待全部線程執(zhí)行完成,打印執(zhí)行時(shí)間   for (Future future : futures) {   future.get();   }   System.out.printf("查詢(xún)耗時(shí):%dms  ", System.currentTimeMillis() - start);   }   System.out.println("當(dāng)前緩存容量:" + Cache.size());   }  }

測(cè)試結(jié)果:

***********不設(shè)置過(guò)期時(shí)間**********  key:id, value:123  key:id, value:123  key:id, value:null  ***********設(shè)置過(guò)期時(shí)間**********  key:id, value:123456  key:id, value:null  ***********并發(fā)性能測(cè)試************  添加耗時(shí):2313ms  查詢(xún)耗時(shí):335ms  當(dāng)前緩存容量:1000000

測(cè)試程序使用有10個(gè)線程的線程池來(lái)模擬并發(fā),總共執(zhí)行一百萬(wàn)次添加和查詢(xún)操作,時(shí)間大約都在兩秒多,表現(xiàn)還不錯(cuò),每秒40萬(wàn)讀寫(xiě)并發(fā)應(yīng)該還是可以滿(mǎn)足大多數(shù)高并發(fā)場(chǎng)景的^_^

關(guān)于如何使用JDK來(lái)實(shí)現(xiàn)自己的緩存就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

jdk
AI