Redis:创建Bigkey

  • 修改maxmemory-policy淘汰策略
  • curl localhost:34567/large/init?size=10
  • 查看大key扫描结果
vim /etc/redis/redis.conf

# 不做任何的清理工作,在redis的内存超过限制之后,所有的写入操作都会返回错误;但是读操作都能正常的进行
maxmemory-policy noeviction

模拟创建大key数据

package icu.kevin.cache.avalanche;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

@Slf4j
@RequestMapping("large")
@RestController
public class LargeKeyController {


    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("init")
    @Async
    public void initData(@RequestParam (name = "size", defaultValue = "5000") Integer size){
        log.info("starting init size........." + size);
        for (int i = 0; i < size; i++) {
            log.info("starting init data........." + i);
//            redisTemplate.opsForValue().set("large_key_" + i, generateTestData(10 * 1024));

            redisTemplate.opsForValue().set("large_key_" + i, generateTestData(10* 1024));
        }
    }


    @GetMapping("get")
    public String get(@RequestParam (name = "index", defaultValue = "0") Integer index){
//        return redisTemplate.opsForSet().randomMember("large_key_" + index).toString();
        return redisTemplate.opsForValue().get("large_key_" + index).toString();
    }


    public static String generateTestData(int sizeInKb) {
        int length = sizeInKb * 1024;
        int chunkSize = 10;
        int chunks = length / chunkSize;
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < chunks; i++) {
            String chunk = generateRandomString(chunkSize);
            sb.append(chunk);
        }
        return sb.toString();
    }

    public static String generateRandomString(int length) {
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        StringBuilder sb = new StringBuilder(length);
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(characters.length());
            sb.append(characters.charAt(index));
        }
        return sb.toString();
    }

}


查看扫描结果

root@DESKTOP-0JS7U4E:~# redis-cli -h 127.0.0.1 -p 16379 --bigkeys

# Scanning the entire keyspace to find biggest keys as well as
# average sizes per key type.  You can use -i 0.1 to sleep 0.1 sec
# per 100 SCAN commands (not usually needed).

[00.00%] Biggest string found so far 'large_key_2' with 10485762 bytes

-------- summary -------

Sampled 10 keys in the keyspace!
Total key length in bytes is 110 (avg len 11.00)

Biggest string found 'large_key_2' has 10485762 bytes

0 lists with 0 items (00.00% of keys, avg size 0.00)
0 hashs with 0 fields (00.00% of keys, avg size 0.00)
10 strings with 104857620 bytes (100.00% of keys, avg size 10485762.00)
0 streams with 0 entries (00.00% of keys, avg size 0.00)
0 sets with 0 members (00.00% of keys, avg size 0.00)
0 zsets with 0 members (00.00% of keys, avg size 0.00)
root@DESKTOP-0JS7U4E:~#
root@DESKTOP-0JS7U4E:~#
root@DESKTOP-0JS7U4E:~#

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jc0803kevin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值