redis 缓存穿透 缓存击穿 缓存雪崩
查询y1:
查询x:
查询z:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | 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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通