Cacheable 類轉換異常

antor發表於2024-08-07

文章遷移
異常情況

java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.xxx.Class

使用@Cacheable 儲存快取 至 redis 中時,呼叫儲存方法會報 類轉換異常
程式碼

class cacheService
@Cacheable(value = "redisCache1103",key = "T(com.xxx.common.constant.CacheKeyPrefixConst).USER_INFO+#key")
    public User saveUserPersonal(User user, String key){
        return user;
    }
class login 
 cacheService.saveUserPersonal(result, result.getAccessToken());

@Cacheable 當快取中沒有資料時,會儲存快取,當快取中存在此資料時,會獲取當前key 的 快取資料,所以會有類轉換異常,返回資料型別 為 map 即可。
或者 使用 @CachePut 註解,每次都會執行。
或者 簡單資料型別。不要使用 複雜的型別
key 值不能相同

相關文章