Redis 中 hash 存储和获取

你知道的越多,你不知道的越多
点赞再看,养成习惯
如果您有疑问或者见解,欢迎指教:
企鹅:869192208

前言

最近做了个需求,需要用到 Redis 中 hash 存储和获取,记录一下使用方法。

import com.alibaba.fastjson.JSON;
import com.minstone.appr.apply.common.utils.RunMerger;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <h2>springBoot项目启动完成后执行<h2>
 *
 * @author 心猿码意
 * @create 2023-06-30 16:47
 */
@Component
public class AppInit implements ApplicationRunner {
    @Resource
    private RedisTemplate redisTemplate;


    @Override
    public void run(ApplicationArguments args) throws Exception {
			//异步执行
        RunMerger.SM.asyncHandle(() -> {
            extracted("全国");
        });
    }

    private void extracted(String parentId) {
        Map<String, List<AreaTest>> setMap = new HashMap<>();
        String cacheSetKey = "area_xxx_cache_key";
        setMap.put("100shen", AreaTest.test("shen"));
        setMap.put("200shi", AreaTest.test("shi"));
        setMap.put("300qu", AreaTest.test("qu"));
        HashOperations hashOperations = redisTemplate.opsForHash();
        hashOperations.putAll(cacheSetKey, setMap);
        //设置过期时间180天
        hashOperations.getOperations().expire(RedisKeyConstant.areaClqhCacheKey,180, TimeUnit.DAYS);
        //模糊获取数据
        Cursor scan = hashOperations.scan(cacheSetKey, ScanOptions.scanOptions().match("200*")
                .build()).forEachRemaining(entry ->{
                log.info("获取到的数据:{}", JSON.toJSONString(entry));
            });
        
        Object o = hashOperations.get(cacheSetKey, "200*");
        System.out.println(JSON.toJSONString(o));
        List<AreaTest> areaTests = JSONObject.parseArray(JSON.toJSONString(o), AreaTest.class);
        System.out.println(JSON.toJSONString(areaTests));
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class AreaTest implements Serializable {
        private static final long serialVersionUID = -6200424567267339450L;
        private String areaCode;
        private String areaName;


        public static List<AreaTest> test(String value) {
            List<AreaTest> a = new ArrayList<>(2);
            for (int i = 0; i < 2; i++) {
                a.add(new AreaTest(i+value,"value"+i));
            }
            return a;
        }
    }
}

如果需要往一个已经存在的 Redis 的 hash 中插入新的一条数据,可以使用以下方法:

// xdeptVos为需要缓存的数据
hashOperations.put(cacheSetKey, "new_data", xdeptVos);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值