java—如何使用Redison客户端获取缓存值

yruzcnhs  于 2021-06-09  发布在  Redis
关注(0)|答案(1)|浏览(354)

我想使用Redison客户端获取cached(@cachable)值,但是如果我在Redison客户端(getbucket(“fruit::1”,stringcodec.instance))中使用任何编解码器,它会返回奇怪的数据,并且除非我使用编解码器,否则它会抛出错误。
我使用了下面的代码进行缓存

@Cacheable(value = "fruits", key = "#id")
    public Fruit getFruitById(int id) {
    // get fruit by id
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Fruit> query = builder.createQuery(Fruit.class);
    Root<Fruit> root = query.from(Fruit.class);
    query.select(root);
    query.where(builder.equal(root.get("id"), id));
    TypedQuery<Fruit> fruitQuery = em.createQuery(query);
    return fruitQuery.getSingleResult();
}

当我使用编解码器获取缓存数据时

RBucket<String> bucket = client.getBucket("fruits::1",
            StringCodec.INSTANCE);
    String fruit = bucket.get();

它返回以下奇怪的数据
��srcom.home.redis.fruit文件��.ܵo*riidipricelnametljava/lang/string;xp、tpomegranite
再贴现配置

@Bean
public RedisCacheConfiguration cacheConfiguration() {
    RedisCacheConfiguration cacheConfig = RedisCacheConfiguration
            .defaultCacheConfig().entryTtl(Duration.ofSeconds(600))
            .disableCachingNullValues();
    return cacheConfig;
}

@Bean
public RedisCacheManager cacheManager() {
    RedisCacheManager rcm = RedisCacheManager
            .builder(this.getRedissonStoreFactory())
            .cacheDefaults(cacheConfiguration()).transactionAware().build();
    return rcm;
}

@Bean
@Primary
public RedisProperties redisProperties() {
    return new RedisProperties();
}

@Bean
public RedissonConnectionFactory getRedissonStoreFactory() {
    return new RedissonConnectionFactory(getConfig());
}

@Bean
public RedissonNode getNode() {
    RedissonNodeConfig nodeConfig = new RedissonNodeConfig(getConfig());
    nodeConfig.setExecutorServiceWorkers(
            Collections.singletonMap("ensimp", 1));
    RedissonNode node = RedissonNode.create(nodeConfig);
    node.start();
    return node;
}

@Bean
public Config getConfig() 
{
    Config config = new Config();
    RedisProperties properties = redisProperties();
    config.useSingleServer().setAddress(
            "redis://" + properties.getHost() + ":" + properties.getPort());
    return config;
}

redisson.json文件

{
  "singleServerConfig":{
      "idleConnectionTimeout":500,
      "connectTimeout":1000,
      "timeout":3000,
      "retryAttempts":3,
      "retryInterval":1500,
      "password":null,
      "subscriptionsPerConnection":5,
      "clientName":null,
      "address": "redis://127.0.0.1:6379",
      "subscriptionConnectionMinimumIdleSize":0,
      "subscriptionConnectionPoolSize":1,
	  "connectionMinimumIdleSize":0,
	  "connectionPoolSize":20,
	  "database":0,
	  "dnsMonitoringInterval":5000
	},
	"threads":16,
    "nettyThreads":32,
    "codec":{
       "class":"org.redisson.codec.FstCodec"
     },
     "transportMode":"NIO"
}

我也用过fst编解码器,但得到了同样奇怪的数据。我想要正确解码的数据如果有人能帮我找到正确的代码那就太好了。

2w2cym1i

2w2cym1i1#

您需要使用rmapcache数据来获取数据,而不是rbucket。

client.getMapCache("fruits::1", StringCodec.INSTANCE);

相关问题