Spring Cache实战
程序员文章站
2022-12-20 08:34:22
Spring Cache实战环境jdk1.8Spring 4.3.9.RELEASE引包spring-contextlombok 1.16.12cache配置
- 环境
jdk1.8
Spring 4.3.9.RELEASE - 引包
spring-context
lombok 1.16.12 - cache配置
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<bean class="com.dong.service.MerchantService"/>
<cache:annotation-driven/>
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
<property name="name" value="merchant"/>
</bean>
</set>
</property>
</bean>
</beans>
- 基本代码
package com.dong.service;
import com.dong.entity.Merchant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.UUID;
/**
* 商户服务
*
* @author dongzhihua
* @date 2020/11/9 11:01
*/
@Service
@Slf4j
public class MerchantService {
@Cacheable(value = "merchant", key = "'merchant-'+#p0")
public Merchant load(String code) {
log.info("查询数据库:{}", code);
Merchant merchant = new Merchant();
merchant.setCode(code);
merchant.setName(UUID.randomUUID().toString());
return merchant;
}
@Cacheable(value = "merchant", key = "'merchant-'+#merchant.code")
public Merchant load(Merchant merchant) {
log.info("查询数据库:{}", merchant);
merchant.setName(UUID.randomUUID().toString());
return merchant;
}
@CacheEvict(value = "merchant", key = "'merchant-'+#merchant.code")
public void update(Merchant merchant) {
log.info("更新商户:{}", merchant);
}
}
package com.dong.entity;
import lombok.Data;
import java.util.Date;
/**
* 商户实体
*
* @author dongzhihua
* @date 2020/11/9 11:01
*/
@Data
public class Merchant {
private String code;
private String name;
}
- 测试代码
package com.dong.service;
import com.dong.entity.Merchant;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
@ContextConfiguration("classpath:spring-cache-test.xml")
@RunWith(SpringJUnit4ClassRunner.class)
@Slf4j
public class MerchantServiceTest {
@Resource
MerchantService merchantService;
@Test
public void load() {
for (int i = 0; i < 5; i++) {
Merchant mer = merchantService.load("110");
log.info("查到结果{}:{}", i, mer);
if (i % 4 == 3) {
merchantService.update(mer);
}
}
}
@Test
public void testLoad() {
Merchant mer = new Merchant();
mer.setCode("110");
for (int i = 0; i < 5; i++) {
Merchant result = merchantService.load(mer);
log.info("查到结果{}:{}", i, result);
if (i % 5 == 2) {
merchantService.update(mer);
}
}
}
}
- 结果
11:27:58.800 [main] INFO com.dong.service.MerchantService - 查询数据库:Merchant(code=110, name=null)
11:27:59.133 [main] INFO com.dong.service.MerchantServiceTest - 查到结果0:Merchant(code=110, name=b19c11ed-d63c-463b-83df-1d25d254acf1)
11:27:59.134 [main] INFO com.dong.service.MerchantServiceTest - 查到结果1:Merchant(code=110, name=b19c11ed-d63c-463b-83df-1d25d254acf1)
11:27:59.134 [main] INFO com.dong.service.MerchantServiceTest - 查到结果2:Merchant(code=110, name=b19c11ed-d63c-463b-83df-1d25d254acf1)
11:27:59.134 [main] INFO com.dong.service.MerchantService - 更新商户:Merchant(code=110, name=b19c11ed-d63c-463b-83df-1d25d254acf1)
11:27:59.135 [main] INFO com.dong.service.MerchantService - 查询数据库:Merchant(code=110, name=b19c11ed-d63c-463b-83df-1d25d254acf1)
11:27:59.135 [main] INFO com.dong.service.MerchantServiceTest - 查到结果3:Merchant(code=110, name=6d6733ac-cdfb-4ecf-83fb-5f575457750b)
11:27:59.135 [main] INFO com.dong.service.MerchantServiceTest - 查到结果4:Merchant(code=110, name=6d6733ac-cdfb-4ecf-83fb-5f575457750b)
11:27:59.139 [main] INFO com.dong.service.MerchantServiceTest - 查到结果0:Merchant(code=110, name=6d6733ac-cdfb-4ecf-83fb-5f575457750b)
11:27:59.139 [main] INFO com.dong.service.MerchantServiceTest - 查到结果1:Merchant(code=110, name=6d6733ac-cdfb-4ecf-83fb-5f575457750b)
11:27:59.139 [main] INFO com.dong.service.MerchantServiceTest - 查到结果2:Merchant(code=110, name=6d6733ac-cdfb-4ecf-83fb-5f575457750b)
11:27:59.139 [main] INFO com.dong.service.MerchantServiceTest - 查到结果3:Merchant(code=110, name=6d6733ac-cdfb-4ecf-83fb-5f575457750b)
11:27:59.139 [main] INFO com.dong.service.MerchantService - 更新商户:Merchant(code=110, name=6d6733ac-cdfb-4ecf-83fb-5f575457750b)
11:27:59.139 [main] INFO com.dong.service.MerchantService - 查询数据库:110
11:27:59.139 [main] INFO com.dong.service.MerchantServiceTest - 查到结果4:Merchant(code=110, name=7948ec44-4470-4052-b450-342b559fcb6b)
END
本文地址:https://blog.csdn.net/o544033135/article/details/109573437
推荐阅读
-
使用Spring Security控制会话的方法
-
Spring Boot报错:No session repository could be auto-configured, check your configuration的解决方法
-
详解Spring极速集成注解redis实录
-
Spring Boot + Vue 前后端分离开发之前端网络请求封装与配置
-
spring整合atomikos实现分布式事务的方法示例
-
mobilenetv3-tensorflow实战项目准备和代码调试
-
spring IOC容器 Bean管理(基于注解方式)
-
Spring的配置,XML提示的配置,Bean的相关配置
-
自己动手在Spring-Boot上加强国际化功能的示例
-
浅析Spring Boot中的spring-boot-load模块