java 数据缓存 singleton

package com.shiaowu.util;

import java.util.*;

import com.xinyi.servicetest.dao.GetAddressDao;
import com.xinyi.servicetest.entity.Address;

public class DataCache {
protected static final HashMap map = new HashMap(); // Cache table

private static final Object lock = new Object();
private DataCache() {} // 防止在外部实例化

public static Address getData(String key, String searchCnt) {
  Address address = null;
  if(searchCnt.toUpperCase().equals("N"))
  {
   address = (Address)map.get(new String(key));  
   if(address!=null)
   {
    int i = address.getPAGE()+1;
    address.setPAGE(i);
   }    
  }
  else if (address == null)
  {
   synchronized(lock)
   {
    //add = (Address)map.get(new String(sKey)); // Check again to avoid re-load   
     System.out.println("loadDataSource.....");
     loadDataSource(key,searchCnt);
     address = (Address)map.get(new String(key)); // retrieves data.      
   }
  }
  return address;
}
/*
*Load data from data source.
*/
protected static synchronized void loadDataSource(String key,String SearchCnt) { 
  Address address = null;
  address = GetAddressDao.getAddress(SearchCnt);    
  map.put(new String(key), address);
}
}

你可能感兴趣的:(java,DAO,cache)