{"id":644,"date":"2023-12-16T09:01:01","date_gmt":"2023-12-16T01:01:01","guid":{"rendered":""},"modified":"2023-12-16T09:01:01","modified_gmt":"2023-12-16T01:01:01","slug":"Redis \u7f13\u5b58\u51fb\u7a7f\uff0c\u7f13\u5b58\u7a7f\u900f\uff0c\u7f13\u5b58\u96ea\u5d29\u539f\u56e0+\u89e3\u51b3\u65b9\u6848","status":"publish","type":"post","link":"https:\/\/mushiming.com\/644.html","title":{"rendered":"Redis \u7f13\u5b58\u51fb\u7a7f\uff0c\u7f13\u5b58\u7a7f\u900f\uff0c\u7f13\u5b58\u96ea\u5d29\u539f\u56e0+\u89e3\u51b3\u65b9\u6848"},"content":{"rendered":"
\n \u5728\u6211\u4eec\u65e5\u5e38\u7684\u5f00\u53d1\u4e2d\uff0c\u65e0\u4e0d\u90fd\u662f\u4f7f\u7528\u6570\u636e\u5e93\u6765\u8fdb\u884c\u6570\u636e\u7684\u5b58\u50a8\uff0c\u7531\u4e8e\u4e00\u822c\u7684\u7cfb\u7edf\u4efb\u52a1\u4e2d\u901a\u5e38\u4e0d\u4f1a\u5b58\u5728\u9ad8\u5e76\u53d1\u7684\u60c5\u51b5\uff0c\u6240\u4ee5\u8fd9\u6837\u770b\u8d77\u6765\u5e76\u6ca1\u6709\u4ec0\u4e48\u95ee\u9898\uff0c\u53ef\u662f\u4e00\u65e6\u6d89\u53ca\u5927\u6570\u636e\u91cf\u7684\u9700\u6c42\uff0c\u6bd4\u5982\u4e00\u4e9b\u5546\u54c1\u62a2\u8d2d\u7684\u60c5\u666f\uff0c\u6216\u8005\u662f\u4e3b\u9875\u8bbf\u95ee\u91cf\u77ac\u95f4\u8f83\u5927\u7684\u65f6\u5019\uff0c\u5355\u4e00\u4f7f\u7528\u6570\u636e\u5e93\u6765\u4fdd\u5b58\u6570\u636e\u7684\u7cfb\u7edf\u4f1a\u56e0\u4e3a\u9762\u5411\u78c1\u76d8\uff0c\u78c1\u76d8\u8bfb\/\u5199\u901f\u5ea6\u6bd4\u8f83\u6162\u7684\u95ee\u9898\u800c\u5b58\u5728\u4e25\u91cd\u7684\u6027\u80fd\u5f0a\u7aef\uff0c\u4e00\u77ac\u95f4\u6210\u5343\u4e0a\u4e07\u7684\u8bf7\u6c42\u5230\u6765\uff0c\u9700\u8981\u7cfb\u7edf\u5728\u6781\u77ed\u7684\u65f6\u95f4\u5185\u5b8c\u6210\u6210\u5343\u4e0a\u4e07\u6b21\u7684\u8bfb\/\u5199\u64cd\u4f5c\uff0c\u8fd9\u4e2a\u65f6\u5019\u5f80\u5f80\u4e0d\u662f\u6570\u636e\u5e93\u80fd\u591f\u627f\u53d7\u7684\uff0c\u6781\u5176\u5bb9\u6613\u9020\u6210\u6570\u636e\u5e93\u7cfb\u7edf\u762b\u75ea\uff0c\u6700\u7ec8\u5bfc\u81f4\u670d\u52a1\u5b95\u673a\u7684\u4e25\u91cd\u751f\u4ea7\u95ee\u9898\u3002<\/span><\/span><\/span><\/p>\n \u4e3a\u4e86\u514b\u670d\u4e0a\u8ff0\u7684\u95ee\u9898\uff0c\u9879\u76ee\u901a\u5e38\u4f1a\u5f15\u5165NoSQL\u6280\u672f\uff0c\u8fd9\u662f\u4e00\u79cd\u57fa\u4e8e\u5185\u5b58\u7684\u6570\u636e\u5e93\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e00\u5b9a\u7684\u6301\u4e45\u5316\u529f\u80fd\u3002<\/span><\/span><\/span><\/p>\n redis\u6280\u672f\u5c31\u662fNoSQL\u6280\u672f\u4e2d\u7684\u4e00\u79cd\uff0c\u4f46\u662f\u5f15\u5165redis\u53c8\u6709\u53ef\u80fd\u51fa\u73b0\u7f13\u5b58\u7a7f\u900f\uff0c\u7f13\u5b58\u51fb\u7a7f\uff0c\u7f13\u5b58\u96ea\u5d29\u7b49\u95ee\u9898\u3002\u672c\u6587\u5c31\u5bf9\u8fd9\u4e09\u79cd\u95ee\u9898\u8fdb\u884c\u8f83\u6df1\u5165\u5256\u6790\u3002<\/span><\/span><\/span><\/p>\n \n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n \n \n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n \n \n key\u53ef\u80fd\u4f1a\u5728\u67d0\u4e9b\u65f6\u95f4\u70b9\u88ab\u8d85\u9ad8\u5e76\u53d1\u5730\u8bbf\u95ee\uff0c\u662f\u4e00\u79cd\u975e\u5e38\u201c\u70ed\u70b9\u201d\u7684\u6570\u636e\u3002\u8fd9\u4e2a\u65f6\u5019\uff0c\u9700\u8981\u8003\u8651\u4e00\u4e2a\u95ee\u9898\uff1a\u7f13\u5b58\u88ab\u201c\u51fb\u7a7f\u201d\u7684\u95ee\u9898\u3002<\/span><\/span><\/span><\/p>\n \u4f7f\u7528\u4e92\u65a5\u9501(mutex key)<\/span><\/span><\/strong><\/span><\/p>\n \u4e1a\u754c\u6bd4\u8f83\u5e38\u7528\u7684\u505a\u6cd5\uff0c\u662f\u4f7f\u7528mutex\u3002\u7b80\u5355\u5730\u6765\u8bf4\uff0c\u5c31\u662f\u5728\u7f13\u5b58\u5931\u6548\u7684\u65f6\u5019\uff08\u5224\u65ad\u62ff\u51fa\u6765\u7684\u503c\u4e3a\u7a7a\uff09\uff0c\u4e0d\u662f\u7acb\u5373\u53bbload db\uff0c\u800c\u662f\u5148\u4f7f\u7528\u7f13\u5b58\u5de5\u5177\u7684\u67d0\u4e9b\u5e26\u6210\u529f\u64cd\u4f5c\u8fd4\u56de\u503c\u7684\u64cd\u4f5c\uff08\u6bd4\u5982Redis\u7684SETNX\u6216\u8005Memcache\u7684ADD\uff09\u53bbset\u4e00\u4e2amutex key\uff0c\u5f53\u64cd\u4f5c\u8fd4\u56de\u6210\u529f\u65f6\uff0c\u518d\u8fdb\u884cload db\u7684\u64cd\u4f5c\u5e76\u56de\u8bbe\u7f13\u5b58\uff1b\u5426\u5219\uff0c\u5c31\u91cd\u8bd5\u6574\u4e2aget\u7f13\u5b58\u7684\u65b9\u6cd5\u3002<\/span><\/span><\/span><\/p>\n SETNX\uff0c\u662f\u300cSET if Not eXists\u300d\u7684\u7f29\u5199\uff0c\u4e5f\u5c31\u662f\u53ea\u6709\u4e0d\u5b58\u5728\u7684\u65f6\u5019\u624d\u8bbe\u7f6e\uff0c\u53ef\u4ee5\u5229\u7528\u5b83\u6765\u5b9e\u73b0\u9501\u7684\u6548\u679c\u3002<\/span><\/span><\/span><\/p>\n public String get(key) { String value = redis.get(key);<\/p>\n if (value == null) { \/\/\u4ee3\u8868\u7f13\u5b58\u503c\u8fc7\u671f<\/p>\n \/\/\u8bbe\u7f6e3min\u7684\u8d85\u65f6\uff0c\u9632\u6b62del\u64cd\u4f5c\u5931\u8d25\u7684\u65f6\u5019\uff0c\u4e0b\u6b21\u7f13\u5b58\u8fc7\u671f\u4e00\u76f4\u4e0d\u80fdload db<\/p>\n if (redis.setnx(key_mutex, 1, 3 * 60) == 1) { \/\/\u4ee3\u8868\u8bbe\u7f6e\u6210\u529f<\/p>\n value = db.get(key);<\/p>\n redis.set(key, value, expire_secs);<\/p>\n redis.del(key_mutex);<\/p>\n } else { \/\/\u8fd9\u4e2a\u65f6\u5019\u4ee3\u8868\u540c\u65f6\u5019\u7684\u5176\u4ed6\u7ebf\u7a0b\u5df2\u7ecfload db\u5e76\u56de\u8bbe\u5230\u7f13\u5b58\u4e86\uff0c\u8fd9\u65f6\u5019\u91cd\u8bd5\u83b7\u53d6\u7f13\u5b58\u503c\u5373\u53ef<\/p>\n sleep(50);<\/p>\n get(key); \/\/\u91cd\u8bd5<\/p>\n }<\/p>\n } else { return value; <\/p>\n }<\/p>\n }<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n \n \n \u90a3\u4e48\u5728\u9ad8\u5e76\u53d1\u7684\u60c5\u51b5\u4e0b\uff0c\u4e00\u4e2a key \u8fc7\u671f\u4e86\uff0c\u7136\u540e\uff0c\u5c31\u662f\u51e0\u5343\u51e0\u4e07\u7684\u5e76\u53d1\u8702\u62e5\u800c\u81f3\uff0c\u8fd9\u8be5\u600e\u4e48\u89e3\u51b3\uff1f<\/span><\/span><\/p>\n \u5148\uff0c\u5927\u90e8\u5206\u4eba\u4f1a\u60f3\u5230\u8fd9\u4e48\u4e00\u4e2a\u7b54\u6848\uff1a\u70ed\u70b9\u6570\u636e\u6c38\u4e0d\u8fc7\u671f\u3002<\/span><\/span><\/span><\/p>\n \u5728\u7f51\u7edc\u4e0a\u76db\u884c\u7684\u89e3\u51b3\u65b9\u6848\u6709\u5f88\u591a\uff1a<\/span><\/span><\/span><\/p>\n \u8bbe\u7f6e key \u6c38\u4e0d\u8fc7\u671f\uff0c\u5728\u4fee\u6539\u6570\u636e\u5e93\u65f6\uff0c\u540c\u65f6\u66f4\u65b0\u7f13\u5b58\uff1b<\/span><\/span> \u8bbe\u7f6e key \u6c38\u8fdc\u4e0d\u4f1a\u8fc7\u671f\uff1b<\/span><\/span> \u6240\u4ee5\uff0c\u5927\u90e8\u5206\u4eba\uff0c\u90fd\u53ea\u662f\u505c\u7559\u4e8e\u7eb8\u4e0a\u8c08\u5175\u7684\u9636\u6bb5\uff0c\u6b63\u786e\u4e0e\u5426\uff0c\u6ca1\u6709\u5b9e\u9645\u573a\u666f\u7684\u9a8c\u8bc1\uff0c\u53ea\u80fd\u9760\u76f4\u89c9\u53bb\u5224\u65ad\uff1b<\/span><\/span> \u4e0b\u9762\u6211\u6765\u8be6\u7ec6\u5206\u6790\uff0c\u4e3a\u4ec0\u4e48\uff0c\u8fd9\u4e9b\u89e3\u51b3\u65b9\u6848\uff0c\u5728\u5b9e\u9645\u7684\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u662f\u65e0\u6cd5\u80dc\u4efb\u7684\u3002<\/span><\/span><\/span><\/p>\n \u9996\u5148\uff0c\u6211\u6765\u5206\u6790\uff0ckey \u6c38\u4e0d\u5931\u6548\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u53ef\u884c\u3002<\/span><\/span><\/span><\/p>\n \u56e0\u4e3a\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u9700\u8981\u89e3\u51b3\u7f13\u5b58\u51fb\u7a7f\u95ee\u9898\u7684\u4f01\u4e1a\uff0c\u4ed6\u4eec\u7684\u4e1a\u52a1\u91cf\u4e00\u5b9a\u662f\u666e\u901a\u4eba\u65e0\u6cd5\u60f3\u8c61\u548c\u4f01\u53ca\u7684\uff1b<\/span><\/span> \u4e3a\u4ec0\u4e48\u5462\uff1f<\/span><\/span> \u4f46\u662f\uff0c\u4f60\u53ef\u4ee5\u8bf4\uff0c\u53ea\u5b58\u70ed\u70b9\u6570\u636e\u554a\uff01<\/span><\/span><\/span><\/p>\n \u4f46\u662f\uff0c\u4ec0\u4e48\u53eb\u70ed\u70b9\u6570\u636e\uff1f\u4f60\u89c9\u5f97\u662f\u5c31\u662f\u5417\uff1f<\/span><\/span><\/span><\/p>\n \u771f\u6b63\u7684\u73af\u5883\u4e2d\uff0c\u70ed\u70b9\u6570\u636e\u662f\u5728\u65f6\u65f6\u53d8\u5316\u7740\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u4e00\u4e9b\u70ed\u70b9\u505a\u4e00\u4e9b\u9884\u4f30\uff0c\u4f46\u662f\uff0c\u6211\u4eec\u6c38\u8fdc\u65e0\u6cd5\u4fdd\u8bc1\u6211\u4eec\u80fd\u9884\u4f30\u5230\u591a\u5c11\u3002<\/span><\/span> \u7b2c\u4e8c\u4e2a\uff0c\u7f51\u7edc\u4e0a\u5f88\u6d41\u884c\u7684\u7b54\u6848\u5c31\u662f\uff1a\u52a0\u9501<\/span><\/span><\/span><\/p>\n synchronized \u52a0\u9501\uff0c\u800c\u4e14\u8fd8\u884d\u751f\u51fa\u53cc\u91cd\u68c0\u67e5\u9501\uff1b<\/span><\/span> \u9996\u5148\uff0c\u5bf9\u4e8e\u7f13\u5b58\u7a7f\u900f\u7684\u60c5\u51b5\uff0c\u80af\u5b9a\u662f\u9ad8\u5e76\u53d1\u573a\u666f\uff0c\u6240\u4ee5\u6570\u636e\u5e93\u624d\u53ef\u80fd\u625b\u4e0d\u4f4f\u3002<\/span><\/span> \u6240\u4ee5\uff0c\u5728\u5982\u6b64\u591a\u7684 Tomcat \u96c6\u7fa4\u7684\u60c5\u51b5\u4e0b\uff0c\u4e00\u628a Java \u9501\uff0c\u662f\u4e0d\u53ef\u80fd\u9501\u4f4f\u4e00\u4e2a\u96c6\u7fa4\u7684\u3002<\/span><\/span> \u867d\u7136\u8bf4\uff0c\u7528 ReentrantLock\uff0ctryLock \u52a0\u9501\uff0c\u6210\u529f\u7684\u53bb\u6570\u636e\u5e93\u8bfb\u53d6\u6570\u636e\uff1b<\/span><\/span> \u4f46\u662f\uff0c\u4e00\u4e2a Java \u9501\u6700\u591a\u53ea\u80fd\u591f\u9501\u4e00\u4e2a JVM \u8fdb\u7a0b\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u96c6\u7fa4\u6765\u8bf4\uff0c\u8fd9\u7edd\u5bf9\u662f\u8fdc\u8fdc\u4e0d\u591f\u7684\u3002<\/span><\/span> \u6240\u4ee5\u5b9e\u9645\u4e0a\uff0c\u7f13\u5b58\u7a7f\u900f\uff0c\u52a0\u9501\u89e3\u51b3\uff0c\u5fc5\u987b\u8fd8\u8981\u6d89\u53ca\u5230\u5206\u5e03\u5f0f\u9501\u7684\u6982\u5ff5\u3002<\/span><\/span><\/span><\/p>\n \u8fd9\u91cc\u4e0d\u8c08 zookeeper \u4e4b\u7c7b\u7684\u4e1c\u897f\uff0c\u65e2\u7136\u8c08 redis\uff0c\u90a3\u4e48\u5c31\u7528 redis \u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/span><\/span><\/span><\/p>\n \u9996\u5148\uff0c\u5f53\u4e00\u4e2a key \u5931\u6548\uff0c\u4e0d\u7ba1\u662f\u65f6\u95f4\u8fc7\u671f\uff0c\u8fd8\u662f\u88ab LRU\u3001LFU \u5254\u9664\uff0c<\/span><\/span> <\/p>\n \u770b\u8d77\u6765\u4f3c\u4e4e\u53ef\u884c\uff0c\u4f46\u662f\uff0c\u8fd8\u6709\u95ee\u9898\u5417\uff1f<\/span><\/span><\/span><\/p>\n |