{"id":1206,"date":"2022-05-10T22:16:31","date_gmt":"2022-05-10T14:16:31","guid":{"rendered":"https:\/\/fireinsect.top\/?p=1206"},"modified":"2022-05-10T22:16:31","modified_gmt":"2022-05-10T14:16:31","slug":"redis-set","status":"publish","type":"post","link":"https:\/\/fireinsect.top\/index.php\/2022\/05\/10\/redis-set\/","title":{"rendered":"Redis Set"},"content":{"rendered":"\n<p>Set\u662fRedis\u4e2d\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4e0eRedis\u7684Zset\u6709\u6240\u533a\u5206\uff1a<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Set\u4e2d\u6587\u63cf\u8ff0\u4e3a\u201c\u65e0\u5e8f\u96c6\u5408\u201d \u7279\u70b9\u662f\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u662f\u65e0\u5e8f\u7684<\/li><li>\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u4e0d\u80fd\u91cd\u590d\uff0c\u662f\u552f\u4e00\u7684<\/li><\/ol>\n\n\n\n<p>\u4e0eZSet\u76f8\u6bd4\u7f3a\u5c11\u4e86\u5206\u6570\uff08point\uff09\uff0c\u6240\u4ee5\u65e0\u6cd5\u6392\u5e8f\u3002<\/p>\n\n\n\n<p class=\"has-large-font-size\">\u65b0\u589e\u6570\u636e<\/p>\n\n\n\n<p>\u4f7f\u7528<code>add()<\/code>\u00a0\u65b9\u6cd5\u6279\u91cf\u589e\u52a0\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">redisTemplate.opsForSet().add(\"ranks\", personalRecord1, personalRecord2);<\/code><\/pre>\n\n\n\n<p>\u65e2\u7136\u662f\u64cd\u4f5c Set\uff0c\u90a3\u4e48\u65b9\u6cd5\u81ea\u7136\u662f\u00a0<code>opsForSet()<\/code>\u00a0\uff0c\u6bcf\u79cd\u6570\u636e\u7c7b\u578b\u90fd\u6709\u81ea\u5df1\u7684\u64cd\u4f5c\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p><code>add()<\/code>&nbsp;\u65b9\u6cd5\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f Key\uff1b\u540e\u9762\u5c31\u662f Java \u5b9e\u4f8b\u5bf9\u8c61\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u5220\u9664\u6570\u636e\u64cd\u4f5c\">\u5220\u9664\u6570\u636e\u64cd\u4f5c<\/h2>\n\n\n\n<p>\u4f7f\u7528&nbsp;<code>remove()<\/code>&nbsp;\u65b9\u6cd5\u5220\u9664\u4e00\u4e2a\u6570\u636e\u5143\u7d20\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">redisTemplate.opsForSet().remove(\"ranks\", personalRecord);<\/code><\/pre>\n\n\n\n<p><code>remove()<\/code>\u00a0\u65b9\u6cd5\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f Key\uff1b\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u5f85\u5220\u9664\u7684\u6570\u636e\u5bf9\u8c61\u3002\u5220\u9664\u4e0e\u6dfb\u52a0\u662f\u5bf9\u5e94\u7684\uff0c\u6dfb\u52a0\u7684\u662f\u81ea\u5b9a\u4e49\u5bf9\u8c61\uff0c\u5220\u9664\u7684\u65f6\u5019\u4e5f\u8981\u4f20\u5165\u76f8\u540c\u7684\u81ea\u5b9a\u4e49\u5bf9\u8c61\u3002\uff08\u5148\u67e5\u8be2\u518d\u5220\u9664\uff09<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u5343\u4e07\u4e0d\u8981\u7406\u89e3\u4e3a\uff0c\u6dfb\u52a0\u7684\u662f personalRecord \u5b9e\u4f8b\u5bf9\u8c61\uff0c\u7136\u540e\u7528 personalRecordId \u5220\u9664\u3002\u4e0d\u80fd\u8ddf MySQL \u6df7\u6dc6\u4e86\u3002<\/p><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u4fee\u6539\u6570\u636e\u64cd\u4f5c\">\u4fee\u6539\u6570\u636e\u64cd\u4f5c<\/h2>\n\n\n\n<p>\u4e0d\u5b58\u5728\u4fee\u6539\u8fd9\u4e2a\u8bf4\u6cd5\u3002\u6216\u8005\u8bf4\uff0c\u4fee\u6539\u7b49\u540c\u4e8e\uff1a\u5148\u5220\u9664\u65e7\u6570\u636e\uff0c\u518d\u52a0\u5165\u65b0\u6570\u636e<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u57fa\u672c\u67e5\u8be2\">\u57fa\u672c\u67e5\u8be2<\/h2>\n\n\n\n<p>\u67e5\u8be2\u65b9\u6cd5\u4e0d\u662f\u5e38\u89c1\u7684 getXX \u800c\u662f&nbsp;<code>members()<\/code>&nbsp;\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">Set&lt;PersonalRecord> datas = redisTemplate.opsForSet().members(\"ranks\");<\/code><\/pre>\n\n\n\n<p>\u901a\u8fc7 Key \u67e5\u8be2\u96c6\u5408\u4e2d\u7684\u6240\u6709\u6570\u636e\u5143\u7d20\u3002\u8fd4\u56de\u503c\u7684\u6cdb\u578b\uff0c\u5c31\u662f\u65b0\u589e\u6570\u636e\u7684\u7c7b\u578b\uff0c\u5f80 Set \u7f13\u5b58\u91cc\u653e\u4e86\u4ec0\u4e48\u6570\u636e\uff0c\u62ff\u51fa\u6765\u5c31\u662f\u4ec0\u4e48\u6570\u636e\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u591a\u96c6\u5408\u64cd\u4f5c\">\u591a\u96c6\u5408\u64cd\u4f5c<\/h2>\n\n\n\n<p>\u4f7f\u7528 Set \u4e00\u822c\u6765\u8bf4\u5e76\u4e0d\u662f\u7528\u4e8e\u6570\u636e\u5bf9\u8c61\u7684\u7f13\u5b58\uff0c\u56e0\u4e3a\u65e0\u5e8f\uff0c\u5b9e\u9645\u4e0a\u64cd\u4f5c\u5f88\u4e0d\u65b9\u4fbf\uff0c\u4e0d\u80fd\u50cf\u5217\u8868\u4e00\u6837\u7cbe\u786e\u67e5\u8be2\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528 Set \u591a\u7528\u4e8e\u96c6\u5408\u95f4\u7684\u64cd\u4f5c\u3002\u6240\u4ee5\uff0c\u63a8\u8350 Set \u5b58\u50a8\u7b80\u5355\u7684\u6570\u636e\uff0c\u6bd4\u5982 Java \u7684\u5b57\u7b26\u4e32\u6216\u6570\u5b57\uff0c\u800c\u4e0d\u8981\u5728 Set \u4e2d\u5b58\u5165\u590d\u6742\u7684 Java \u81ea\u5b9a\u4e49\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>\u6bd4\u5982\u53ea\u5b58\u5165\u4e2a\u4eba\u6218\u7ee9\u7684 id \u503c\u800c\u4e0d\u662f\u6574\u4e2a\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>\u591a\u96c6\u5408\u7684\u64cd\u4f5c\u4e3b\u8981\u6709\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u6c42\u5e76\u96c6\">\u6c42\u5e76\u96c6<\/h3>\n\n\n\n<p>\u7ed9\u5b9a\u4e24\u4e2a\u96c6\u5408A\uff0cB\uff0c\u628a\u4ed6\u4eec\u6240\u6709\u7684\u5143\u7d20\u5408\u5e76\u5728\u4e00\u8d77\u7ec4\u6210\u7684\u96c6\u5408\uff0c\u53eb\u505a\u96c6\u5408A\u4e0e\u96c6\u5408B\u7684\u5e76\u96c6\u3002\u5927\u5bb6\u5e94\u8be5\u5b66\u4e60\u8fc7\u5e76\u96c6\u7684\u6982\u5ff5\u3002<\/p>\n\n\n\n<p><code>union()<\/code>&nbsp;\u65b9\u6cd5\u7528\u4e8e\u6c42\u591a\u4e2a\u96c6\u5408\u7684\u5e76\u96c6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">List&lt;String> keys = <strong>new<\/strong> ArrayList&lt;>();\nkeys.add(\"ranks1\");\nkeys.add(\"ranks2\");\nkeys.add(\"ranks3\");\nSet&lt;Long> unionDatas = redisTemplate.opsForSet().union(keys);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u6c42\u4ea4\u96c6\">\u6c42\u4ea4\u96c6<\/h3>\n\n\n\n<p>\u96c6\u5408\u8bba\u4e2d\uff0c\u8bbeA\uff0cB\u662f\u4e24\u4e2a\u96c6\u5408\uff0c\u7531\u6240\u6709\u5c5e\u4e8e\u96c6\u5408A\u4e14\u5c5e\u4e8e\u96c6\u5408B\u7684\u5143\u7d20\u6240\u7ec4\u6210\u7684\u96c6\u5408\uff0c\u53eb\u505a\u96c6\u5408A\u4e0e\u96c6\u5408B\u7684\u4ea4\u96c6<\/p>\n\n\n\n<p><code>intersect()<\/code>&nbsp;\u65b9\u6cd5\u7528\u4e8e\u6c42\u591a\u4e2a\u96c6\u5408\u7684\u4ea4\u96c6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">List&lt;String> keys = <strong>new<\/strong> ArrayList&lt;>();\nkeys.add(\"ranks1\");\nkeys.add(\"ranks2\");\nkeys.add(\"ranks3\");\nSet&lt;Long> interDatas = redisTemplate.opsForSet().intersect(keys);<\/code><\/pre>\n\n\n\n<p><code>intersect()<\/code>&nbsp;\u65b9\u6cd5\u8fd4\u56de\u7ed9\u5b9a\u6240\u6709\u7ed9\u5b9a\u96c6\u5408\u7684\u4ea4\u96c6\u3002<\/p>\n\n\n\n<p>\u4e0d\u5b58\u5728\u7684\u96c6\u5408 key \u88ab\u89c6\u4e3a\u7a7a\u96c6\u3002\u5f53\u7ed9\u5b9a\u96c6\u5408\u5f53\u4e2d\u6709\u4e00\u4e2a\u7a7a\u96c6\u65f6\uff0c\u7ed3\u679c\u4e5f\u4e3a\u7a7a\u96c6(\u6839\u636e\u96c6\u5408\u8fd0\u7b97\u5b9a\u5f8b)\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u6c42\u5dee\u96c6\">\u6c42\u5dee\u96c6<\/h3>\n\n\n\n<p>\u8bbeA\uff0cB\u662f\u4e24\u4e2a\u96c6\u5408\uff0c\u4ee5\u5c5e\u4e8eA\u800c\u4e0d\u5c5e\u4e8eB\u7684\u5143\u7d20\u4e3a\u5143\u7d20\u7684\u96c6\u5408\u6210\u4e3aA\u4e0eB\u7684\u5dee\u96c6<\/p>\n\n\n\n<p><code>difference()<\/code>&nbsp;\u65b9\u6cd5\u7528\u4e8e\u6c42\u4e00\u4e2a\u96c6\u5408\u4e0e\u5176\u5b83\u96c6\u5408\u7684\u5dee\u96c6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">List&lt;String> otherkeys = <strong>new<\/strong> ArrayList&lt;>();\notherkeys.add(\"ranks2\");\notherkeys.add(\"ranks3\");\nSet&lt;Long> diffDatas = redisTemplate.opsForSet().difference(\"ranks1\", otherkeys);<\/code><\/pre>\n\n\n\n<p><code>difference()<\/code>\u00a0\u65b9\u6cd5\u8fd4\u56de\u7b2c\u4e00\u4e2a\u96c6\u5408\u4e0e\u5176\u4ed6\u96c6\u5408\u4e4b\u95f4\u7684\u5dee\u5f02\uff0c\u4e5f\u53ef\u4ee5\u8ba4\u4e3a\u8bf4\u7b2c\u4e00\u4e2a\u96c6\u5408\u4e2d\u72ec\u6709\u7684\u5143\u7d20\u3002\u4e0d\u5b58\u5728\u7684\u96c6\u5408 key \u5c06\u89c6\u4e3a\u7a7a\u96c6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Set\u662fRedis\u4e2d\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4e0eRedis\u7684Zset\u6709\u6240\u533a\u5206\uff1a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[6],"tags":[],"class_list":["post-1206","post","type-post","status-publish","format-standard","hentry","category-6"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1206"}],"collection":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/comments?post=1206"}],"version-history":[{"count":1,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1206\/revisions"}],"predecessor-version":[{"id":1207,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1206\/revisions\/1207"}],"wp:attachment":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/media?parent=1206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/categories?post=1206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/tags?post=1206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}