我从一个朋友那里得到了这个问题。
问题)我想写一个类,它可以为每个键缓存"n"次迭代的数据,之后它将从数据库中获取数据。在为该键再次从数据库中获取数据之后,它应该只在"n"次迭代之后获取数据。对于每个可能来自数据库或缓存的提取,应该减少迭代次数。
问题1)扩展HashMap或编写包含HashMap的类的最佳方法是什么问题2)为上述问题编写代码。
我已经写了下面的代码。请给我建议一个更好的方法。
public class CacheHashMap {
private int iterationValue = 3;
static Map<String, String> dbSimulator = new HashMap<String, String>();
private Map<String, String> cacheMap;
private Map<String, Integer> iterationMap;
static{
dbSimulator.put("Vijay","VJ");
dbSimulator.put("Smith","SM");
dbSimulator.put("Raj","RJ");
}
public CacheHashMap(Map valueMap, int n) {
this.iterationValue = n;
if(null != valueMap){
this.cacheMap = valueMap;
this.iterationMap = new HashMap<String, Integer>();
for(Map.Entry<String, String> entry:cacheMap.entrySet()){
iterationMap.put(entry.getKey(), iterationValue);
}
}
}
public String getValue(String key){
if(null != cacheMap && null != iterationMap){
if(cacheMap.containsKey(key)){
if(0 == iterationMap.get(key)){
cacheMap.put(key, dbSimulator.get(key));
iterationMap.put(key, (iterationValue-1));
return cacheMap.get(key);
}else{
iterationMap.put(key, (iterationMap.get(key)-1));
return cacheMap.get(key);
}
}else{
cacheMap.put(key, dbSimulator.get(key));
iterationMap.put(key, (iterationValue-1));
return cacheMap.get(key);
}
}
return "No data found. Please enter a valid key";
}
public void printCacheMap(){
System.out.println("==================================================================");
for(Map.Entry<String, String> entry:cacheMap.entrySet()){
System.out.println("Cache Map DatatKey:: " + entry.getKey() + "tValue:: " + entry.getValue());
}
}
public void printIterationMap(){
System.out.println("==================================================================");
for(Map.Entry<String, Integer> entry:iterationMap.entrySet()){
System.out.println("Iteration Map DatatKey:: " + entry.getKey() + "tValue:: " + entry.getValue());
}
}
}
public class CacheHashMapExecutor {
public static void main(String[] args) {
Map<String, String> myMap = new HashMap<String, String>();
CacheHashMap cacheHashMap = new CacheHashMap(myMap, 3);
cacheHashMap.getValue("Vijay");cacheHashMap.printCacheMap();cacheHashMap.printIterationMap();
cacheHashMap.getValue("Raj");cacheHashMap.printCacheMap();cacheHashMap.printIterationMap();
cacheHashMap.getValue("Smith");cacheHashMap.printCacheMap();cacheHashMap.printIterationMap();
cacheHashMap.getValue("Vijay");cacheHashMap.printCacheMap();cacheHashMap.printIterationMap();
cacheHashMap.getValue("Raj");cacheHashMap.printCacheMap();cacheHashMap.printIterationMap();
cacheHashMap.getValue("Vijay");cacheHashMap.printCacheMap();cacheHashMap.printIterationMap();
cacheHashMap.getValue("Raj");cacheHashMap.printCacheMap();cacheHashMap.printIterationMap();
cacheHashMap.getValue("Vijay");cacheHashMap.printCacheMap();cacheHashMap.printIterationMap();
cacheHashMap.getValue("Raj");cacheHashMap.printCacheMap();cacheHashMap.printIterationMap();
}
}
第一个问题:我也会用HashMap编写,而不是扩展它,因为你的Cache系统不是HashMap;它只有一个哈希映射。
第二:我会这样做:
public class CacheHashMap {
private int iterationValue = 3;
static Map<String, String> dbSimulator = new HashMap<String, String>();
private Map<String, CacheItem> cacheMap = new HashMap<>();
static{
dbSimulator.put("Vijay","VJ");
dbSimulator.put("Smith","SM");
dbSimulator.put("Raj","RJ");
}
public CacheHashMap(int n) {
this.iterationValue = n;
}
public String getValue(String key) {
CacheItem item = cacheMap.get(key);
if (item == null || item.isExpired()) {
// Load from DB
String value = dbSimulator.get(key);
cacheMap.put(key, new CacheItem(iterationValue, value));
return value;
} else {
return item.getValue();
}
}
private class CacheItem {
private int iteration;
private String value;
public CacheItem(int iteration, String value) {
this.iteration = iteration;
this.value = value;
}
public boolean isExpired() {
iteration--;
return iteration < 0;
}
public String getValue() {
return value;
}
}
}
这个想法是有一个内部类"CacheItem",它可以防止您必须维护两个不同的映射,并冒着密钥不一致的风险。此外,我认为在读/写缓存的算法方面还有一些改进。