MyBatis + Redis缓存。可能吗?

时间:2013-02-13 06:52:44

标签: java spring caching redis mybatis

我有一个小项目只是为了检查一切是如何运作的。我已经实现了MyBatis的使用,该项目正常工作,我能够从数据库中检索一些数据。但是现在我需要第二次缓存结果。我已经在spring中测试了redis是一个嵌入式缓存管理器(缓存抽象:http://static.springsource.org/spring-data/data-redis/docs/current/reference/html/redis.html和liker:http://static.springsource.org/spring/docs/3.1.0.M1/spring-framework-reference/html/cache.html)。我已经实现了所有内容并缓存了一个方法。但!!! 我不能真正理解它是否被缓存。第一次当我标记方法时,redis说,db有更改并保存了..然后我更改了密钥,没有任何改变......我怎么理解,该方法是否被缓存??我会在这里放一些代码让你了解我在做什么。

Spring Context:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:redis="http://www.springframework.org/schema/redis"
       xmlns:context="http://www.springframework.org/schema/context" 
       xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
       xmlns:tx="http://www.springframework.org/schema/tx" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.1.xsd 
       http://www.springframework.org/schema/jdbc 
       http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd 
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
       http://www.springframework.org/schema/redis http://www.springframework.org/schema/redis/spring-redis.xsd
       http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd   
        ">

    <jdbc:embedded-database id="dataSource" type="H2"> 
        <jdbc:script location="file:src/main/java/schema.sql" /> 
        <jdbc:script location="file:src/main/java/test-data.sql" />
    </jdbc:embedded-database>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" /> 
    </bean>

    <tx:annotation-driven />

    <context:component-scan base-package="com.mycompany.mybatisproject.serviceimpl" />

    <!-- Define the SqlSessionFactory --> 
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" value="file:src/main/java/com/mycompany/mybatisproject/persistence/ContactMapper.xml" />
        <property name="typeAliasesPackage" value="com.mycompany.mybatisproject.data" /> 
    </bean>

    <!-- classpath*:com/mycompany/mybatisproject/persistence/*.xml -->

    <!-- Scan for mappers and let them be autowired -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.mycompany.mybatisproject.persistence" />
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>

    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
    p:host-name="localhost" p:port="6379" />

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
    <property name="connectionFactory" ref="jedisConnectionFactory" />
    </bean>

    <cache:annotation-driven />

    <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager" 
        c:template-ref="redisTemplate" />

</beans>

服务的实施:

@Service("contactService")
@Repository
@Transactional
public class ContactServiceImpl implements ContactService {

    private Log log = LogFactory.getLog(ContactServiceImpl.class);

    @Autowired
    private ContactMapper contactMapper;

    @Cacheable("pacan")
    @Transactional(readOnly=true)
    public List<Contact> findAll() {
        List<Contact> contacts = contactMapper.findAll();
        return contacts;
    }
}

ContactMapper.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mycompany.mybatisproject.persistence.ContactMapper">

    <resultMap id="contactResultMap" type="Contact">
        <id property="id" column="ID" />
        <result property="firstName" column="FIRST_NAME" />
        <result property="lastName" column="LAST_NAME" /> 
        <result property="birthDate" column="BIRTH_DATE" />
    </resultMap>

    <select id="findAll" resultMap="contactResultMap">
        SELECT ID, FIRST_NAME, LAST_NAME, BIRTH_DATE 
        FROM CONTACT
    </select>

最后是主要课程:

public class App {

    private static void ListContacts(List<Contact> contacts) {
        System.out.println("");
        System.out.println("Listing contacts without details: ");
        for (Contact contact : contacts) {
            System.out.println(contact);
            System.out.println();
        }
    }

    public static void main( String[] args ) {
        GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
        ctx.load("file:src/main/java/app-context.xml");
        ctx.refresh();

        ContactService contactService = ctx.getBean("contactService", ContactService.class);

        List<Contact> contacts;
        contacts = contactService.findAll(); 
        ListContacts(contacts);
    }    
}

提前致谢。

1 个答案:

答案 0 :(得分:1)

您正在缓存ContactServiceImpl.findAll方法的调用。出于测试目的,您可以在findAll方法中添加System.out.println(“Method invoked”)。如果缓存工作,findAll方法的主体应该只调用一次,下一次调用应该从缓存中读取值(结果),这样你就不会在控制台上看到“方法调用”。

不要使用与3.1.0.RELEASE不同的Spring 3.1.0.M1文档:http://static.springsource.org/spring/docs/3.1.0.RELEASE/spring-framework-reference/html/cache.html