java - MyBatis+Redis caching. Is it possible? -
i have little project check how works. i've implemented usage of mybatis , project works, able retrieve info database. right need result cached 2nd time. i've tested redis embedded cache manager in spring(cache abstraction: http://static.springsource.org/spring-data/data-redis/docs/current/reference/html/redis.html , liker here: http://static.springsource.org/spring/docs/3.1.0.m1/spring-framework-reference/html/cache.html). i've implemented , cached 1 method. but!!! can't understand cached or not. first time when marked method, redis said, there changes db , saved it.. changed key, , nil changed... how understand, method cached or not?? set code here understand i'm doing.
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 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 mappers , allow them 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>
implementation of service:
@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(); homecoming 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 contact </select>
and main class:
public class app { private static void listcontacts(list<contact> contacts) { system.out.println(""); system.out.println("listing contacts without details: "); (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); } }
thanks in advance.
you're caching invocation of contactserviceimpl.findall method. test purpose can add together system.out.println("method invoked") in findall method. if cache works body of findall method should invoked once, next invocations should read value (result) cache shouldn't see "method invoked" on console.
don't utilize spring 3.1.0.m1 documentation different 3.1.0.release: http://static.springsource.org/spring/docs/3.1.0.release/spring-framework-reference/html/cache.html.
java spring caching redis mybatis
No comments:
Post a Comment