{"id":257,"date":"2023-02-24T06:13:59","date_gmt":"2023-02-23T22:13:59","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=257"},"modified":"2023-04-30T15:18:10","modified_gmt":"2023-04-30T07:18:10","slug":"redis-hash","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/24\/redis-hash\/","title":{"rendered":"Redis \u54c8\u5e0c(Hash)"},"content":{"rendered":"<h2>Redis \u54c8\u5e0c(Hash)<\/h2>\n<p>Redis hash \u662f\u4e00\u4e2a string \u7c7b\u578b\u7684 field\uff08\u5b57\u6bb5\uff09\u548c value\uff08\u503c\uff09\u7684\u6620\u5c04\u8868\uff0chash \u7279\u522b\u9002\u5408\u7528\u4e8e\u5b58\u50a8\u5bf9\u8c61<\/p>\n<p>Redis \u4e2d\u6bcf\u4e2a hash \u53ef\u4ee5\u5b58\u50a8 2^32 &#8211; 1 \u952e\u503c\u5bf9\uff0840\u591a\u4ebf\uff09<\/p>\n<p><!-- more --><\/p>\n<p><strong>\u5b9e\u4f8b<\/strong><\/p>\n<pre><code class=\"language-bash\">127.0.0.1:6379&gt; HMSET appblog name &quot;redis tutorial&quot; description &quot;redis basic commands for caching&quot; likes 20 visitors 23000\nOK\n127.0.0.1:6379&gt; HGETALL appblog\n1) &quot;name&quot;\n2) &quot;redis tutorial&quot;\n3) &quot;description&quot;\n4) &quot;redis basic commands for caching&quot;\n5) &quot;likes&quot;\n6) &quot;20&quot;\n7) &quot;visitors&quot;\n8) &quot;23000&quot;<\/code><\/pre>\n<p>\u5728\u4ee5\u4e0a\u5b9e\u4f8b\u4e2d\uff0c\u6211\u4eec\u8bbe\u7f6e\u4e86 redis \u7684\u4e00\u4e9b\u63cf\u8ff0\u4fe1\u606f(name, description, likes, visitors) \u5230\u54c8\u5e0c\u8868\u7684 appblog \u4e2d<\/p>\n<h2>Redis hash \u547d\u4ee4<\/h2>\n<h3>hdel key field1 [field2]<\/h3>\n<p>\u5220\u9664\u4e00\u4e2a\u6216\u591a\u4e2a\u54c8\u5e0c\u8868\u5b57\u6bb5\uff0c\u4e0d\u5b58\u5728\u7684\u5b57\u6bb5\u5c06\u88ab\u5ffd\u7565<\/p>\n<pre><code class=\"language-bash\">redis 127.0.0.1:6379&gt; HSET myhash field1 &quot;foo&quot;\n(integer) 1\nredis 127.0.0.1:6379&gt; HDEL myhash field1\n(integer) 1\nredis 127.0.0.1:6379&gt; HDEL myhash field2\n(integer) 0<\/code><\/pre>\n<h3>hexists key field<\/h3>\n<p>\u67e5\u770b\u54c8\u5e0c\u8868 key \u4e2d\uff0c\u6307\u5b9a\u7684\u5b57\u6bb5\u662f\u5426\u5b58\u5728<\/p>\n<pre><code class=\"language-bash\">redis 127.0.0.1:6379&gt; HSET myhash field1 &quot;foo&quot;\n(integer) 1\nredis 127.0.0.1:6379&gt; HEXISTS myhash field1\n(integer) 1\nredis 127.0.0.1:6379&gt; HEXISTS myhash field2\n(integer) 0<\/code><\/pre>\n<h3>hget key field<\/h3>\n<p>\u83b7\u53d6\u5b58\u50a8\u5728\u54c8\u5e0c\u8868\u4e2d\u6307\u5b9a\u5b57\u6bb5\u7684\u503c\uff0c\u4e0d\u5b58\u5728\u65f6\u8fd4\u56de nil<\/p>\n<pre><code class=\"language-bash\"># \u5b57\u6bb5\u5b58\u5728\n\nredis&gt; HSET site redis redis.com\n(integer) 1\n\nredis&gt; HGET site redis\n&quot;redis.com&quot;\n\n# \u5b57\u6bb5\u4e0d\u5b58\u5728\n\nredis&gt; HGET site mysql\n(nil)<\/code><\/pre>\n<h3>hgetall key<\/h3>\n<p>\u83b7\u53d6\u5728\u54c8\u5e0c\u8868\u4e2d\u6307\u5b9a key \u7684\u6240\u6709\u5b57\u6bb5\u548c\u503c<\/p>\n<pre><code class=\"language-bash\">redis&gt; HSET myhash field1 &quot;Hello&quot;\n(integer) 1\nredis&gt; HSET myhash field2 &quot;World&quot;\n(integer) 1\nredis&gt; HGETALL myhash\n1) &quot;field1&quot;\n2) &quot;Hello&quot;\n3) &quot;field2&quot;\n4) &quot;World&quot;\nredis&gt; <\/code><\/pre>\n<h3>hincrby key field increment<\/h3>\n<p>\u4e3a\u54c8\u5e0c\u8868 key \u4e2d\u7684\u6307\u5b9a\u5b57\u6bb5\u7684\u6574\u6570\u503c\u52a0\u4e0a\u589e\u91cf increment<\/p>\n<ul>\n<li>\u589e\u91cf\u4e5f\u53ef\u4ee5\u4e3a\u8d1f\u6570\uff0c\u76f8\u5f53\u4e8e\u5bf9\u6307\u5b9a\u5b57\u6bb5\u8fdb\u884c\u51cf\u6cd5\u64cd\u4f5c<\/li>\n<li>\u5982\u679c\u54c8\u5e0c\u8868\u7684 key \u4e0d\u5b58\u5728\uff0c\u4e00\u4e2a\u65b0\u7684\u54c8\u5e0c\u8868\u88ab\u521b\u5efa\u5e76\u6267\u884c HINCRBY \u547d\u4ee4<\/li>\n<li>\u5982\u679c\u6307\u5b9a\u7684\u5b57\u6bb5\u4e0d\u5b58\u5728\uff0c\u90a3\u4e48\u5728\u6267\u884c\u547d\u4ee4\u524d\uff0c\u5b57\u6bb5\u7684\u503c\u88ab\u521d\u59cb\u5316\u4e3a 0<\/li>\n<li>\u5bf9\u4e00\u4e2a\u50a8\u5b58\u5b57\u7b26\u4e32\u503c\u7684\u5b57\u6bb5\u6267\u884c HINCRBY \u547d\u4ee4\u5c06\u9020\u6210\u4e00\u4e2a\u9519\u8bef<\/li>\n<li>\u672c\u64cd\u4f5c\u7684\u503c\u88ab\u9650\u5236\u5728 64 \u4f4d(bit)\u6709\u7b26\u53f7\u6570\u5b57\u8868\u793a\u4e4b\u5185<\/li>\n<\/ul>\n<pre><code class=\"language-bash\">redis&gt; HSET myhash field 5\n(integer) 1\nredis&gt; HINCRBY myhash field 1\n(integer) 6\nredis&gt; HINCRBY myhash field -1\n(integer) 5\nredis&gt; HINCRBY myhash field -10\n(integer) -5\nredis&gt; <\/code><\/pre>\n<h3>hincrbyfloat key field increment<\/h3>\n<p>\u4e3a\u54c8\u5e0c\u8868 key \u4e2d\u7684\u6307\u5b9a\u5b57\u6bb5\u7684\u6d6e\u70b9\u6570\u503c\u52a0\u4e0a\u589e\u91cf increment<\/p>\n<p>\u5982\u679c\u6307\u5b9a\u7684\u5b57\u6bb5\u4e0d\u5b58\u5728\uff0c\u90a3\u4e48\u5728\u6267\u884c\u547d\u4ee4\u524d\uff0c\u5b57\u6bb5\u7684\u503c\u88ab\u521d\u59cb\u5316\u4e3a 0<\/p>\n<pre><code class=\"language-bash\">redis&gt; HSET mykey field 10.50\n(integer) 1\nredis&gt; HINCRBYFLOAT mykey field 0.1\n&quot;10.6&quot;\nredis&gt; HINCRBYFLOAT mykey field -5\n&quot;5.6&quot;\nredis&gt; HSET mykey field 5.0e3\n(integer) 0\nredis&gt; HINCRBYFLOAT mykey field 2.0e2\n&quot;5200&quot;\nredis&gt; <\/code><\/pre>\n<h3>hkeys key<\/h3>\n<p>\u83b7\u53d6\u6240\u6709\u54c8\u5e0c\u8868\u4e2d\u7684\u5b57\u6bb5<\/p>\n<pre><code class=\"language-bash\">redis 127.0.0.1:6379&gt; HSET myhash field1 &quot;foo&quot;\n(integer) 1\nredis 127.0.0.1:6379&gt; HSET myhash field2 &quot;bar&quot;\n(integer) 1\nredis 127.0.0.1:6379&gt; HKEYS myhash\n1) &quot;field1&quot;\n2) &quot;field2&quot;<\/code><\/pre>\n<h3>hlen key<\/h3>\n<p>\u83b7\u53d6\u54c8\u5e0c\u8868\u4e2d\u5b57\u6bb5\u7684\u6570\u91cf<\/p>\n<p>\u5f53 key \u4e0d\u5b58\u5728\u65f6\uff0c\u8fd4\u56de 0<\/p>\n<pre><code class=\"language-bash\">redis 127.0.0.1:6379&gt; HSET myhash field1 &quot;foo&quot;\n(integer) 1\nredis 127.0.0.1:6379&gt; HSET myhash field2 &quot;bar&quot;\n(integer) 1\nredis 127.0.0.1:6379&gt; HLEN myhash\n(integer) 2<\/code><\/pre>\n<h3>hmget key field1 [field2]<\/h3>\n<p>\u83b7\u53d6\u6240\u6709\u7ed9\u5b9a\u5b57\u6bb5\u7684\u503c<\/p>\n<p>\u5982\u679c\u6307\u5b9a\u7684\u5b57\u6bb5\u4e0d\u5b58\u5728\u4e8e\u54c8\u5e0c\u8868\uff0c\u90a3\u4e48\u8fd4\u56de\u4e00\u4e2a nil \u503c<\/p>\n<pre><code class=\"language-bash\">redis 127.0.0.1:6379&gt; HSET myhash field1 &quot;foo&quot;\n(integer) 1\nredis 127.0.0.1:6379&gt; HSET myhash field2 &quot;bar&quot;\n(integer) 1\nredis 127.0.0.1:6379&gt; HMGET myhash field1 field2 nofield\n1) &quot;foo&quot;\n2) &quot;bar&quot;\n3) (nil)<\/code><\/pre>\n<h3>hmset key field1 value1 [field2 value2 ]<\/h3>\n<p>\u540c\u65f6\u5c06\u591a\u4e2a field-value (\u57df-\u503c)\u5bf9\u8bbe\u7f6e\u5230\u54c8\u5e0c\u8868 key \u4e2d<\/p>\n<ul>\n<li>\u6b64\u547d\u4ee4\u4f1a\u8986\u76d6\u54c8\u5e0c\u8868\u4e2d\u5df2\u5b58\u5728\u7684\u5b57\u6bb5<\/li>\n<li>\u5982\u679c\u54c8\u5e0c\u8868\u4e0d\u5b58\u5728\uff0c\u4f1a\u521b\u5efa\u4e00\u4e2a\u7a7a\u54c8\u5e0c\u8868\uff0c\u5e76\u6267\u884c HMSET \u64cd\u4f5c<\/li>\n<\/ul>\n<pre><code class=\"language-bash\">redis 127.0.0.1:6379&gt; HMSET myhash field1 &quot;Hello&quot; field2 &quot;World&quot;\nOK\nredis 127.0.0.1:6379&gt; HGET myhash field1\n&quot;Hello&quot;\nredis 127.0.0.1:6379&gt; HGET myhash field2\n&quot;World&quot;<\/code><\/pre>\n<h3>hset key field value<\/h3>\n<p>\u5c06\u54c8\u5e0c\u8868 key \u4e2d\u7684\u5b57\u6bb5 field \u7684\u503c\u8bbe\u4e3a value<\/p>\n<ul>\n<li>\u5982\u679c\u54c8\u5e0c\u8868\u4e0d\u5b58\u5728\uff0c\u4e00\u4e2a\u65b0\u7684\u54c8\u5e0c\u8868\u88ab\u521b\u5efa\u5e76\u8fdb\u884c HSET \u64cd\u4f5c<\/li>\n<li>\u5982\u679c\u5b57\u6bb5\u5df2\u7ecf\u5b58\u5728\u4e8e\u54c8\u5e0c\u8868\u4e2d\uff0c\u65e7\u503c\u5c06\u88ab\u8986\u76d6<\/li>\n<\/ul>\n<pre><code class=\"language-bash\">redis 127.0.0.1:6379&gt; HSET myhash field1 &quot;foo&quot;\nOK\nredis 127.0.0.1:6379&gt; HGET myhash field1\n&quot;foo&quot;\n\nredis 127.0.0.1:6379&gt; HSET website google &quot;www.g.cn&quot;       # \u8bbe\u7f6e\u4e00\u4e2a\u65b0\u57df\n(integer) 1\n\nredis 127.0.0.1:6379&gt; HSET website google &quot;www.google.com&quot; # \u8986\u76d6\u4e00\u4e2a\u65e7\u57df\n(integer) 0<\/code><\/pre>\n<h3>bashnx key field value<\/h3>\n<p>\u53ea\u6709\u5728\u5b57\u6bb5 field \u4e0d\u5b58\u5728\u65f6\uff0c\u8bbe\u7f6e\u54c8\u5e0c\u8868\u5b57\u6bb5\u7684\u503c<\/p>\n<ul>\n<li>\u5982\u679c\u54c8\u5e0c\u8868\u4e0d\u5b58\u5728\uff0c\u4e00\u4e2a\u65b0\u7684\u54c8\u5e0c\u8868\u88ab\u521b\u5efa\u5e76\u8fdb\u884c HSET \u64cd\u4f5c<\/li>\n<li>\u5982\u679c\u5b57\u6bb5\u5df2\u7ecf\u5b58\u5728\u4e8e\u54c8\u5e0c\u8868\u4e2d\uff0c\u64cd\u4f5c\u65e0\u6548<\/li>\n<li>\u5982\u679c key \u4e0d\u5b58\u5728\uff0c\u4e00\u4e2a\u65b0\u54c8\u5e0c\u8868\u88ab\u521b\u5efa\u5e76\u6267\u884c HSETNX \u547d\u4ee4<\/li>\n<\/ul>\n<pre><code class=\"language-bash\">redis 127.0.0.1:6379&gt; HSETNX myhash field1 &quot;foo&quot;\n(integer) 1\nredis 127.0.0.1:6379&gt; HSETNX myhash field1 &quot;bar&quot;\n(integer) 0\nredis 127.0.0.1:6379&gt; HGET myhash field1\n&quot;foo&quot;<\/code><\/pre>\n<h3>hvals key<\/h3>\n<p>\u83b7\u53d6\u54c8\u5e0c\u8868\u4e2d\u6240\u6709\u503c\uff0c\u5f53 key \u4e0d\u5b58\u5728\u65f6\uff0c\u8fd4\u56de\u4e00\u4e2a\u7a7a\u8868<\/p>\n<pre><code class=\"language-bash\">redis 127.0.0.1:6379&gt; HSET myhash field1 &quot;foo&quot;\n(integer) 1\nredis 127.0.0.1:6379&gt; HSET myhash field2 &quot;bar&quot;\n(integer) 1\nredis 127.0.0.1:6379&gt; HVALS myhash\n1) &quot;foo&quot;\n2) &quot;bar&quot;\n\n# \u7a7a\u54c8\u5e0c\u8868\/\u4e0d\u5b58\u5728\u7684key\n\nredis 127.0.0.1:6379&gt; EXISTS not_exists\n(integer) 0\n\nredis 127.0.0.1:6379&gt; HVALS not_exists\n(empty list or set)<\/code><\/pre>\n<h3>hscan key cursor [MATCH pattern] [COUNT count]<\/h3>\n<p>\u8fed\u4ee3\u54c8\u5e0c\u8868\u4e2d\u7684\u952e\u503c\u5bf9<\/p>\n<p>Redis HSCAN \u547d\u4ee4\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-bash\">HSCAN key cursor [MATCH pattern] [COUNT count]\n``\n\n- cursor - \u6e38\u6807\n- pattern - \u5339\u914d\u7684\u6a21\u5f0f\n- count - \u6307\u5b9a\u4ece\u6570\u636e\u96c6\u91cc\u8fd4\u56de\u591a\u5c11\u5143\u7d20\uff0c\u9ed8\u8ba4\u503c\u4e3a 10\n\n```bash\n> HMSET sites google \"google.com\" appblog \"appblog.cn\" weibo \"weibo.com\" taobao \"taobao.com\"\nOK\n> HSCAN sites 0 match \"run*\"\n1) \"0\"\n2) 1) \"appblog\"\n2) \"appblog.cn\"<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Redis \u54c8\u5e0c(Hash) Redis hash \u662f\u4e00\u4e2a string \u7c7b\u578b\u7684 field\uff08\u5b57\u6bb5\uff09\u548c val [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":["post-257","post","type-post","status-publish","format-standard","hentry","category-redis"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/257","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=257"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/257\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}