I made an attempt to implement a Properties File MRU Cache with a limited cache size which are of the most recently used properties can only be held in. A read miss from this cache leads to a read from the file for that particular property and keep it in the cache until it goes out of the limit where other recently properties gets read into the cache from the file.
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedHashMap;
public class PropertiesFileMRUCache {
private LinkedHashMap<String, String> propertiesMap = new LinkedHashMap<>(
10);
private int limit = 8;
private String propertiesFile = "default.properties";
public PropertiesFileMRUCache() {
}
public PropertiesFileMRUCache(String propertiesFile) {
this.propertiesFile = propertiesFile;
}
public PropertiesFileMRUCache(int limit, String propertiesFile) {
this(propertiesFile);
this.limit = limit;
}
public String getCapital(String country) {
String capital = propertiesMap.get(country);
if (capital == null) {
capital = readFromFile(country);
if (propertiesMap.size() == limit) {
propertiesMap.remove(propertiesMap.keySet().iterator().next());
}
} else {
propertiesMap.remove(country);
}
propertiesMap.put(country, capital);
return capital;
}
private String readFromFile(String country) {
try (BufferedReader br = new BufferedReader(new FileReader(
propertiesFile))) {
for (String line; (line = br.readLine()) != null;) {
if (line.startsWith(country)) {
return line.substring(line.indexOf('=') + 1);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
PropertiesFileMRUCache pfc = new PropertiesFileMRUCache("res\\CountryCapital.properties");
pfc.getCapital("India");
pfc.getCapital("USA");
pfc.getCapital("UK");
pfc.getCapital("Kuwait");
pfc.getCapital("Iraq");
pfc.getCapital("Canada");
pfc.getCapital("Australia");
pfc.getCapital("India");
pfc.getCapital("India");
pfc.getCapital("India");
pfc.getCapital("Germany");
pfc.getCapital("India");
pfc.getCapital("Germany");
pfc.getCapital("China");
pfc.getCapital("Pakistan");
System.out.println(pfc.propertiesMap);
}
}
For testing purpose I provided a user specified property as res\CountryCapital.properties with the default cache limit as 8. The content of the CountryCapital.properties is provided below for reference:
India=New Delhi USA=Washington Egypt=Cairo Srilanka=Columbo South Korea=Seoul Tailand=Bangkok Qutar=Doha Lebanon=Beirut Canada=Toronto Norway=Oslo Sweden=Stockholm Finland=Helsinki China=Beiging Japan=Tokyo Bangladesh=Dhaka Pakistan=Karachi Afganistan=Kabul Australia=Sydney Newzeland=Wellington Ireland=Dublin Germany=Berlin Itlay=Rome Nepal=Kadmantu North Korea=Pyongyang Brazil=Brazilia South Africa=Cape Town France=Paris Czech Repulic=Prague Austria=Vienna Israel=Jerusalem Kuwait=Kuwait City Greece=Athens Iraq=Bagdad Syria=Demascaus Denmark=Copengahen UK=London