惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Forbes - Security
Forbes - Security
GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
SegmentFault 最新的问题
Y
Y Combinator Blog
Recorded Future
Recorded Future
博客园 - Franky
I
InfoQ
T
The Blog of Author Tim Ferriss
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
Cyberwarzone
Cyberwarzone
The Register - Security
The Register - Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
雷峰网
雷峰网
P
Palo Alto Networks Blog
G
GRAHAM CLULEY
Cloudbric
Cloudbric
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
MongoDB | Blog
MongoDB | Blog
F
Full Disclosure
Google DeepMind News
Google DeepMind News
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
Check Point Blog
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
T
Threat Research - Cisco Blogs
U
Unit 42
N
Netflix TechBlog - Medium
The Cloudflare Blog
Spread Privacy
Spread Privacy
Microsoft Azure Blog
Microsoft Azure Blog
美团技术团队
T
Troy Hunt's Blog
Engineering at Meta
Engineering at Meta
H
Heimdal Security Blog
TaoSecurity Blog
TaoSecurity Blog
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tenable Blog
B
Blog
S
Securelist
H
Hacker News: Front Page
Google Online Security Blog
Google Online Security Blog
G
Google Developers Blog

博客园 - 雨V幕

使用chromedp 来做人工模拟操作爬取数据方法 使用rabbitmq 进行任务调度 使用trace进行排查网络瓶颈 使用vscode 调试 Python 使用power shell 拆分 csv文件 将大文件拆分成小文件。 使用postman 添加预处理验签。 go 使用pprof 进行问题排查 Mysql无主键删除重复数据的快速方法 解决mysql 事务死锁的方法 go在处理批量下载时候出现fatal error: runtime: out of memory AnalyticDB 创建db go 序列化反序列化之后时区信息丢失 clickhouse 进行建表期间的一些优化 kraots2.0 在windows 环境搭建开发环境 Sql Server使用函数获取拼音码 关于async 和await关键字 使用kubespray 一键部署 containerd 的安装和熟悉 VMware 配置双网卡实现上网和固定ip
遍历redis按照前缀给未设置过期时间的数据添加过期时间
雨V幕 · 2025-12-23 · via 博客园 - 雨V幕
package main

import (
    "context"
    "github.com/redis/go-redis/v9"
    "log"
    "time"
)

var (
    redisAddr     = "addr"
    redisPassword = "pwd"
    redisDB       = 0

    // ⚠️ 只清这些前缀(非常重要)
    patterns = []string{
        //"PLAT:USER:*",
        "PLAT:USER:NEW:CHANNEL:INFO:*",
        //"PLAT:AD:*",
    }

    // 给历史 Key 补的 TTL
    expireSeconds = int64(7 * 24) // 7 天

    // SCAN 每批数量(别太大)
    scanCount = int64(500)

    // 每处理一批 sleep,防止打爆 Redis
    batchSleep = 50 * time.Millisecond
)

func main() {
    ctx := context.Background()

    rdb := redis.NewClient(&redis.Options{
        Addr:     redisAddr,
        Password: redisPassword,
        DB:       redisDB,
    })

    defer rdb.Close()

    for _, pattern := range patterns {
        log.Printf("🚀 start cleaning pattern: %s\n", pattern)
        if err := cleanByPattern(ctx, rdb, pattern); err != nil {
            log.Fatalf("❌ clean failed: %v", err)
        }
    }

    log.Println("✅ all patterns finished")
}

func cleanByPattern(ctx context.Context, rdb *redis.Client, pattern string) error {
    var cursor uint64
    var total int64

    for {
        keys, nextCursor, err := rdb.Scan(ctx, cursor, pattern, scanCount).Result()
        if err != nil {
            return err
        }

        for _, key := range keys {
            ttl, err := rdb.TTL(ctx, key).Result()
            if err != nil {
                log.Printf("⚠️ TTL error key=%s err=%v\n", key, err)
                continue
            }

            // 只处理「无过期时间」的 key
            if ttl == -1 {
                ok, err := rdb.Expire(ctx, key, time.Duration(expireSeconds)*time.Hour).Result()
                if err != nil || !ok {
                    log.Printf("⚠️ expire failed key=%s err=%v\n", key, err)
                    continue
                }
                total++
            }
        }

        log.Printf("pattern=%s cursor=%d processed=%d\n", pattern, nextCursor, total)

        cursor = nextCursor
        if cursor == 0 {
            break
        }

        time.Sleep(batchSleep)
    }

    log.Printf("🎯 pattern=%s done, total expired=%d\n", pattern, total)
    return nil
}