我从我的一个朋友那里得到了这个问题。
问题)我想写一个类,它可以为每个键的“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 Data\tKey:: " + entry.getKey() + "\tValue:: " + entry.getValue());
}
}
public void printIterationMap(){
System.out.println("==================================================================");
for(Map.Entry<String, Integer> entry:iterationMap.entrySet()){
System.out.println("Iteration Map Data\tKey:: " + 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();
}
}
答案 0 :(得分:0)
第一个问题:我也会使用HashMap而不是扩展它,因为你的Cache系统ISN是一个HashMap;它只是一个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;
}
}
}
我们的想法是拥有一个内部类&#34; CacheItem&#34;这可以防止你不得不维护2个不同的地图,这可能会导致密钥不一致。此外,我认为,在读取/写入缓存的算法中有一些改进。