自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

面試官:你是如何使用JDK來實(shí)現(xiàn)自己的緩存(支持高并發(fā))?

開發(fā) 架構(gòu) 后端
只是在單線程或者多線程間作數(shù)據(jù)緩存,其實(shí)完全可以自己手寫一個(gè)緩存工具。下面就來簡(jiǎn)單實(shí)現(xiàn)一個(gè)這樣的工具。

[[267393]]

需求分析

項(xiàng)目中經(jīng)常會(huì)遇到這種場(chǎng)景:一份數(shù)據(jù)需要在多處共享,有些數(shù)據(jù)還有時(shí)效性,過期自動(dòng)失效。比如手機(jī)驗(yàn)證碼,發(fā)送之后需要緩存起來,然后處于安全性考慮,一般還要設(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í)完全可以自己手寫一個(gè)緩存工具。下面就來簡(jiǎn)單實(shí)現(xiàn)一個(gè)這樣的工具。

先上代碼: 

  1. import java.util.HashMap;  
  2. import java.util.Map;  
  3. import java.util.concurrent.*;  
  4. /**  
  5.  * @Author: lixk  
  6.  * @Date: 2018/5/9 15:03  
  7.  * @Description: 簡(jiǎn)單的內(nèi)存緩存工具類  
  8.  */  
  9. public class Cache {  
  10.  //鍵值對(duì)集合  
  11.  private final static Map<String, Entity> map = new HashMap<>();  
  12.  //定時(shí)器線程池,用于清除過期緩存  
  13.  private final static ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();  
  14.  /**  
  15.  * 添加緩存  
  16.  *  
  17.  * @param key 鍵  
  18.  * @param data 值  
  19.  */  
  20.  public synchronized static void put(String key, Object data) {  
  21.  Cache.put(key, data, 0);  
  22.  }  
  23.  /**  
  24.  * 添加緩存  
  25.  *  
  26.  * @param key 鍵  
  27.  * @param data 值  
  28.  * @param expire 過期時(shí)間,單位:毫秒, 0表示***長(zhǎng)  
  29.  */  
  30.  public synchronized static void put(String key, Object data, long expire) {  
  31.  //清除原鍵值對(duì)  
  32.  Cache.remove(key);  
  33.  //設(shè)置過期時(shí)間  
  34.  if (expire > 0) {  
  35.  Future future = executor.schedule(new Runnable() {  
  36.  @Override  
  37.  public void run() {  
  38.  //過期后清除該鍵值對(duì)  
  39.  synchronized (Cache.class) {  
  40.  map.remove(key);  
  41.  }  
  42.  }  
  43.  }, expire, TimeUnit.MILLISECONDS);  
  44.  map.put(key, new Entity(data, future));  
  45.  } else {  
  46.  //不設(shè)置過期時(shí)間  
  47.  map.put(key, new Entity(data, null));  
  48.  }  
  49.  }  
  50.  /**  
  51.  * 讀取緩存  
  52.  *  
  53.  * @param key 鍵  
  54.  * @return  
  55.  */  
  56.  public synchronized static Object get(String key) {  
  57.  Entity entity = map.get(key);  
  58.  return entity == null ? null : entity.getValue();  
  59.  }  
  60.  /**  
  61.  * 讀取緩存  
  62.  *  
  63.  * @param key 鍵  
  64.  * * @param clazz 值類型  
  65.  * @return  
  66.  */  
  67.  public synchronized static <T> T get(String key, Class<T> clazz) {  
  68.  return clazz.cast(Cache.get(key));  
  69.  }  
  70.  /**  
  71.  * 清除緩存  
  72.  *  
  73.  * @param key  
  74.  * @return  
  75.  */  
  76.  public synchronized static Object remove(String key) {  
  77.  //清除原緩存數(shù)據(jù)  
  78.  Entity entity = map.remove(key);  
  79.  if (entity == null) return null;  
  80.  //清除原鍵值對(duì)定時(shí)器  
  81.  Future future = entity.getFuture();  
  82.  if (future != null) future.cancel(true);  
  83.  return entity.getValue();  
  84.  }  
  85.  /**  
  86.  * 查詢當(dāng)前緩存的鍵值對(duì)數(shù)量  
  87.  *  
  88.  * @return  
  89.  */  
  90.  public synchronized static int size() {  
  91.  return map.size();  
  92.  }  
  93.  /**  
  94.  * 緩存實(shí)體類  
  95.  */  
  96.  private static class Entity {  
  97.  //鍵值對(duì)的value  
  98.  private Object value;  
  99.  //定時(shí)器Future   
  100.  private Future future;  
  101.  public Entity(Object value, Future future) {  
  102.  this.value = value;  
  103.  this.future = future;  
  104.  }  
  105.  /**  
  106.  * 獲取值  
  107.  *  
  108.  * @return  
  109.  */  
  110.  public Object getValue() {  
  111.  return value;  
  112.  }  
  113.  /**  
  114.  * 獲取Future對(duì)象  
  115.  *  
  116.  * @return  
  117.  */  
  118.  public Future getFuture() {  
  119.  return future;  
  120.  }  
  121.  }  

本工具類主要采用 HashMap+定時(shí)器線程池 實(shí)現(xiàn),map 用于存儲(chǔ)鍵值對(duì)數(shù)據(jù),map的value是 Cache 的內(nèi)部類對(duì)象 Entity,Entity 包含 value 和該鍵值對(duì)的生命周期定時(shí)器 Future。Cache 類對(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()方法,清除掉原來相同 key 的數(shù)據(jù),并取消對(duì)應(yīng)的定時(shí)清除任務(wù),然后添加新數(shù)據(jù)到 map 中,并且,如果設(shè)置了有效時(shí)間,則添加對(duì)應(yīng)的定時(shí)清除任務(wù)到定時(shí)器線程池。

測(cè)試 

  1. import java.util.concurrent.ExecutionException;  
  2. import java.util.concurrent.ExecutorService;  
  3. import java.util.concurrent.Executors;  
  4. import java.util.concurrent.Future;  
  5. /**  
  6.  * @Author: lixk  
  7.  * @Date: 2018/5/9 16:40  
  8.  * @Description: 緩存工具類測(cè)試  
  9.  */  
  10. public class CacheTest {  
  11.  /**  
  12.  * 測(cè)試  
  13.  *  
  14.  * @param args  
  15.  */  
  16.  public static void main(String[] args) throws InterruptedException, ExecutionException {  
  17.  String key = "id" 
  18.  //不設(shè)置過期時(shí)間  
  19.  System.out.println("***********不設(shè)置過期時(shí)間**********");  
  20.  Cache.put(key, 123);  
  21.  System.out.println("key:" + key + ", value:" + Cache.get(key));  
  22.  System.out.println("key:" + key + ", value:" + Cache.remove(key));  
  23.  System.out.println("key:" + key + ", value:" + Cache.get(key));  
  24.  //設(shè)置過期時(shí)間  
  25.  System.out.println("  
  26. ***********設(shè)置過期時(shí)間**********");  
  27.  Cache.put(key, "123456", 1000);  
  28.  System.out.println("key:" + key + ", value:" + Cache.get(key));  
  29.  Thread.sleep(2000);  
  30.  System.out.println("key:" + key + ", value:" + Cache.get(key));  
  31.  /******************并發(fā)性能測(cè)試************/  
  32.  System.out.println("  
  33. ***********并發(fā)性能測(cè)試************");  
  34.  //創(chuàng)建有10個(gè)線程的線程池,將1000000次操作分10次添加到線程池  
  35.  ExecutorService executorService = Executors.newFixedThreadPool(10);  
  36.  Future[] futures = new Future[10];  
  37.  /********添加********/  
  38.  {  
  39.  long start = System.currentTimeMillis();  
  40.  for (int j = 0; j < 10; j++) {  
  41.  futures[j] = executorService.submit(() -> {  
  42.  for (int i = 0; i < 100000; i++) {  
  43.  Cache.put(Thread.currentThread().getId() + key + i, i, 300000);  
  44.  }  
  45.  });  
  46.  }  
  47.  //等待全部線程執(zhí)行完成,打印執(zhí)行時(shí)間  
  48.  for (Future future : futures) {  
  49.  future.get();  
  50.  }  
  51.  System.out.printf("添加耗時(shí):%dms  
  52. ", System.currentTimeMillis() - start);  
  53.  }  
  54.  /********查詢********/  
  55.  {  
  56.  long start = System.currentTimeMillis();  
  57.  for (int j = 0; j < 10; j++) {  
  58.  futures[j] = executorService.submit(() -> {  
  59.  for (int i = 0; i < 100000; i++) {  
  60.  Cache.get(Thread.currentThread().getId() + key + i);  
  61.  }  
  62.  });  
  63.  }  
  64.  //等待全部線程執(zhí)行完成,打印執(zhí)行時(shí)間  
  65.  for (Future future : futures) {  
  66.  future.get();  
  67.  }  
  68.  System.out.printf("查詢耗時(shí):%dms  
  69. ", System.currentTimeMillis() - start);  
  70.  }  
  71.  System.out.println("當(dāng)前緩存容量:" + Cache.size());  
  72.  }  

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

  1. ***********不設(shè)置過期時(shí)間**********  
  2. key:id, value:123  
  3. key:id, value:123  
  4. key:id, value:null  
  5. ***********設(shè)置過期時(shí)間**********  
  6. key:id, value:123456  
  7. key:id, value:null  
  8. ***********并發(fā)性能測(cè)試************  
  9. 添加耗時(shí):2313ms  
  10. 查詢耗時(shí):335ms  
  11. 當(dāng)前緩存容量:1000000 

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

責(zé)任編輯:龐桂玉 來源: 今日頭條
相關(guān)推薦

2025-03-10 11:48:22

項(xiàng)目服務(wù)設(shè)計(jì)

2024-02-20 14:10:55

系統(tǒng)緩存冗余

2025-02-26 12:19:52

2024-01-19 14:03:59

Redis緩存系統(tǒng)Spring

2015-08-13 10:29:12

面試面試官

2021-05-08 08:35:33

Webpack前端性能

2024-05-11 15:11:44

系統(tǒng)軟件部署

2023-10-28 09:13:32

系統(tǒng)面試官架構(gòu)

2024-02-04 10:08:34

2024-12-25 15:44:15

2022-02-14 20:53:33

開源庫(kù)開發(fā)代碼

2023-12-06 13:38:00

Redis緩存穿透緩存擊穿

2024-11-08 13:36:09

2023-12-19 09:24:22

LinuxBIOSUEFI

2024-03-12 10:44:42

2020-10-26 07:07:50

線程安全框架

2020-10-15 06:26:24

高并發(fā)場(chǎng)景冰河

2024-10-15 10:00:06

2023-11-27 08:17:05

SpringJava

2021-09-27 07:11:18

MySQLACID特性
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)