redis 缓存穿透 缓存击穿 缓存雪崩

查询y1:
查询x:
查询z:
public class RedisBloomFilter {
private static final Logger LOGGER = Logger.getLogger(RedisBloomFilter.class);
private static final String BF_KEY_PREFIX = "bf:";
private int numApproxElements;
private double fpp;
private int numHashFunctions;
private int bitmapLength;
private JedisResourcePool jedisResourcePool;
/**
* 构造布隆过滤器。注意:在同一业务场景下,三个参数务必相同
*
* @param numApproxElements 预估元素数量
* @param fpp 可接受的最大误差(假阳性率)
* @param jedisResourcePool Codis专用的Jedis连接池
*/
public RedisBloomFilter(int numApproxElements, double fpp, JedisResourcePool jedisResourcePool) {
this.numApproxElements = numApproxElements;
this.fpp = fpp;
this.jedisResourcePool = jedisResourcePool;
bitmapLength = (int) (-numApproxElements * Math.log(fpp) / (Math.log(2) * Math.log(2)));
numHashFunctions = Math.max(1, (int) Math.round((double) bitmapLength / numApproxElements * Math.log(2)));
}
/**
* 取得自动计算的最优哈希函数个数
*/
public int getNumHashFunctions() {
return numHashFunctions;
}
/**
* 取得自动计算的最优Bitmap长度
*/
public int getBitmapLength() {
return bitmapLength;
}
}
/**
* 计算一个元素值哈希后映射到Bitmap的哪些bit上
*
* @param element 元素值
* @return bit下标的数组
*/
private long[] getBitIndices(String element) {
long[] indices = new long[numHashFunctions];
byte[] bytes = Hashing.murmur3_128()
.hashObject(element, Funnels.stringFunnel(Charset.forName("UTF-8")))
.asBytes();
long hash1 = Longs.fromBytes(
bytes[7], bytes[6], bytes[5], bytes[4], bytes[3], bytes[2], bytes[1], bytes[0]
);
long hash2 = Longs.fromBytes(
bytes[15], bytes[14], bytes[13], bytes[12], bytes[11], bytes[10], bytes[9], bytes[8]
);
long combinedHash = hash1;
for (int i = 0; i < numHashFunctions; i++) {
indices[i] = (combinedHash & Long.MAX_VALUE) % bitmapLength;
combinedHash += hash2;
}
return indices;
}
/**
* 插入元素
*
* @param key 原始Redis键,会自动加上'bf:'前缀
* @param element 元素值,字符串类型
* @param expireSec 过期时间(秒)
*/
public void insert(String key, String element, int expireSec) {
if (key == null || element == null) {
throw new RuntimeException("键值均不能为空");
}
String actualKey = BF_KEY_PREFIX.concat(key);
try (Jedis jedis = jedisResourcePool.getResource()) {
try (Pipeline pipeline = jedis.pipelined()) {
for (long index : getBitIndices(element)) {
pipeline.setbit(actualKey, index, true);
}
pipeline.syncAndReturnAll();
} catch (IOException ex) {
LOGGER.error("pipeline.close()发生IOException", ex);
}
jedis.expire(actualKey, expireSec);
}
}
/**
* 检查元素在集合中是否(可能)存在
*
* @param key 原始Redis键,会自动加上'bf:'前缀
* @param element 元素值,字符串类型
*/
public boolean mayExist(String key, String element) {
if (key == null || element == null) {
throw new RuntimeException("键值均不能为空");
}
String actualKey = BF_KEY_PREFIX.concat(key);
boolean result = false;
try (Jedis jedis = jedisResourcePool.getResource()) {
try (Pipeline pipeline = jedis.pipelined()) {
for (long index : getBitIndices(element)) {
pipeline.getbit(actualKey, index);
}
result = !pipeline.syncAndReturnAll().contains(false);
} catch (IOException ex) {
LOGGER.error("pipeline.close()发生IOException", ex);
}
}
return result;
}
public class RedisBloomFilterTest {
private static final int NUM_APPROX_ELEMENTS = 3000;
private static final double FPP = 0.03;
private static final int DAY_SEC = 60 * 60 * 24;
private static JedisResourcePool jedisResourcePool;
private static RedisBloomFilter redisBloomFilter;
@BeforeClass
public static void beforeClass() throws Exception {
jedisResourcePool = RoundRobinJedisPool.create()
.curatorClient("10.10.99.130:2181,10.10.99.132:2181,10.10.99.133:2181,10.10.99.124:2181,10.10.99.125:2181,", 10000)
.zkProxyDir("/jodis/bd-redis")
.build();
redisBloomFilter = new RedisBloomFilter(NUM_APPROX_ELEMENTS, FPP, jedisResourcePool);
System.out.println("numHashFunctions: " + redisBloomFilter.getNumHashFunctions());
System.out.println("bitmapLength: " + redisBloomFilter.getBitmapLength());
}
@AfterClass
public static void afterClass() throws Exception {
jedisResourcePool.close();
}
@Test
public void testInsert() throws Exception {
redisBloomFilter.insert("topic_read:8839540:20190609", "76930242", DAY_SEC);
redisBloomFilter.insert("topic_read:8839540:20190609", "76930243", DAY_SEC);
redisBloomFilter.insert("topic_read:8839540:20190609", "76930244", DAY_SEC);
redisBloomFilter.insert("topic_read:8839540:20190609", "76930245", DAY_SEC);
redisBloomFilter.insert("topic_read:8839540:20190609", "76930246", DAY_SEC);
}
@Test
public void testMayExist() throws Exception {
System.out.println(redisBloomFilter.mayExist("topic_read:8839540:20190609", "76930242"));
System.out.println(redisBloomFilter.mayExist("topic_read:8839540:20190609", "76930244"));
System.out.println(redisBloomFilter.mayExist("topic_read:8839540:20190609", "76930246"));
System.out.println(redisBloomFilter.mayExist("topic_read:8839540:20190609", "76930248"));
}
}

127.0.0.1:6379> setnx lock value1 #在键lock不存在的情况下,将键key的值设置为value1 (integer) 1 127.0.0.1:6379> setnx lock value2 #试图覆盖lock的值,返回0表示失败 (integer) 0 127.0.0.1:6379> get lock #获取lock的值,验证没有被覆盖 "value1" 127.0.0.1:6379> del lock #删除lock的值,删除成功 (integer) 1 127.0.0.1:6379> setnx lock value2 #再使用setnx命令设置,返回0表示成功 (integer) 1 127.0.0.1:6379> get lock #获取lock的值,验证设置成功 "value2"

https://www.zhihu.com/question/300767410/answer/1749442787
https://www.cnblogs.com/williamjie/p/11132211.html
https://www.jianshu.com/p/47fd7f86c848
https://www.jianshu.com/p/c2defe549b40
https://blog.csdn.net/womenyiqilalala/article/details/105205532










浙公网安备 33010602011771号