Spring Cache实战
程序员文章站
2022-05-08 09:18:11
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
下一篇: 谈显卡做工好坏的判断