2013-02-13 3 views
0

У меня есть небольшой проект, чтобы проверить, как все работает. Я реализовал использование MyBatis, и проект просто работает, я смог получить некоторые данные из базы данных. Но сейчас мне нужно, чтобы результат был кэширован во второй раз. Я уже тестировал 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 и сохранены. Но потом я изменил ключ, и ничего не изменилось ... Как я понял, что метод был кеширован или нет ?? Я приведу здесь код, чтобы понять, что я делаю.MyBatis + Redis кэширование. Является ли это возможным?

Spring Контекст:

<?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

Вы кэшируете вызов метода ContactServiceImpl.findAll. Для целей тестирования вы можете добавить System.out.println («Метод, вызванный») в метод findAll. Если кеш работает, тело метода findAll должно вызываться только один раз, следующие вызовы должны считывать значение (результат) из кеша, поэтому вы не должны видеть «метод, вызванный» на консоли.

Не используйте весеннюю 3.1.0.M1 документацию, отличную от 3.1.0.RELEASE: http://static.springsource.org/spring/docs/3.1.0.RELEASE/spring-framework-reference/html/cache.html.

+0

Большое спасибо за ваш ответ. Итак, как я понял из ссылки, я должен объявить в xml метод, который должен быть кэширован? – PAcan

+0

Нет, нет. Достаточно отметить метод с помощью @Cacheable, добавить в xml: и определить bean cacheManager. Сначала вы можете попробовать org.springframework.cache.support.SimpleCacheManager, и если он работает, переключитесь на redis. – ragnor