How to store properties value in database and convert to re cache in java




How to Cache a File in Java

Introduction

This article shows how to cache files in Java in order to increase application performance. It discusses an algorithm for caching the file, a data structure for holding the cached content and a cache API for storing cached files.

Caching Files in Java

Reading files from the disk can be slow, especially when an application reads the same file many times. Caching solves this problem by keeping frequently accessed files in memory. This allows the application to read the content of the from the fast local memory instead of the slow hard drive. Design for caching a file in Java includes three elements:
  1. An algorithm for caching the file
  2. A data structure for holding the cached content
  3. A cache API for storing cached files

Algorithm for Caching Files

A general algorithm for caching a file must account for file modifications and consists of the following steps:
  1. Get a value from the cache using a fully qualified file path as a key.
  2. If a key is not found, read the file content and put it to the cache.
  3. If the key is found, check if a timestamp of the cached content matches the file timestamp.
  4. If the timestamps are equal, return the cached content.
  5. If the timestamps are not equal, refresh the cache by reading the file and putting it into the cache.
The activity diagram below provides a visual representation of the algorithm:
Caching Files in Java Activity Diagram
Figure 1. Algorithm for caching files.

Class Diagram

The complete class diagram for the application-level file cache consists of an application, a cache and an object that holds the content of the cached file:
Caching Files in Java Class Diagram
For more understanding : click




package com.kartik.re.cache;

import java.util.HashSet;
import java.util.Set;

/**
 * @author kmandal
 *
 */
public abstract class ConfigKeys {

 public static final String USER_THRESHOLD_LIMIT = "USER_THRESHOLD_LIMIT";
 
  //START - EMAIL Configuration Keys
 public static final String EMAIL_NOTIFICATION_ENABLED = "EMAIL_NOTIFICATION_ENABLED";
 public static final String USER_EMAIL_NOTIFICATION_ENABLED = "USER_EMAIL_NOTIFICATION_ENABLED";
 public static final String EMAIL_TO = "EMAIL_TO";
 public static final String EMAIL_CC = "EMAIL_CC";
 public static final String EMAIL_FROM = "EMAIL_FROM";
 public static final String EMAIL_FROM_ALIAS = "EMAIL_FROM_ALIAS";
 public static final String EMAIL_SUBJECT = "EMAIL_SUBJECT";
 public static final String EMAIL_BODY = "EMAIL_BODY";
 public static final String WARNING_ALERT_PERCENTAGE = "WARNING_ALERT_PERCENTAGE";
 //END - EMAIL Configuration Keys
 
 // AUTH_TOKEN_HMAC_KEY is used in ValidationUtil for admin APIs 
 public static final String AUTH_TOKEN_HMAC_KEY = "AUTH_TOKEN_HMAC_KEY";
 
 
 public static final Set<String> configKeys = new HashSet<String>();
 
 static {
  configKeys.add(USER_THRESHOLD_LIMIT);
   
  //START - EMAIL Configuration Keys.
  configKeys.add(EMAIL_NOTIFICATION_ENABLED);
  configKeys.add(USER_EMAIL_NOTIFICATION_ENABLED);
  configKeys.add(EMAIL_TO);
  configKeys.add(EMAIL_CC);
  configKeys.add(EMAIL_FROM);
  configKeys.add(EMAIL_FROM_ALIAS);
  configKeys.add(EMAIL_SUBJECT);
  configKeys.add(EMAIL_BODY);
  configKeys.add(WARNING_ALERT_PERCENTAGE);
  
  //END - EMAIL Configuration Keys.

  
 }
}






package com.kartik.re.cache;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Component;

import com.kartik.re.cache.ConfigKeys;

import com.kartik.re.cache.PropertiesReader;
import com.kartik.re.cache.CobrandConfigurationMongoQueries;

/**
 * @author kmandal
 *
 * TODO : Contention during re-cache.
 */
@Component
public class ConfigurationCache {
 
 private static Map<String,String> configurationCache = new HashMap<String,String>();
 
 public static Date lastUpdated = null;
 
 @PostConstruct
 public void init() {
  for(String key : ConfigKeys.configKeys){
   String value = PropertiesReader.getPropertyByKey(key);
   if(value != null){
    configurationCache.put(key, value);
   }
  }
  CobrandConfigurationMongoQueries ccmq = new CobrandConfigurationMongoQueries();
  configurationCache.putAll(ccmq.getCobrandConfigurationData());
  lastUpdated = new Date();
 }
 
 public static Long getConfigValue(String configKey, Long cobrandId){
  String configValue = configurationCache.get(configKey+"-"+cobrandId);
  if(configValue == null){
   configValue = configurationCache.get(configKey);
  }
  return Long.valueOf(configValue);
 }
 
 public static String getConfigValueAsString(String configKey, Long cobrandId){
  String configValue = configurationCache.get(configKey+"-"+cobrandId);
  if(configValue == null){
   configValue = configurationCache.get(configKey);
  }
  return configValue;
 }
 
 public static void updateCache(String configKey, Long cobrandId, String configValue){
  String cacheKey = configKey+"-"+cobrandId;
  configurationCache.put(cacheKey, configValue);
 }
 
 public static void removeConfigFromCache(String configKey, Long cobrandId){
  String cacheKey = configKey+"-"+cobrandId;
  configurationCache.remove(cacheKey);
 }
 
 public static void updateCache(Map<String,String> map){
  configurationCache.putAll(map);
 }

}

 





package com.kartik.re.cache;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

/**
 * 
 * @author kmandal
 *
 */
public class PropertiesReader {
 
 private static Map<String,String> propertiesCache = new HashMap<String,String>();
 
 /**
  * Injects properties from *.properties(one or more files) from spring framework.
  * @param properties
  */
 public void setProperties(Properties...properties) {
  
  if(properties != null && properties.length > 0) {
   for (Properties property : properties) {
    for (Entry<Object, Object> entry : property.entrySet()) {
     propertiesCache.put((String)entry.getKey(), (String)entry.getValue());
    }
    
   }
  }
 }
 
 /**
  * Gets property value for a given key.
  * @param keyName
  * @return
  */
 public static String getPropertyByKey(final String keyName) {
  return propertiesCache.get(keyName);
 }
 
 /**
  * Replaces {} with value.
  * @param keyName
  * @param replaceBraces
  * @return
  */
 public static String getPropertyByKey(final String keyName, String...replaceBraces) {
  
  String sPropValue = getPropertyByKey(keyName);
    
  if(sPropValue != null && sPropValue.contains("{}")) {
   if (replaceBraces != null && replaceBraces.length > 0) {
    for (String replaceBrace : replaceBraces) {
     sPropValue = sPropValue.replace("{}", replaceBrace);
    }
   }
  }
   
  System.out.println("****Key : "+keyName+", value : "+sPropValue);
  
  return sPropValue;

 }
}


 


Previous
Next Post »