1

我使用 spring 使用 ehcache 创建了一个通用 dao,但我想知道如何使用 @Cacheable、@CachePut。

通用DAO.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适用于很多表,这里的缓存策略如何实现?

谢谢。

4

1 回答 1

0

在实施缓存策略之前先测量。

于 2013-01-07T04:11:30.450 回答