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適用於許多表以及如何在這裏實施的緩存策略?
謝謝。
不知道,但我強烈建議對通用緩存所有策略。應該只將緩存專門應用於「受傷的地方」。測量(例如響應時間)以查找熱點,調整,然後再次測量。 –
我會在稍後做。 – peterwkc