alibaba / jetcache

JetCache is a Java cache framework.
Apache License 2.0
5.15k stars 1.06k forks source link

存入的value为啥有一些乱码 #934

Open starslink opened 1 month ago

starslink commented 1 month ago
QuickConfig qc = QuickConfig.newBuilder(SHOP_ITEM_LACK_LOCAL)
      // 24 小时过期
      .expire(Duration.ofHours(24))
      .localExpire(Duration.ofHours(24))
      .localLimit(Integer.MAX_VALUE)
      .cacheType(CacheType.BOTH)
      .valueEncoder(Fastjson2ValueEncoder.INSTANCE)
      .valueDecoder(Fastjson2ValueDecoder.INSTANCE)
      // 本地通知过期
      .syncLocal(true)
      .build();
    shopItemLackCache = cacheManager.getOrCreateCache(qc);
@PostMapping("/post")
  public String post(@RequestBody A a) {
    shopItemLackCache.put("AX", a);
    return "ok";
  }

  static class A {

    private String x;

    private String y;

    public A() {
    }

    public A(String x, String y) {
      this.x = x;
      this.y = y;
    }

    public String getX() {
      return x;
    }

    public void setX(String x) {
      this.x = x;
    }

    public String getY() {
      return y;
    }

    public void setY(String y) {
      this.y = y;
    }
  }

一个简单的POST请求:

image
areyouok commented 1 month ago

有额外的元信息

starslink commented 1 month ago
[
    {
        "@type": "com.xxxx.oms.domain.base.DataDictItem",
        "createdTime": "2023-04-11T09:13:19",
        "dataDictId": 16274017971061760,
        "dataDictItemCode": "test",
        "dataDictItemExtendName": "红灯打开",
        "dataDictItemExtendName1": "red-on",
        "dataDictItemExtendName2": "1",
        "dataDictItemId": 16274024014480384,
        "dataDictItemName": "扫描提醒哦",
        "modifiedTime": "2024-02-26T15:04:13",
        "system": false
    },
    {
        "@type": "com.xxxx.oms.domain.base.DataDictItem",
        "createdTime": "2023-04-12T15:02:50",
        "dataDictId": 16274017971061760,
        "dataDictItemCode": "test1",
        "dataDictItemId": 16275783180272640,
        "dataDictItemName": "测试1",
        "modifiedTime": "2023-04-12T15:02:50",
        "system": false
    }
]

为啥不是这样显示呢

areyouok commented 1 month ago

你不能只顾自己需要的数据,框架也需要额外的数据

starslink commented 1 month ago

我知道您的意思,我的意思是CacheValueHolder能不能也格式化存储

例如这样

{
    "type": "@com.alicp.jetcache.CacheValueHolder",
    "expireTime": 12323434,
    "accessTime": 123123213,
    "value": {
        "type": {
            "@type": "com.xxxx.oms.domain.base.DataDictItem",
            "createdTime": "2023-04-12T15:02:50",
            "dataDictId": 16274017971061760,
            "dataDictItemCode": "test1",
            "dataDictItemId": 16275783180272640,
            "dataDictItemName": "测试1",
            "modifiedTime": "2023-04-12T15:02:50",
            "system": false
        }
    }
}
areyouok commented 1 month ago

不光是CacheValueHolder,还有别的信息,首先最重要的信息是编码方式(比如描述这条数据是json编码方式),放在最前面。

这不可能改了,改了就兼容,如果你觉得有必要,就得自己改。