1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package ohos.devtools.services; 17 18 import ohos.devtools.datasources.utils.profilerlog.ProfilerLogManager; 19 import org.apache.commons.collections.map.LRUMap; 20 import org.apache.log4j.LogManager; 21 import org.apache.log4j.Logger; 22 23 import java.util.Iterator; 24 import java.util.LinkedHashMap; 25 import java.util.LinkedList; 26 27 /** 28 * LRUCache 29 * 30 * @param <V> v 31 * @since 2021/8/25 32 */ 33 public class LRUCache<V> { 34 private static final Logger LOGGER = LogManager.getLogger(LRUCache.class); 35 36 private final LinkedList<Integer> linkedList = new LinkedList<>(); 37 private final LRUMap lruMap = new LRUMap(1000); 38 39 /** 40 * addCaCheData 41 * 42 * @param key key 43 * @param value value 44 */ addCaCheData(Integer key, V value)45 public void addCaCheData(Integer key, V value) { 46 this.add(key); 47 lruMap.put(key, value); 48 } 49 add(int element)50 private void add(int element) { 51 boolean inserted = false; 52 for (int index = 0; index < linkedList.size(); index++) { 53 if (element < linkedList.get(index)) { 54 linkedList.add(index, element); 55 inserted = true; 56 break; 57 } 58 } 59 if (!inserted) { 60 linkedList.add(element); 61 } 62 } 63 64 /** 65 * getCaCheData 66 * 67 * @param start start 68 * @param end end 69 * @return LinkedHashMap<Integer, V> 70 */ getCaCheData(int start, int end)71 public LinkedHashMap<Integer, V> getCaCheData(int start, int end) { 72 boolean firstEntry = true; 73 boolean endEntry = true; 74 int startIndex; 75 LinkedHashMap<Integer, V> linkedHashMap = new LinkedHashMap<>(); 76 for (int index = 0; index < linkedList.size(); index++) { 77 int integer = linkedList.get(index); 78 if (integer >= start && integer <= end) { 79 if (firstEntry) { 80 startIndex = index - 1; 81 if (startIndex >= 0) { 82 Integer timeBeforeKey = linkedList.get(startIndex); 83 linkedHashMap.put(timeBeforeKey, (V) lruMap.get(timeBeforeKey)); 84 } 85 firstEntry = false; 86 } 87 linkedHashMap.put(integer, (V) lruMap.get(integer)); 88 } else if (integer > end) { 89 if (endEntry) { 90 linkedHashMap.put(integer, (V) lruMap.get(integer)); 91 break; 92 } 93 } else { 94 if (ProfilerLogManager.isInfoEnabled()) { 95 LOGGER.info("linkedHashMap put error"); 96 } 97 } 98 } 99 long removeStartTime = (end - start) >= 10000 ? start : (end - 10000) ; 100 Iterator<Integer> iterator = linkedList.iterator(); 101 while (true) { 102 if (!iterator.hasNext()) { 103 break; 104 } 105 Integer next = iterator.next(); 106 if (next < (removeStartTime - 1000)) { 107 iterator.remove(); 108 } else { 109 break; 110 } 111 } 112 return linkedHashMap; 113 } 114 115 /** 116 * size 117 * 118 * @return int 119 */ size()120 public int size() { 121 return linkedList.size(); 122 } 123 124 /** 125 * isEmpty 126 * 127 * @return boolean 128 */ isEmpty()129 public boolean isEmpty() { 130 return linkedList.isEmpty(); 131 } 132 133 /** 134 * get Map data 135 * 136 * @param key key 137 * @return V 138 */ get(int key)139 public V get(int key) { 140 return (V) lruMap.get(key); 141 } 142 } 143