首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Spring boot应用程序中用Java为Redis Cache创建hardCode密钥?

如何在Spring boot应用程序中用Java为Redis Cache创建hardCode密钥?
EN

Stack Overflow用户
提问于 2020-04-09 20:23:39
回答 2查看 578关注 0票数 1

我正在尝试使用单个redis缓存来存储两个GET服务的响应-一个是POST,一个是GET。对于GET服务,没有请求参数可以用来在Redis中存储Webservice的响应,当我尝试用硬编码的Key存储它时,它给出了下面的错误。

代码语言:javascript
复制
null key returned for cache operation (maybe you are using named params on classes without debug info?) Builder[public java.util.Map com.getResponse() throws com.adapter.framework.assetserviceadaptor.exception.ServiceExceptions] caches=[redis-cache] | key='#AssetCache' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'

这就是我尝试存储GET服务响应的方式

代码语言:javascript
复制
@Cacheable(value="redis-cache",key ="#AssetCache")
public Map<String, AssetDetailBO> getAssetsResponse() throws COAssetServiceExceptions {
    try {
        log.info("---- CO_STAGE=[ Caching Service ] ---- ");
        return mergingBrokerageAndMutulFund(assetServiceImpl.getAssetResponse());
    } catch (ServiceExceptions e) {
        log.info("---- CO_STAGE=[ Exception while Calling Assets Adapter For COT ] ---- " 
        + e.getErrorMessage());
        throw e;
    }
}

这就是我存储POST服务响应的方式,它工作得很好。

代码语言:javascript
复制
@Cacheable(value="redis-cache",key ="#customerId")
public CustomerDTO retriveCustomerdetails( String customerId, String modelId, 
  String requestId) throws COException {
CustomerInfo csDTO;
try {
    csDTO = csDTOAdapterImpl.
    getCustomerDetails(customerId);
} catch (Exception e) {
    e.printStackTrace();
}
return csDTO;
}
EN

回答 2

Stack Overflow用户

发布于 2020-04-12 22:33:18

您可以使用不带值的@Cacheable和其他属性,如@Cacheable("all-students")

示例:

代码语言:javascript
复制
@GetMapping("/student/{id}")
@Cacheable(value = "post-single", key = "#id")
public Student findStudentById(@PathVariable String id) {
    System.out.println("Searching by ID  : " + id);
    return studentService.getStudentByID(id);
}

@GetMapping("/students")
@Cacheable("all-students")
public List<Student> getAllStudents() {

    List<Student> _ = new ArrayList<>();
    _.add(studentService.getStudentByID("1"));
    return _;
}
票数 0
EN

Stack Overflow用户

发布于 2020-04-14 21:40:52

设置不带#的常量关键点,如下所示:

代码语言:javascript
复制
@Cacheable(value="redis-cache",key ="'AssetCache'")
public Map<String, AssetDetailBO> getAssetsResponse() throws COAssetServiceExceptions {
    try {
        log.info("---- CO_STAGE=[ Caching Service ] ---- ");
        return mergingBrokerageAndMutulFund(assetServiceImpl.getAssetResponse());
    } catch (ServiceExceptions e) {
        log.info("---- CO_STAGE=[ Exception while Calling Assets Adapter For COT ] ---- " 
        + e.getErrorMessage());
        throw e;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61121093

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档