spring boot redis (三,終極整合)

上一章介紹了一些基本的配置和測(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]
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末湃交,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子藤巢,更是在濱河造成了極大的恐慌搞莺,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,378評(píng)論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件掂咒,死亡現(xiàn)場離奇詭異才沧,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)绍刮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,970評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門温圆,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人孩革,你說我怎么就攤上這事岁歉。” “怎么了膝蜈?”我有些...
    開封第一講書人閱讀 168,983評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵锅移,是天一觀的道長。 經(jīng)常有香客問我饱搏,道長非剃,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,938評(píng)論 1 299
  • 正文 為了忘掉前任窍帝,我火速辦了婚禮努潘,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘坤学。我一直安慰自己,他們只是感情好报慕,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,955評(píng)論 6 398
  • 文/花漫 我一把揭開白布深浮。 她就那樣靜靜地躺著,像睡著了一般眠冈。 火紅的嫁衣襯著肌膚如雪飞苇。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,549評(píng)論 1 312
  • 那天蜗顽,我揣著相機(jī)與錄音布卡,去河邊找鬼。 笑死雇盖,一個(gè)胖子當(dāng)著我的面吹牛忿等,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播崔挖,決...
    沈念sama閱讀 41,063評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼贸街,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼庵寞!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起薛匪,我...
    開封第一講書人閱讀 39,991評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤捐川,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后逸尖,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體古沥,經(jīng)...
    沈念sama閱讀 46,522評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,604評(píng)論 3 342
  • 正文 我和宋清朗相戀三年娇跟,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了渐白。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,742評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡逞频,死狀恐怖纯衍,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情苗胀,我是刑警寧澤襟诸,帶...
    沈念sama閱讀 36,413評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站基协,受9級(jí)特大地震影響歌亲,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜澜驮,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,094評(píng)論 3 335
  • 文/蒙蒙 一陷揪、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧杂穷,春花似錦悍缠、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,572評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至廊蜒,卻和暖如春趴拧,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背山叮。 一陣腳步聲響...
    開封第一講書人閱讀 33,671評(píng)論 1 274
  • 我被黑心中介騙來泰國打工著榴, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人屁倔。 一個(gè)月前我還...
    沈念sama閱讀 49,159評(píng)論 3 378
  • 正文 我出身青樓脑又,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子挂谍,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,747評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理叔壤,服務(wù)發(fā)現(xiàn),斷路器口叙,智...
    卡卡羅2017閱讀 134,714評(píng)論 18 139
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,317評(píng)論 25 707
  • 這部分主要是開源Java EE框架方面的內(nèi)容炼绘,包括Hibernate、MyBatis妄田、Spring俺亮、Spring ...
    雜貨鋪老板閱讀 1,392評(píng)論 0 2
  • 老刀有點(diǎn)擔(dān)憂,看了看手表疟呐,清晨5點(diǎn)脚曾。他回到樓門口等著。兩旁狼吞虎咽的饑餓少年圍繞著他启具。他認(rèn)識(shí)其中兩個(gè)本讥,原來在彭蠡家...
    湯圓君花生醬A15003閱讀 220評(píng)論 0 0
  • 企業(yè)
    be58e0597e58閱讀 457評(píng)論 0 0