springboot_cache

使用注解

@EnableCaching(proxyTargetClass = true) //cglib做为代理
@Cacheable

#参数名 或者 #p参数index    "#id"  或者 "#p0 "

@Cacheable(value = { "sampleCache" },key="targetClass.getName()+'.'+methodName+'.'+#id")//或者key="#id"
public Optional<UserJwtshiro> findById(Long id) {
	TUserJwtshiro tUserJwtshiro = tUserJwtshiroSubMapper.selectByPrimaryKey(id);
	return Optional.ofNullable(tUserJwtshiro).map(userConverter());
}

 注册使用的缓存,springboot不用配制也可以的

import org.springframework.cache.CacheManager;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Arrays;
@Configuration
public class CacheManagerConfig {
	@Bean
	public CacheManager cacheManager() {
		SimpleCacheManager cacheManager = new SimpleCacheManager();
		cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("sampleCache"),new ConcurrentMapCache("sampleCache2")));
		return cacheManager;
	}
}

与shiro结合时的一些坑

当与shiro结合时有时会发现@Cacheable注解失效
解决方式为所有shiro框架用到的XxxxService.java类
在引入时要这样,必须要有一个@Lazy注解才会好,原因为shiro框架也有一个内置的cache框架
@Autowired
@Lazy

private AuthService authService;

shiro里的AuthRealm.java 或自定义的ShiroFilter等
凡是shiro用到的业务类都要 多加一个@Lazy

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值