2013-01-03 29 views
1

我创建了一个使用spring的ehcache的通用dao,但我想知道如何使用@Cacheable @CachePut。Spring EhCache抽象通用DAO

GenericDAO.java

/** 
* 
*/ 
package com.breeze.bis.core.service.jdbcTemplate; 

import java.sql.SQLException; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 

import org.apache.log4j.Logger; 
import org.checkthread.annotations.ThreadSafe; 
import org.springframework.beans.factory.annotation.Qualifier; 
import org.springframework.cache.annotation.CachePut; 
import org.springframework.cache.annotation.Cacheable; 
import org.springframework.jdbc.core.BatchPreparedStatementSetter; 
import org.springframework.jdbc.core.CallableStatementCreator; 
import org.springframework.jdbc.core.JdbcTemplate; 
import org.springframework.jdbc.core.PreparedStatementCallback; 
import org.springframework.jdbc.core.PreparedStatementCreator; 
import org.springframework.jdbc.core.SqlParameter; 
import org.springframework.stereotype.Component; 
import org.springframework.stereotype.Repository; 
import org.springframework.stereotype.Service; 
import org.springframework.transaction.annotation.Isolation; 
import org.springframework.transaction.annotation.Propagation; 
import org.springframework.transaction.annotation.Transactional; 
import org.springframework.dao.DataAccessException; 
/** 
* @author peter.wong 
* 
*/ 
@Repository 
@Qualifier("genericDaoImpl") 
@Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.DEFAULT, rollbackFor={Exception.class, SQLException.class, DataAccessException.class}, timeout=9999) 
public class GenericDAOImpl implements GenericDAO { 

    private JdbcTemplate jdbcDao; 

    private static final Logger logger = Logger.getLogger(GenericDAOImpl.class); 

    private static final int FETCH_SIZE = 500; 
    private static final int MAX_ROW = 500; 
    // ========================================================================================================= 

    /** 
    * 
    */ 
    public GenericDAOImpl() { 

    } 

    /** 
    * @return the jdbcDao 
    */ 
    public JdbcTemplate getJdbcDao() { 
     return jdbcDao; 
    } 

    /** 
    * @param jdbcDao the jdbcDao to set 
    */ 
    public void setJdbcDao(JdbcTemplate jdbcDao) { 
     this.jdbcDao = jdbcDao; 
    } 

    /** 
    * 
    */ 
    public void initJDBC() { 
     jdbcDao.setFetchSize(FETCH_SIZE); 
     jdbcDao.setMaxRows(MAX_ROW); 
    } 

    /* (non-Javadoc) 
    * @see com.breeze.bis.core.service.jdbcTemplate.GenericDAO#createObject(java.lang.String, java.lang.Object) 
    */ 
    @Override 
    @CachePut(value = "genericDao") 
    @SuppressWarnings({ "rawtypes", "unchecked" }) 
    @ThreadSafe 
    public int insert(String sql, Map<Integer, Object> paramMap) { 

     boolean hasResult = true, hasKey = true; 
     Integer id = -1; 

     IStatementExecutor stmtExecutor = new UpdateStatementExecutor(hasKey); 
     IExtractor integerExtr = new IntegerExtractor(null, hasResult); 

     IDatabaseExecutor executor = new GenericDatabaseExecutor(stmtExecutor, integerExtr); 
     PreparedStatementCreator stmtCreator = new GenericPreparedStatementCreator(hasKey, sql, paramMap); 

     PreparedStatementCallback stmtCallback = new GenericPreparedStatementCallback(executor); 

     try { 
      id = this.jdbcDao.execute(stmtCreator, stmtCallback); 
     } catch (DataAccessException exp) { 
      logger.error(exp.getMessage()); 
     } 

     return id; 
    } 

    /* (non-Javadoc) 
    * @see com.breeze.bis.core.service.jdbcTemplate.GenericDAO#update(com.breeze.bis.core.service.jdbcTemplate.GenericPreparedStatementCreator) 
    */ 
    @Override 
    @CachePut(value = "genericDao") 
    @ThreadSafe 
    public int update(PreparedStatementCreator stmt) { 
     int num = -1; 

     try { 
      num = this.jdbcDao.update(stmt); 
     } catch (DataAccessException exp) { 
      logger.error(exp.getMessage()); 
     } 

     return num; 
    } 

    /* (non-Javadoc) 
    * @see com.breeze.bis.core.service.jdbcTemplate.GenericDAO#batchUpdate(java.lang.String, java.util.List) 
    */ 
    @Override 
    @CachePut(value = "genericDao") 
    @ThreadSafe 
    public int[] batchUpdate(String sql, BatchPreparedStatementSetter stmtSetter) { 

     int num[] = null; 

     try { 
      num = this.jdbcDao.batchUpdate(sql, stmtSetter); 
     } catch (DataAccessException exp) { 
      logger.error(exp.getMessage()); 
     } 

     return num; 
    } 

    /* (non-Javadoc) 
    * @see com.breeze.bis.core.service.jdbcTemplate.GenericDAO#callProcedure() 
    */ 
    @Override 
    @Cacheable(value = "genericDao") 
    @ThreadSafe 
    public Map<String, Object> callProcedure(CallableStatementCreator stmt, List<SqlParameter> paramList) { 
     Map<String, Object> result = null; 

     try { 
      result = this.jdbcDao.call(stmt, paramList); 
     } catch (DataAccessException exp) { 
      logger.error(exp.getMessage()); 
     } 

     return result; 

    } 



} 

这GenericDAO适用于许多表以及如何在这里实施的缓存策略?

谢谢。

+1

不知道,但我强烈建议对通用缓存所有策略。应该只将缓存专门应用于“受伤的地方”。测量(例如响应时间)以查找热点,调整,然后再次测量。 –

+1

我会在稍后做。 – peterwkc

回答

0

执行缓存策略之前先测量。