在平时做项目的时候,有一些数据并不是很频繁的改动,而且这些数据在很多地方用到,这个时候就可以把这个数据做成缓存的形式,一旦有了缓存,就不用每次用到的时候,就去数据库里读取,只需要第一次拿到数据以后,以后就可以从缓存里面提取数据。一个通常的处理方式,就是在应用程序启动的时候,就从数据库读取数据,然后保存在缓存中.
缓存的数据放在上面地方呢,在JAVA中比较方便的是放在hashmap里面。注意hashmap 是非线程安全的,所以要注意同步处理.
虽然数据是保存在缓存中,也是可以更新的。你可以结合定时器规则,比如每天晚上半夜时候更新一次,或者其他规律。
由于项目用到spring 框架,因此我用了spring 来配置singleton 模式的bean
<bean id="customerCache" class="com.mycommon.cache.CustomerCache" init-method="loadAll" scope="singleton" >
指定了init-method 方法,应用程序启动就加载,同时指定了scope=singleton ,说明这个类是单例模式.
实现一个cache类的接口
package com.mycommon.cache;
public interface CacheLoader {
public void loadAll();
public void reloadAll();
}
实现一个具体的cache,我用的一个客户表,Custom 做的测试,具体相关dao层的配置这些不是重点,重点是 如何与hashmap结合,实现缓存的
package com.mycommon.cache;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.card.dao.CustomerDAO;
import com.card.model.Customer;
@Component
public class CustomerCache implements CacheLoader {
private static HashMap<String,Object> cacheMap;
@Autowired
private CustomerDAO customerDAO;
private static CustomerCache cuscache;
private byte[] lock = new byte[0];
public static CustomerCache getInstance()
{
if(cuscache== null)
{
cuscache = new CustomerCache();
}
return cuscache;
}
public CustomerCache(){
}
public synchronized void loadAll() {
System.out.println("==========="+"得到缓存===================");
cacheMap=new HashMap<String,Object>();
List<Customer> cuslist = customerDAO.getAllCache();
if(cuslist != null){
setCacheMap(cuslist);
}
System.out.println("==========="+"得到缓存完毕===================");
}
public synchronized void reloadAll() {
System.out.println("==========="+"重新得到缓存===================");
List<Customer> cuslist = customerDAO.getAllCache();
if(cuslist != null){
cacheMap.clear();
setCacheMap(cuslist);
}
System.out.println("==========="+"重新得到缓存===================");
}
private void setCacheMap(List<Customer> cuslist) {
for (Customer cus : cuslist) {
cacheMap.put(String.valueOf(cus.getId()),cus);
}
}
public synchronized void add(Customer cus){
cacheMap.put(String.valueOf(cus.getId()),cus);
}
public synchronized void update(Customer cus) {
cacheMap.put(String.valueOf(cus.getId()), cus);
}
public synchronized Customer findCusByID(String id){
if(cacheMap.values()!=null && cacheMap.values().size()>0){
for (Iterator iter = cacheMap.values().iterator(); iter.hasNext();) {
Customer cus = (Customer) iter.next();
if(id.equals(String.valueOf(cus.getId()))){
return cus;
}
}
}
return null;
}
public synchronized List<Customer> getallCache(){
List<Customer> listAll=new ArrayList<Customer>();
for (Iterator iter = cacheMap.values().iterator(); iter.hasNext();) {
Customer cus = (Customer) iter.next();
listAll.add(cus);
}
return listAll;
}
public void removeCusCache(Customer cus) {
synchronized (lock) {
cacheMap.remove(String.valueOf(cus.getId()));
}
}
//----------------------------------------------------------------------
public CustomerDAO getCustomerDAO() {
return customerDAO;
}
public void setCustomerDAO(CustomerDAO customerDAO) {
this.customerDAO = customerDAO;
}
}
一旦应用程序启动以后,就可以直接用 CustomerCache.getInstance 得到这个单例模式的实例。然后在调用这个实例的各种方法,比如 tmpcache.getallCache() 等等
CustomerCache tmpcache=CustomerCache.getInstance();
System.out.println("tmpcache"+tmpcache);
System.out.println("cache:===");
System.out.println( tmpcache.getallCache());
缓存的数据放在上面地方呢,在JAVA中比较方便的是放在hashmap里面。注意hashmap 是非线程安全的,所以要注意同步处理.
虽然数据是保存在缓存中,也是可以更新的。你可以结合定时器规则,比如每天晚上半夜时候更新一次,或者其他规律。
由于项目用到spring 框架,因此我用了spring 来配置singleton 模式的bean
程序代码
<bean id="customerCache" class="com.mycommon.cache.CustomerCache" init-method="loadAll" scope="singleton" >
指定了init-method 方法,应用程序启动就加载,同时指定了scope=singleton ,说明这个类是单例模式.
实现一个cache类的接口
程序代码
package com.mycommon.cache;
public interface CacheLoader {
public void loadAll();
public void reloadAll();
}
实现一个具体的cache,我用的一个客户表,Custom 做的测试,具体相关dao层的配置这些不是重点,重点是 如何与hashmap结合,实现缓存的
程序代码
package com.mycommon.cache;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.card.dao.CustomerDAO;
import com.card.model.Customer;
@Component
public class CustomerCache implements CacheLoader {
private static HashMap<String,Object> cacheMap;
@Autowired
private CustomerDAO customerDAO;
private static CustomerCache cuscache;
private byte[] lock = new byte[0];
public static CustomerCache getInstance()
{
if(cuscache== null)
{
cuscache = new CustomerCache();
}
return cuscache;
}
public CustomerCache(){
}
public synchronized void loadAll() {
System.out.println("==========="+"得到缓存===================");
cacheMap=new HashMap<String,Object>();
List<Customer> cuslist = customerDAO.getAllCache();
if(cuslist != null){
setCacheMap(cuslist);
}
System.out.println("==========="+"得到缓存完毕===================");
}
public synchronized void reloadAll() {
System.out.println("==========="+"重新得到缓存===================");
List<Customer> cuslist = customerDAO.getAllCache();
if(cuslist != null){
cacheMap.clear();
setCacheMap(cuslist);
}
System.out.println("==========="+"重新得到缓存===================");
}
private void setCacheMap(List<Customer> cuslist) {
for (Customer cus : cuslist) {
cacheMap.put(String.valueOf(cus.getId()),cus);
}
}
public synchronized void add(Customer cus){
cacheMap.put(String.valueOf(cus.getId()),cus);
}
public synchronized void update(Customer cus) {
cacheMap.put(String.valueOf(cus.getId()), cus);
}
public synchronized Customer findCusByID(String id){
if(cacheMap.values()!=null && cacheMap.values().size()>0){
for (Iterator iter = cacheMap.values().iterator(); iter.hasNext();) {
Customer cus = (Customer) iter.next();
if(id.equals(String.valueOf(cus.getId()))){
return cus;
}
}
}
return null;
}
public synchronized List<Customer> getallCache(){
List<Customer> listAll=new ArrayList<Customer>();
for (Iterator iter = cacheMap.values().iterator(); iter.hasNext();) {
Customer cus = (Customer) iter.next();
listAll.add(cus);
}
return listAll;
}
public void removeCusCache(Customer cus) {
synchronized (lock) {
cacheMap.remove(String.valueOf(cus.getId()));
}
}
//----------------------------------------------------------------------
public CustomerDAO getCustomerDAO() {
return customerDAO;
}
public void setCustomerDAO(CustomerDAO customerDAO) {
this.customerDAO = customerDAO;
}
}
一旦应用程序启动以后,就可以直接用 CustomerCache.getInstance 得到这个单例模式的实例。然后在调用这个实例的各种方法,比如 tmpcache.getallCache() 等等
程序代码
CustomerCache tmpcache=CustomerCache.getInstance();
System.out.println("tmpcache"+tmpcache);
System.out.println("cache:===");
System.out.println( tmpcache.getallCache());