gpt4 book ai didi

java - MyBatis+Redis 缓存。可能吗?

转载 作者:可可西里 更新时间:2023-11-01 11:22:36 24 4
gpt4 key购买 nike

我有一个小项目只是为了检查一切是如何工作的。我已经实现了 MyBatis 的使用并且项目正常运行,我能够从数据库中检索一些数据。但是现在我需要第二次缓存结果。我已经在 spring 中将 redis 测试为嵌入式缓存管理器(缓存抽象:http://static.springsource.org/spring-data/data-redis/docs/current/reference/html/redis.html 和此处的类似者:http://static.springsource.org/spring/docs/3.1.0.M1/spring-framework-reference/html/cache.html)。我已经实现了所有内容并缓存了一种方法。但!!!我真的不明白它是否被缓存了。我第一次标记方法时,redis说,db有变化并保存了..但是后来我改变了键,没有任何改变......我怎么理解,方法是否被缓存??我将在此处放置一些代码,以便您了解我在做什么。

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);
}
}

提前致谢。

最佳答案

您正在缓存 ContactServiceImpl.findAll 方法的调用。出于测试目的,您可以在 findAll 方法中添加 System.out.println("Method invoked") 。如果缓存有效,则 findAll 方法的主体应仅调用一次,下一次调用应从缓存中读取值(结果),因此您不应在控制台上看到“方法已调用”。

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

关于java - MyBatis+Redis 缓存。可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14848044/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com