NutzCN Logo
问答 Spring @Cacheable 部分方法不起作用.
发布于 2904天前 作者 TuWei1992 11503 次浏览 复制 上一个帖子 下一个帖子
标签: spring

用的 Spring + Redis 做缓存, 同一个 Service 中, 其他的方法都可以被缓存, 只有某一个方法不可以, 有谁知道为啥么?

8 回复

这个方法的命名,参数类型,返回值类型有啥特别没

这是不起作用的那个方法,

/**
     * 获取用户最新的健康档案
     *
     * @param userId 用户Id
     * @return
     */
    @Cacheable(key = "'userLatestHealthRecord'.concat(#userId)")
    public UserHealthRecord userLatestHealthRecord(String userId) {
        UserHealthRecord record = this.fetch(Cnd.where("userId", "=", userId).desc("createDate"));
        return record;
    }

起作用的如下

/**
     * 获取该 BMI 分数超过全国用户的百分比
     *
     * @param score 用户的 bmi 得分
     * @return 返回该分数超过全国多少人/%
     */
    @Cacheable(key = "'getBmiScorePercent'.concat(#score)")
    public int getBmiScorePercent(int score) {
        return getScorePercent(score, UserHealthScore.SCORE_TYPE_BMI);
    }

Log

2017-01-10 17:14:30.940 TRACE 3940 --- [nio-8080-exec-1] o.s.cache.interceptor.CacheInterceptor   : Computed cache key 'userLatestHealthRecordp7pfmo8tm8jptrv3kvsgsku16r' for operation Builder[public com.a.app.model.UserHealthRecord com.a.app.db.service.UserHealthRecordService.userLatestHealthRecord(java.lang.String)] caches=[app_user_health] | key=''userLatestHealthRecord'.concat(#userId)' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'
2017-01-10 17:14:30.941 TRACE 3940 --- [nio-8080-exec-1] o.s.cache.interceptor.CacheInterceptor   : No cache entry for key 'userLatestHealthRecordp7pfmo8tm8jptrv3kvsgsku16r' in cache(s) [app_user_health]
2017-01-10 17:14:30.941 TRACE 3940 --- [nio-8080-exec-1] o.s.cache.interceptor.CacheInterceptor   : Computed cache key 'userLatestHealthRecordp7pfmo8tm8jptrv3kvsgsku16r' for operation Builder[public com.a.app.model.UserHealthRecord com.a.app.db.service.UserHealthRecordService.userLatestHealthRecord(java.lang.String)] caches=[app_user_health] | key=''userLatestHealthRecord'.concat(#userId)' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'
2017-01-10 17:14:30.992 DEBUG 3940 --- [nio-8080-exec-1] o.nutz.dao.impl.sql.run.NutDaoExecutor   : SELECT * FROM user_health_record  WHERE user_id=? ORDER BY create_date DESC   LIMIT 0, 1 
    |                          1 |
    |----------------------------|
    | p7pfmo8tm8jptrv3kvsgsku16r |
  For example:> "SELECT * FROM user_health_record  WHERE user_id='p7pfmo8tm8jptrv3kvsgsku16r' ORDER BY create_date DESC   LIMIT 0, 1 "

去掉临时变量试试,直接return

@wendal 找到点头绪, 将返回类UserHealthRecord 改成其他的就可以, 但是我这个UserHealthRecord也实现序列化接口了啊, 搞不懂

难道是属性太多的事?

@wendal 😢知道为啥了, 因为查出的数据为 null.....

添加回复
请先登陆
回到顶部