# Clear2Cache **Repository Path**: wittz/Clear2Cache ## Basic Information - **Project Name**: Clear2Cache - **Description**: Go 项目清理 J2Cache 缓存 - **Primary Language**: Go - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 0 - **Created**: 2023-03-01 - **Last Updated**: 2023-03-08 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Clear2Cache 使用 Go 清理 J2Cache 缓存 ## 背景 起因是社区用 Go 实现了一部分服务,牵扯到与使用 J2Cache 的老的 Java 项目进行缓存同步,直接清理 Redis 缓存好办,难就难在要清理 J2Cache L1 缓存,看了 J2Cache 的项目代码,发现可以通过发送一条广播的形式实现清理 J2Cache L1 缓存的目的,说干就干就有了此项目 ```java /** * * * 当接收到订阅频道获得的消息时触发此方法 * @param channel 频道名称 * @param message 消息体 */ @Override public void onMessage(String channel, String message) { Command cmd = Command.parse(message); handleCommand(cmd); } ``` ## 使用 ```go // 需要和 J2Cache 连接到同一个 Redis 节点,同一个 DB redisClient := redis.NewClient(&redis.Options{ Network: "tcp", Addr: "127.0.0.1:6379", DB: 0, }) // model 和 J2Cache redis.storage 保持一致 // channel 和 J2Cache redis.channel 保持一致 cli := NewClearCache(redisClient, "Hash|Generic", "channel") // 清理指定 region + key cli.Evict(context.Background(), "region", "key") // 清理域 region cli.Clear(context.Background(), "region") ```