{"id":828,"date":"2010-10-25T00:59:37","date_gmt":"2010-10-24T15:59:37","guid":{"rendered":"http:\/\/peta.okechan.net\/blog\/?p=828"},"modified":"2010-10-25T00:59:37","modified_gmt":"2010-10-24T15:59:37","slug":"redis%e3%81%a7%e3%82%bd%e3%83%bc%e3%83%88","status":"publish","type":"post","link":"https:\/\/peta.okechan.net\/blog\/archives\/828","title":{"rendered":"Redis\u3067\u30bd\u30fc\u30c8"},"content":{"rendered":"<p>\u4e00\u90e8\u3067Redis\u304c\u30a2\u30c4\u3044\u3068\u3044\u3046\u3053\u3068\u3067\u89e6\u3063\u3066\u307f\u307e\u3057\u305f\u3002<br \/>\nSQL\u3068\u306f\u304b\u306a\u308a\u9055\u3063\u3066\u3066\u7406\u89e3\u3059\u308b\u306e\u306b\u5c11\u3057\u82e6\u52b4\u3057\u307e\u3057\u305f\u3002<br \/>\n\u5fd8\u308c\u306a\u3044\u3088\u3046\u306b\u30bd\u30fc\u30c8\u306b\u95a2\u9023\u3059\u308b\u51e6\u7406\u306e\u65b9\u6cd5\u30922\u30d1\u30bf\u30fc\u30f3\u66f8\u3044\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<p>\u306a\u304a\u3001Redis\u306e\u4f7f\u3044\u65b9\u306f\u307e\u30601\u6642\u9593\u7a0b\u5ea6\u3057\u304b\u8abf\u3079\u3066\u306a\u3044\u3067\u3059\u3057\u3001NoSQL\u81ea\u4f53\u59cb\u3081\u3066\u306a\u306e\u3067\u3001\u300c\u30bd\u30fc\u30c8\u306f\u3053\u3046\u3084\u308b\u3093\u3060\u3088\u300d\u7684\u306a\u30a8\u30f3\u30c8\u30ea\u3067\u306f\u306a\u304f\u3066\u300c\u3053\u3046\u3084\u3063\u305f\u3089\u30bd\u30fc\u30c8\u3067\u304d\u305f\u3093\u3060\u3051\u3069\u3053\u308c\u3067\u3044\u3044\u306e\uff1f\u6559\u3048\u3066\u30a8\u30ed\u30a4\u4eba\u300d\u7684\u306a\u30a8\u30f3\u30c8\u30ea\u3067\u3059\u306e\u3067\u3001\u6307\u6458\uff0f\u88dc\u8db3\u306a\u3069\u3042\u308b\u3068\u3042\u308a\u304c\u305f\u3044\u3067\u3059\u3002<\/p>\n<p>\u4f7f\u3063\u3066\u308b\u306e\u306fRedis 1.2.6 + redis-py 1.34.1 + Python 2.7\u3067\u3059\u3002<\/p>\n<p>1. id\u306e\u30bb\u30c3\u30c8\u3068\u30b9\u30b3\u30a2\u30ea\u30f3\u30b0\u7528\u306e\u30c7\u30fc\u30bf\u3092\u5225\u306b\u7528\u610f\u3059\u308b\u65b9\u6cd5\uff08\u666e\u901a\u306eset\u3092\u4f7f\u3046\u65b9\u6cd5\uff09<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport redis, random\r\n\r\n# Redis\u30b5\u30fc\u30d0\u30fc\u306b\u63a5\u7d9a\r\nr = redis.Redis(host='127.0.0.1', port=6379, db=0)\r\n\r\n# \u30bd\u30fc\u30c8\u7528\u306e\u30c7\u30fc\u30bf\u3092\u751f\u6210\r\nfor id in xrange(100):\r\n\tscore = random.randint(0, 100)\r\n\tr.sadd('set_of_ids', id)\r\n\tr.set('score_' + str(id), score)\r\n\tr.set('value_' + str(id), 'id: %d, score: %d' % (id, score))\r\n\r\n# score\u306e\u9ad8\u3044\u9806\u306b\u30bd\u30fc\u30c8\u3057\u3066\u6700\u521d\u306e20\u4ef6id\u306e\u307f\u8868\u793a\r\nfor id in r.sort('set_of_ids', by='score_*', desc=True, start=0, num=20) or &#x5B;]:\r\n\tprint id\r\n\r\n# score\u306e\u9ad8\u3044\u9806\u306b\u30bd\u30fc\u30c8\u3057\u3066\u6700\u521d\u306e20\u4ef6\u306e\u5024\u3092\u8868\u793a\r\nfor v in r.sort('set_of_ids', by='score_*', get='value_*', desc=True, start=0, num=20) or &#x5B;]:\r\n\tprint v\r\n\r\n# \u4f7f\u3063\u305f\u30c7\u30fc\u30bf\u306e\u524a\u9664\r\nr.delete('set_of_ids')\r\nfor k in r.keys('score_*') + r.keys('value_*'):\r\n\tr.delete(k)\r\n<\/pre>\n<p>2. id\u306e\u30bb\u30c3\u30c8\u3068\u30b9\u30b3\u30a2\u30ea\u30f3\u30b0\u7528\u306e\u30c7\u30fc\u30bf\u3092\u4e00\u5ea6\u306b\u7528\u610f\u3059\u308b\u65b9\u6cd5\uff08\u30bd\u30fc\u30c8\u6e08\u307fset\u3092\u4f7f\u3046\u65b9\u6cd5\uff09<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport redis, random\r\n\r\n# Redis\u30b5\u30fc\u30d0\u30fc\u306b\u63a5\u7d9a\r\nr = redis.Redis(host='127.0.0.1', port=6379, db=0)\r\n\r\n# \u30bd\u30fc\u30c8\u7528\u306e\u30c7\u30fc\u30bf\u3092\u751f\u6210\r\nfor id in xrange(100):\r\n\tscore = random.randint(0, 100)\r\n\tr.zadd('sorted_set_of_ids', str(id), score)\r\n\tr.set('value_' + str(id), 'id: %d, score: %d' % (id, score))\r\n\r\n# score\u306e\u9ad8\u3044\u9806\u306b\u30bd\u30fc\u30c8\u3057\u3066\u6700\u521d\u306e20\u4ef6id\u306e\u307f\u8868\u793a\r\nfor id in r.zrange('sorted_set_of_ids', 0, 20 - 1, desc=True) or &#x5B;]:\r\n\tprint id\r\n\r\n# score\u306e\u9ad8\u3044\u9806\u306b\u30bd\u30fc\u30c8\u3057\u3066\u6700\u521d\u306e20\u4ef6\u306e\u5024\u3092\u8868\u793a\r\nfor v in r.mget(&#x5B;'value_' + str(id) for id in r.zrange('sorted_set_of_ids', 0, 20 - 1, desc=True) or &#x5B;]]):\r\n\tprint v\r\n\r\n# \u4f7f\u3063\u305f\u30c7\u30fc\u30bf\u306e\u524a\u9664\r\nr.delete('sorted_set_of_ids')\r\nfor k in r.keys('value_*'):\r\n\tr.delete(k)\r\n<\/pre>\n<p>\u30bd\u30fc\u30c8\u6e08\u307fset\u3092\u4f7f\u3046\u307b\u3046\u304c\u30ad\u30fc\u306e\u30bd\u30fc\u30c8\u306b\u306f\u5411\u3044\u3066\u3044\u308b\u3088\u3046\u306a\u6c17\u304c\u3057\u307e\u3059\u304c\u3001\u5024\u307e\u3067\u53d6\u5f97\u3059\u308b\u3068\u306a\u308b\u3068\u3001\u4e00\u5ea6\u30ad\u30fc\u306e\u30ea\u30b9\u30c8\u3092Python\u5074\u3067\u53d7\u3051\u53d6\u308b\u305f\u3081\u7f8e\u3057\u304f\u306a\u3044\u6c17\u304c\u3002<br \/>\n\u666e\u901a\u306eset\u306f\u30bd\u30fc\u30c8\u3067\u5024\u307e\u3067\u53d6\u5f97\u3059\u308b\u51e6\u7406\u304c\u4e00\u5ea6\u306b\u66f8\u3051\u307e\u3059\u304c\u3001\u30ad\u30fc\u3092\u7121\u99c4\u306b\u6d88\u8cbb\u3057\u3066\u308b\u6c17\u304c\u3057\u306a\u304f\u3082\u306a\u3044\u3002<\/p>\n<p>Redis\u3082redis-py\u3082\u30d0\u30fc\u30b8\u30e7\u30f32\u7cfb\u306e\u30e2\u30ce\u304c\u51fa\u3066\u307e\u3059\u306e\u3067\u3001\u305d\u3063\u3061\u3092\u4f7f\u3046\u3068\u3082\u3063\u3068\u30b9\u30de\u30fc\u30c8\u306b\u66f8\u3051\u308b\u306e\u304b\u3082\u3002<\/p>\n<p>\u4eca\u56de\u306f\u30c6\u30b9\u30c8\u306a\u306e\u3067score\u3092\u30e9\u30f3\u30c0\u30e0\u306a\u5024\u306b\u30a4\u30ad\u30ca\u30ea\u8a2d\u5b9a\u3057\u3066\u307e\u3059\u304c\u3001\u5b9f\u969b\u306fscore\u3092\u4e0a\u6607\u3055\u305b\u308b\u30bf\u30a4\u30df\u30f3\u30b0\u3067\u3001r.incr(&#8216;score_&#8217; + str(id))\u3084\u3001r.zincrby(&#8216;sorted_set_of_ids&#8217;, str(id)) \u3092\u4f7f\u3046\u306e\u304c\u666e\u901a\u304b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u90e8\u3067Redis\u304c\u30a2\u30c4\u3044\u3068\u3044\u3046\u3053\u3068\u3067\u89e6\u3063\u3066\u307f\u307e\u3057\u305f\u3002<br \/>\nSQL\u3068\u306f\u304b\u306a\u308a\u9055\u3063\u3066\u3066\u7406\u89e3\u3059\u308b\u306e\u306b\u5c11\u3057\u82e6\u52b4\u3057\u307e\u3057\u305f\u3002<br \/>\n\u5fd8\u308c\u306a\u3044\u3088\u3046\u306b\u30bd\u30fc\u30c8\u306b\u95a2\u9023\u3059\u308b\u51e6\u7406\u306e\u65b9\u6cd5\u30922\u30d1\u30bf\u30fc\u30f3\u66f8\u3044\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<p>\u306a\u304a\u3001Redis\u306e\u4f7f\u3044\u65b9\u306f\u307e\u30601\u6642\u9593\u7a0b\u5ea6\u3057\u304b\u8abf\u3079\u3066\u306a\u3044\u3067\u3059\u3057\u3001NoSQL\u81ea\u4f53\u59cb\u3081\u3066\u306a\u306e\u3067\u3001\u300c\u30bd\u30fc\u30c8\u306f\u3053\u3046\u3084\u308b\u3093\u3060\u3088\u300d\u7684\u306a\u30a8\u30f3\u30c8\u30ea\u3067\u306f\u306a\u304f\u3066\u300c\u3053\u3046\u3084\u3063\u305f\u3089\u30bd\u30fc\u30c8\u3067\u304d\u305f\u3093\u3060\u3051\u3069\u3053\u308c\u3067\u3044\u3044\u306e\uff1f\u6559\u3048\u3066\u30a8\u30ed\u30a4\u4eba\u300d\u7684\u306a\u30a8\u30f3\u30c8\u30ea\u3067\u3059\u306e\u3067\u3001\u6307\u6458\uff0f\u88dc\u8db3\u306a\u3069\u3042\u308b\u3068\u3042\u308a\u304c\u305f\u3044\u3067\u3059\u3002<\/p>\n<p>\u4f7f\u3063\u3066\u308b\u306e\u306fRedis 1.2.6 + redis-py 1.34.1 + Python 2.7\u3067\u3059\u3002<\/p>\n<p>1. id\u306e\u30bb\u30c3\u30c8\u3068\u30b9\u30b3\u30a2\u30ea\u30f3\u30b0\u7528\u306e\u30c7\u30fc\u30bf\u3092\u5225\u306b\u7528\u610f\u3059\u308b\u65b9\u6cd5\uff08\u666e\u901a\u306eset\u3092\u4f7f\u3046\u65b9\u6cd5\uff09<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport redis, random\r\n\r\n# Redis\u30b5\u30fc\u30d0\u30fc\u306b\u63a5\u7d9a\r\nr = redis.Redis(host='127.0.0.1', port=6379, db=0)\r\n\r\n# \u30bd\u30fc\u30c8\u7528\u306e\u30c7\u30fc\u30bf\u3092\u751f\u6210\r\nfor id in xrange(100):\r\n\tscore = random.randint(0, 100)\r\n\tr.sadd('set_of_ids', id)\r\n\tr.set('score_' + str(id), score)\r\n\tr.set('value_' + str(id), 'id: %d, score: %d' % (id, score))\r\n\r\n# score\u306e\u9ad8\u3044\u9806\u306b\u30bd\u30fc\u30c8\u3057\u3066\u6700\u521d\u306e20\u4ef6id\u306e\u307f\u8868\u793a\r\nfor id in r.sort('set_of_ids', by='score_*', desc=True, start=0, num=20) or &#x5B;]:\r\n\tprint id\r\n\r\n# score\u306e\u9ad8\u3044\u9806\u306b\u30bd\u30fc\u30c8\u3057\u3066\u6700\u521d\u306e20\u4ef6\u306e\u5024\u3092\u8868\u793a\r\nfor v in r.sort('set_of_ids', by='score_*', get='value_*', desc=True, start=0, num=20) or &#x5B;]:\r\n\tprint v\r\n\r\n# \u4f7f\u3063\u305f\u30c7\u30fc\u30bf\u306e\u524a\u9664\r\nr.delete('set_of_ids')\r\nfor k in r.keys('score_*') + r.keys('value_*'):\r\n\tr.delete(k)\r\n<\/pre>\n<p>2. id\u306e\u30bb\u30c3\u30c8\u3068\u30b9\u30b3\u30a2\u30ea\u30f3\u30b0\u7528\u306e\u30c7\u30fc\u30bf\u3092\u4e00\u5ea6\u306b\u7528\u610f\u3059\u308b\u65b9\u6cd5\uff08\u30bd\u30fc\u30c8\u6e08\u307fset\u3092\u4f7f\u3046\u65b9\u6cd5\uff09<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport redis, random\r\n\r\n# Redis\u30b5\u30fc\u30d0\u30fc\u306b\u63a5\u7d9a\r\nr = redis.Redis(host='127.0.0.1', port=6379, db=0)\r\n\r\n# \u30bd\u30fc\u30c8\u7528\u306e\u30c7\u30fc\u30bf\u3092\u751f\u6210\r\nfor id in xrange(100):\r\n\tscore = random.randint(0, 100)\r\n\tr.zadd('sorted_set_of_ids', str(id), score)\r\n\tr.set('value_' + str(id), 'id: %d, score: %d' % (id, score))\r\n\r\n# score\u306e\u9ad8\u3044\u9806\u306b\u30bd\u30fc\u30c8\u3057\u3066\u6700\u521d\u306e20\u4ef6id\u306e\u307f\u8868\u793a\r\nfor id in r.zrange('sorted_set_of_ids', 0, 20 - 1, desc=True) or &#x5B;]:\r\n\tprint id\r\n\r\n# score\u306e\u9ad8\u3044\u9806\u306b\u30bd\u30fc\u30c8\u3057\u3066\u6700\u521d\u306e20\u4ef6\u306e\u5024\u3092\u8868\u793a\r\nfor v in r.mget(&#x5B;'value_' + str(id) for id in r.zrange('sorted_set_of_ids', 0, 20 - 1, desc=True) or &#x5B;]]):\r\n\tprint v\r\n\r\n# \u4f7f\u3063\u305f\u30c7\u30fc\u30bf\u306e\u524a\u9664\r\nr.delete('sorted_set_of_ids')\r\nfor k in r.keys('value_*'):\r\n\tr.delete(k)\r\n<\/pre>\n<p>\u30bd\u30fc\u30c8\u6e08\u307fset\u3092\u4f7f\u3046\u307b\u3046\u304c\u30ad\u30fc\u306e\u30bd\u30fc\u30c8\u306b\u306f\u5411\u3044\u3066\u3044\u308b\u3088\u3046\u306a\u6c17\u304c\u3057\u307e\u3059\u304c\u3001\u5024\u307e\u3067\u53d6\u5f97\u3059\u308b\u3068\u306a\u308b\u3068\u3001\u4e00\u5ea6\u30ad\u30fc\u306e\u30ea\u30b9\u30c8\u3092Python\u5074\u3067\u53d7\u3051\u53d6\u308b\u305f\u3081\u7f8e\u3057\u304f\u306a\u3044\u6c17\u304c\u3002<br \/>\n\u666e\u901a\u306eset\u306f\u30bd\u30fc\u30c8\u3067\u5024\u307e\u3067\u53d6\u5f97\u3059\u308b\u51e6\u7406\u304c\u4e00\u5ea6\u306b\u66f8\u3051\u307e\u3059\u304c\u3001\u30ad\u30fc\u3092\u7121\u99c4\u306b\u6d88\u8cbb\u3057\u3066\u308b\u6c17\u304c\u3057\u306a\u304f\u3082\u306a\u3044\u3002<\/p>\n<p>Redis\u3082redis-py\u3082\u30d0\u30fc\u30b8\u30e7\u30f32\u7cfb\u306e\u30e2\u30ce\u304c\u51fa\u3066\u307e\u3059\u306e\u3067\u3001\u305d\u3063\u3061\u3092\u4f7f\u3046\u3068\u3082\u3063\u3068\u30b9\u30de\u30fc\u30c8\u306b\u66f8\u3051\u308b\u306e\u304b\u3082\u3002<\/p>\n<p>\u4eca\u56de\u306f\u30c6\u30b9\u30c8\u306a\u306e\u3067score\u3092\u30e9\u30f3\u30c0\u30e0\u306a\u5024\u306b\u30a4\u30ad\u30ca\u30ea\u8a2d\u5b9a\u3057\u3066\u307e\u3059\u304c\u3001\u5b9f\u969b\u306fscore\u3092\u4e0a\u6607\u3055\u305b\u308b\u30bf\u30a4\u30df\u30f3\u30b0\u3067\u3001r.incr(&#8216;score_&#8217; + str(id))\u3084\u3001r.zincrby(&#8216;sorted_set_of_ids&#8217;, str(id)) \u3092\u4f7f\u3046\u306e\u304c\u666e\u901a\u304b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[60,357],"class_list":["post-828","post","type-post","status-publish","format-standard","hentry","category-tech","tag-python","tag-redis"],"_links":{"self":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts\/828","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/comments?post=828"}],"version-history":[{"count":0,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts\/828\/revisions"}],"wp:attachment":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/media?parent=828"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/categories?post=828"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/tags?post=828"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}