技术实践第三期|HashTag在Redis集群环境下的使用

简介:欢迎了解友盟+技术干货第三期内容:Redis集群环境如何按照前缀批量删除缓存。希望能对开发者们在实际应用中有所帮助。

一、背景

数据源列表添加缓存支持,types字段可传多值,如app, mini, web等,会构建如下缓存key,

  • application_list:123456:app
  • application_list:123456:mini
  • application_list:123456:web
  • application_list:123456:app,mini
  • application_list:123456:app,web
  • application_list:123456:mini,web
  • application_list:123456:app,mini,web
  • ...

当创建应用,更新应用或删除应用的时候,需要批量删除旧版本缓存。

二、思路

1.按照前缀 `application_list:123456`,查询所有相关的key

2.遍历keys,执行删除

/**
 * 移除缓存
 *
 * @param prefix prefix
 */
public static void deleteByPrefix(String prefix) {
    long start = System.currentTimeMillis();
    Set<String> keys;
    try {
        keys = jedisCluster.keys(CacheKeyUtils.buildCacheKey(prefix, "*"));
        LOGGER.info("cache keys {} with prefix {}", keys, prefix);
        if (keys != null && !keys.isEmpty()) {
            jedisCluster.del(keys.toArray(new String[keys.size()]));
        }
    } catch (Exception e) {
        LOGGER.error("cache deleteByPrefix error, prefix = {}", prefix, e);
        throw new BusinessException(CoreErrorEnum.CACHE_DELETE_ERROR, prefix);
    }
    long end = System.currentTimeMillis();
    LOGGER.info("cache deleteByPrefix success, prefix = {}, cost {} ms", prefix, (end - start));
}
复制代码

三、问题

按照这个写完,执行报错,"JedisCluster only supports KEYS commands with patterns containing hash-tags ( curly-brackets enclosed strings )"

Redis Cluster 采用虚拟槽分区,所有的根据哈希函数映射到 0~16383 整数槽内,计算公式:slot = CRC16(key) % 16384。每个节点负责维护一部分槽以及槽所映射的键值数据,如图所示:

四、方案

使用HashTag生成缓存Key

if (StringUtils.isNotEmpty(platform)) {
    cacheKey = CacheKeyUtils.buildCacheKey(
        CacheKeyUtils.buildHashTag(CacheConstant.APPLICATION_LIST, String.valueOf(userId)), "platform",
        platform);
} else if (types != null && !types.isEmpty()) {
    cacheKey = CacheKeyUtils.buildCacheKey(
        CacheKeyUtils.buildHashTag(CacheConstant.APPLICATION_LIST, String.valueOf(userId)), "types",
        types.stream().sorted().collect(Collectors.joining(",")));
} else {
    cacheKey = CacheKeyUtils.buildCacheKey(
        CacheKeyUtils.buildHashTag(CacheConstant.APPLICATION_LIST, String.valueOf(userId)));
}
复制代码
  • {application_list:123456}:app
  • {application_list:123456}:mini
  • {application_list:123456}:web
  • {application_list:123456}:app,mini
  • {application_list:123456}:app,web
  • {application_list:123456}:mini,web
  • {application_list:123456}:app,mini,web
  • ...

缓存用户下全量的数据源

每次从缓存或者数据库查询当前用户下的所有数据源,按照参数筛选。

原文链接

本文为阿里云原创内容,未经允许不得转载。

おすすめ

転載: juejin.im/post/7047426056675917832