项目背景:把黑白名单数据,通过hash分桶路由的方式存储在redis里面
项目为短信发送平台,以下是黑白名单相关介绍:
黑名单包括三类:
第一类,平台黑名单
此类是指的平台设置的黑名单手机号,设置了以后,就不能给这个手机号发送短信
第二类:账号黑名单
每个使用平台的用户都有一个账号,每个账号可以自己设置一些黑名单手机号
第三类:省份黑名单
此类指的是,可以设置某个省份,凡是判断出手机号码是某个省份的,就不发这个省份
第四类:账号白名单
每个使用短信平台的用户都有一个账号,每个账号可以自己设置一些白名单手机号,优先级高于所有黑名单,在这个账号的白名单中这个账号就可以给这个手机号发送短信
下面是示例代码:
java
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.Map;
public class BlackWhiteListService {
private static final int NUM_SHARDS = 4; // 分片数量
private final RedisTemplate<String, String> redisTemplate;
private final HashOperations<String, String, String> hashOperations;
public BlackWhiteListService(RedisTemplate<String, String> redisTemplate) {
this.redisTemplate = redisTemplate;
this.hashOperations = redisTemplate.opsForHash();
}
// 计算手机号所在的分片
private String getShard(String phoneNumber) {
int hashCode = phoneNumber.hashCode();
int shardIndex = Math.abs(hashCode % NUM_SHARDS); // 使用取模运算确定分片索引
return "shard" + (shardIndex + 1); // 分片索引从1开始
}
// 存储黑名单信息
public void addToBlacklist(String phoneNumber, String blacklistType, String reason) {
String shard = getShard(phoneNumber);
String key = shard + ":blacklist:" + blacklistType;
hashOperations.put(key, phoneNumber, reason);
}
// 存储白名单信息
public void addToWhitelist(String phoneNumber, String whitelistType, String allowed) {
String shard = getShard(phoneNumber);
String key = shard + ":whitelist:" + whitelistType;
hashOperations.put(key, phoneNumber, allowed);
}
// 存储省份黑名单信息
public void addToProvinceBlacklist(String province, String phoneNumber, String blacklistType, String reason) {
String key = "province:" + province + ":blacklist:" + blacklistType;
hashOperations.put(key, phoneNumber, reason);
}
// 获取黑名单信息
public Map<String, String> getBlacklist(String phoneNumber, String blacklistType) {
String shard = getShard(phoneNumber);
String key = shard + ":blacklist:" + blacklistType;
return hashOperations.entries(key);
}
// 获取白名单信息
public Map<String, String> getWhitelist(String phoneNumber, String whitelistType) {
String shard = getShard(phoneNumber);
String key = shard + ":whitelist:" + whitelistType;
return hashOperations.entries(key);
}
// 获取省份黑名单信息
public Map<String, String> getProvinceBlacklist(String province, String blacklistType) {
String key = "province:" + province + ":blacklist:" + blacklistType;
return hashOperations.entries(key);
}
// 获取所有省份黑名单信息
public Map<String, Map<String, String>> getAllProvinceBlacklist(String blacklistType) {
Set<String> keys = redisTemplate.keys("province:*:blacklist:" + blacklistType);
return hashOperations.multiGet(keys);
}
}
接下来是调用方:
java
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
public class Main {
public static void main(String[] args) {
// 创建 RedisTemplate 实例
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
// 设置 Redis 连接配置(例如主机名、端口号、密码等)
// 创建 BlackWhiteListService 实例
BlackWhiteListService blackWhiteListService = new BlackWhiteListService(redisTemplate);
// 添加黑名单
String phoneNumber = "13812345678";
String blacklistType = "platform"; // 黑名单类型为平台黑名单
String reason = "This phone number is in the platform blacklist.";
blackWhiteListService.addToBlacklist(phoneNumber, blacklistType, reason);
// 添加白名单
String whitelistType = "account"; // 白名单类型为账号白名单
String allowed = "true";
blackWhiteListService.addToWhitelist(phoneNumber, whitelistType, allowed);
// 获取黑名单信息
String retrievedReason = blackWhiteListService.getBlacklist(phoneNumber, blacklistType).get(phoneNumber);
System.out.println("Blacklist Reason: " + retrievedReason);
// 获取白名单信息
String retrievedAllowed = blackWhiteListService.getWhitelist(phoneNumber, whitelistType).get(phoneNumber);
System.out.println("Whitelist Allowed: " + retrievedAllowed);
}
}