上一章介紹了一些基本的配置和測(cè)試荠卷,這一章就開始實(shí)際操作了。
開始前的糾結(jié)
在想緩存到底放在哪一層烛愧,之前測(cè)試的時(shí)候是放在controller上面的油宜,但是根據(jù)自己的代碼來看 有些是返回model或者M(jìn)odelAndView 的,緩存里面存的就是你返回的東西屑彻,居然報(bào)錯(cuò)验庙,說不能轉(zhuǎn)換成json,然后放在底層的dao 還可能被其他業(yè)務(wù)調(diào)用社牲,使用緩存就不好了粪薛。service針對(duì)具體業(yè)務(wù),控制到這個(gè)級(jí)別就行了搏恤,最后就決定放在service層了违寿。
單個(gè)對(duì)象的查詢
@Cacheable(value = "company")
public Company findById(Long id) {
return companyDao.findOne(id);
}
單個(gè)對(duì)象保存修改
@CachePut(value = "company", key = "#root.caches[0].name + ':' + #company.id")
public Company save(Company company){
return companyDao.save(company);
}
單個(gè)對(duì)象的刪除(list刪除也是調(diào)用單個(gè)對(duì)象刪除)
@CacheEvict(value = "company")
public void delete( long id) {
companyDao.delete(id);
}
測(cè)試結(jié)果
21.png
未解決的問題
關(guān)于list分頁的緩存沒有解決,有給力的朋友幫忙解決下么
@Cacheable(value = "companys")
public Page<Company> getCompanyPageByName(String name,Integer page,Integer size ) {
Pageable pageable = new PageRequest(page,size,SortBuilder.generateSort("createTime desc","id asc"));
if(StringKit.isEmpty(name)){
return companyDao.findAll(pageable);
}else{
return companyDao.findByNameLike("%"+name+"%",pageable);
}
}
錯(cuò)誤信息
2017-12-26 15:34:08.853 ERROR 15840 --- [io-8088-exec-13] o.s.boot.context.web.ErrorPageFilter : Forwarding to error page from request [/backend/system/company/list] due to exception [Could not read JSON: No suitable constructor found for type [simple type, class org.springframework.data.domain.PageImpl]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?)
at [Source: [B@503b4614; line: 1, column: 46]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class org.springframework.data.domain.PageImpl]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?)
at [Source: [B@503b4614; line: 1, column: 46]]
org.springframework.data.redis.serializer.SerializationException: Could not read JSON: No suitable constructor found for type [simple type, class org.springframework.data.domain.PageImpl]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?)
at [Source: [B@503b4614; line: 1, column: 46]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class org.springframework.data.domain.PageImpl]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?)
at [Source: [B@503b4614; line: 1, column: 46]
at org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer.deserialize(Jackson2JsonRedisSerializer.java:73) ~[spring-data-redis-1.6.4.RELEASE.jar:na]
at org.springframework.data.redis.cache.RedisCache$CacheValueAccessor.deserializeIfNecessary(RedisCache.java:378) ~[spring-data-redis-1.6.4.RELEASE.jar:na]
at org.springframework.data.redis.cache.RedisCache.get(RedisCache.java:144) ~[spring-data-redis-1.6.4.RELEASE.jar:na]
at org.springframework.data.redis.cache.RedisCache.get(RedisCache.java:94) ~[spring-data-redis-1.6.4.RELEASE.jar:na]
at org.springframework.cache.interceptor.AbstractCacheInvoker.doGet(AbstractCacheInvoker.java:68) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.cache.interceptor.CacheAspectSupport.findInCaches(CacheAspectSupport.java:469) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.cache.interceptor.CacheAspectSupport.findCachedItem(CacheAspectSupport.java:435) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:336) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:302) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at com.daiwei.project.backend.system.service.CompanyService$$EnhancerBySpringCGLIB$$998f6d25.getCompanyPageByName(<generated>) ~[classes/:0.0.1-SNAPSHOT]
at com.daiwei.project.backend.system.controller.CompanyController.list(CompanyController.java:129) ~[classes/:0.0.1-SNAPSHOT]
at com.daiwei.project.backend.system.controller.CompanyController$$FastClassBySpringCGLIB$$ae161425.invoke(<generated>) ~[classes/:0.0.1-SNAPSHOT]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at com.daiwei.project.backend.system.controller.CompanyController$$EnhancerBySpringCGLIB$$5f416f1.list(<generated>) ~[classes/:0.0.1-SNAPSHOT]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0]
at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0]