{"id":1515,"date":"2013-01-09T14:06:06","date_gmt":"2013-01-09T05:06:06","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=1515"},"modified":"2018-11-14T16:33:57","modified_gmt":"2018-11-14T07:33:57","slug":"memcached%e3%81%a8redis%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2013\/01\/09\/memcached%e3%81%a8redis%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6\/","title":{"rendered":"memcached\u3068redis\u306b\u3064\u3044\u3066"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f<\/p>\n<p>\u6751\u4e0a\u3067\u3059\u3002<\/p>\n<p>\u3061\u3087\u3063\u3068\u9ad8\u901f\u306b\u8aad\u307f\u66f8\u304d\u3067\u304d\u308b\u5171\u6709\u30b9\u30c8\u30ec\u30fc\u30b8\u304c\u6848\u4ef6\u3067\u5fc5\u8981\u306b\u306a\u3063\u305f\u306e\u3067\u3001KVS\u306ememcached\u3068redis\u3092\u89e6\u3063\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<h2>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h2>\n<p>\u3069\u3061\u3089\u3082yum\u3067\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3067\u304d\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true \">\r\nsudo yum install memcached\r\nsudo yum install redis\r\n<\/pre>\n<p>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u304c\u7d42\u308f\u3063\u305f\u3089\u8d77\u52d5\u3067\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true \">\r\nsudo \/etc\/init.d\/memcached start\r\nsudo \/etc\/init.d\/redis start\r\n<\/pre>\n<p>\u4eca\u56de\u306f\u3069\u3061\u3089\u3082Ruby\u304b\u3089\u64cd\u4f5c\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true \" >\r\ngem install dalli\r\ngem install redis\r\n<\/pre>\n<p>\u3063\u3066\u3057\u3066\u3001\u3082\u3057\u304f\u306fGemfile\u306b\u66f8\u3044\u3066\u300cbundle install\u300d \u6e96\u5099\u306fOK<br \/>\n\u3061\u306a\u307f\u306b\u300cdalli\u300d\u306fmemcached\u7528\u306egem\u3067\u3059\u3002<br \/>\n(\u8aad\u307f\u65b9\u306f\u300c\u30c0\u30ea\uff1f\u3000\u30c0\u30eb\u30ea\uff01\uff1f\u300d)<\/p>\n<h2>memcached<\/h2>\n<p>\u307e\u305a\u3001memcached\u306edalli\u3067\u3059\u304c\u3001\u3053\u3093\u306a\u611f\u3058\u3067\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true \" >\r\nrequire 'dalli'\r\n\r\ndalli = Dalli::Client.new('localhost')\r\n\r\ndalli.set(:string, \"hoge\") # \u6587\u5b57\u5217hoge\u3092\u30bb\u30c3\u30c8\r\ndalli.get(:string)         # \u6587\u5b57\u5217\u3092\u53d6\u5f97\r\n\r\ndalli.set(:time, Time.now) # Time\u3092\u30bb\u30c3\u30c8\r\ndalli.get(:time)           # Time\u3092\u53d6\u5f97\u3002Time\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u53d6\u5f97\u53ef\u80fd\r\n\r\ndalli.set(:hash, {:a => \"a\", :b => \"b\"}) # \u30cf\u30c3\u30b7\u30e5\u3092\u30bb\u30c3\u30c8\r\ndalli.get(:hash)                         # \u30cf\u30c3\u30b7\u30e5\u3092\u53d6\u5f97 \u30cf\u30c3\u30b7\u30e5\u3067\u53d6\u5f97\u53ef\u80fd\r\n\r\ndalli.set(:array, [\"1\", \"2\", \"3\"])       # \u30ea\u30b9\u30c8\u3092\u30bb\u30c3\u30c8\r\ndalli.get(:array)                        # \u30ea\u30b9\u30c8\u3092\u53d6\u5f97 \u30ea\u30b9\u30c8\u3067\u53d6\u5f97\u53ef\u80fd\r\n\r\ndalli.set(:string, \"hoge\", 5) # \u7b2c3\u5f15\u6570\u306b\u6574\u6570\u3092\u30bb\u30c3\u30c8\r\ndalli.get(:string)            # 5\u79d2\u9593\u306f\u30c7\u30fc\u30bf\u53d6\u308a\u51fa\u3057\u53ef\u80fd\u30025\u79d2\u5f8c\u306b\u30c7\u30fc\u30bf\u306f\u524a\u9664\u3055\u308c\u308b\r\n<\/pre>\n<h2>redis<\/h2>\n<p>\u7d9a\u3044\u3066redis\u3067\u3059\u304c\u3001<\/p>\n<pre class=\"lang:ruby decode:true \" >\r\nrequire 'redis'\r\n\r\nredis = Redis.new\r\n\r\nredis.set(:string, \"hoge\") # \u6587\u5b57\u5217hoge\u3092\u30bb\u30c3\u30c8\r\nredis.get(:string)         # \u6587\u5b57\u5217\u3092\u53d6\u5f97\r\n\r\nredis.set(:time, Time.now) # Time\u3092\u30bb\u30c3\u30c8\r\nredis.get(:time)           # Time\u3092\u53d6\u5f97\u304c\u6587\u5b57\u5217\u3067\u53d6\u5f97\u3055\u308c\u308b\uff01\uff01\r\n\r\n<\/pre>\n<p>\u3063\u3066\u3053\u3068\u3067\u3001redis\u306e\u5834\u5408\u306f\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u6587\u5b57\u5217\u3068\u306a\u3063\u3066\u53d6\u5f97\u3055\u308c\u3061\u3083\u3044\u307e\u3059\u3002<br \/>\n\u306a\u3093\u3067\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306a\u30b3\u30f3\u30d0\u30fc\u30c8\u3092\u5b9f\u65bd<\/p>\n<pre class=\"lang:ruby decode:true \" >\r\nrequire 'redis'\r\n\r\ndef encode(val)\r\n  Marshal.dump val\r\nend\r\n\r\ndef decode(val)\r\n  Marshal.load val\r\nend\r\n\r\nredis.set(:string, encode(\"hoge\")) # \u6587\u5b57\u5217hoge\u3092\u30bb\u30c3\u30c8\r\ndecode(redis.get(:string))         # \u6587\u5b57\u5217\u3092\u53d6\u5f97\r\n\r\nredis.set(:time, encode(Time.now)) # Time\u3092\u30bb\u30c3\u30c8\r\ndecode(redis.get(:time))           # Time\u3092\u53d6\u5f97\u3002Time\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u53d6\u5f97\u53ef\u80fd\r\n\r\nredis.set(:hash, encode({:a => \"a\", :b => \"b\"})) # \u30cf\u30c3\u30b7\u30e5\u3092\u30bb\u30c3\u30c8\r\ndecode(redis.get(:hash))                         # \u30cf\u30c3\u30b7\u30e5\u3092\u53d6\u5f97 \u30cf\u30c3\u30b7\u30e5\u3067\u53d6\u5f97\u53ef\u80fd\r\n\r\nredis.set(:array, encode([\"1\", \"2\", \"3\"]))       # \u30ea\u30b9\u30c8\u3092\u30bb\u30c3\u30c8\r\ndecode(redis.get(:array))                        # \u30ea\u30b9\u30c8\u3092\u53d6\u5f97 \u30ea\u30b9\u30c8\u3067\u53d6\u5f97\u53ef\u80fd\r\n<\/pre>\n<p>\u3053\u3093\u306a\u611f\u3058\u3067\u3044\u3051\u305d\u3046\u3067\u3059\u3002<br \/>\n\u3042\u3068\u3001redis\u306e\u5834\u5408\u306f<\/p>\n<pre class=\"lang:ruby decode:true \" >\r\nredis.setex(:string, 5, encode(\"hoge\")) # \u7b2c2\u5f15\u6570\u306b\u6574\u6570\u3092\u30bb\u30c3\u30c8\r\ndecode(redis.get(:string))              # 5\u79d2\u9593\u306f\u30c7\u30fc\u30bf\u53d6\u308a\u51fa\u3057\u53ef\u80fd\u30025\u79d2\u5f8c\u306b\u30c7\u30fc\u30bf\u306f\u524a\u9664\u3055\u308c\u308b\r\n<\/pre>\n<p>\u306b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u30e1\u30bd\u30c3\u30c9\u304c\u5909\u308f\u3063\u3066\u3001dalli\u3068\u306f\u5f15\u6570\u306e\u5834\u6240\u304c\u9055\u3046\u3053\u3068\u306b\u6ce8\u610f\u3067\u3059\u3002<br \/>\ndecode\u306e\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u304c\u3001nil\u306e\u5834\u5408\u306f\u30a8\u30e9\u30fc\u306b\u306a\u308b\u306e\u3067\u3001nil\u3092\u8003\u616e\u3057\u305f\u4f5c\u308a\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n<p>\u3061\u306a\u307f\u306bnil\u3092\u30bb\u30c3\u30c8\u3057\u305f\u6642\u3001\u52d5\u304d\u304c\u9055\u3044\u307e\u3057\u305f\u3002<\/p>\n<pre class=\"lang:ruby decode:true \" >\r\ndalli.set(:nil, nil) # nil\u3092\u30bb\u30c3\u30c8\r\ndalli.get(:nil)      # nil\u304c\u53d6\u5f97\u3055\u308c\u308b\u3002\r\n\r\nredis.set(:nil, nil) # nil\u3092\u30bb\u30c3\u30c8\r\ndalli.get(:nil)      # \u7a7a\u6587\u5b57\u304c\u53d6\u5f97\u3055\u308c\u308b\r\n\r\nredis.set(:nil, encode(nil)) # \u30a8\u30f3\u30b3\u30fc\u30c9\u3057\u3066nil\u3092\u30bb\u30c3\u30c8\r\ndecode(redis.get(:nil))      # \u30c7\u30b3\u30fc\u30c9\u3059\u308b\u3068nil\u3067\u53d6\u5f97\u53ef\u80fd\r\n\r\ndecode(nil)                  # \u3053\u308c\u306f\u4e0a\u8a18\u306e\u5b9a\u7fa9\u3067\u306f\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u307e\u3059\u3002\r\n                             # nil\u306e\u6642\u306fnil\u3092\u8fd4\u3059\u3088\u3046\u306a\u4ed5\u69d8\u306b\u3059\u308b\u304b\u3001\r\n                             # nil\u53c2\u7167\u306e\u30a8\u30e9\u30fc\u306b\u3059\u308b\u304b\u306f\u30a2\u30d7\u30ea\u6b21\u7b2c\uff1f\r\n<\/pre>\n<h2>\u901f\u5ea6\u5dee<\/h2>\n<p>KVS\u306a\u3093\u3067\u3001\u9ad8\u901f\u306e\u8aad\u307f\u66f8\u304d\u304c\u6c42\u3081\u3089\u308c\u307e\u3059\u3002<br \/>\n\u4eca\u56de\u306f3\u79d2\u9593\u3067\u3069\u308c\u3060\u3051\u8aad\u307f\u66f8\u304d(KVS\u3078\u306e\u5024\u30bb\u30c3\u30c8\u3068\u5024\u53d6\u5f97\u30921\u56de\u305a\u3064\u884c\u3046)\u304c\u3067\u304d\u308b\u304b\u3092\u6e2c\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<h4>memcached<\/h4>\n<pre class=\"lang:ruby decode:true \" >\r\ncount = 0\r\nbegin\r\n  timeout(3) {\r\n    loop {\r\n      dalli.set(count, count)\r\n      dalli.get(count)\r\n      count = count + 1\r\n    }\r\n  }\r\nensure\r\n  p count\r\nend\r\n<\/pre>\n<h4>redis<\/h4>\n<pre class=\"lang:ruby decode:true \" >\r\ncount = 0\r\nbegin\r\n  timeout(3) {\r\n    loop {\r\n      redis.set(count, encode(count))\r\n      decode(redis.get(count))\r\n      count = count + 1\r\n    }\r\n  }\r\nensure\r\n  p count\r\nend\r\n<\/pre>\n<p>\u7d50\u679c\u306f\u4e0b\u8a18\u306e\u901a\u308a<br \/>\n10\u56de\u306e\u5e73\u5747\u3067\u3059\u3002<\/p>\n<table>\n<tbody>\n<tr>\n<th><\/th>\n<th>3\u79d2\u9593\u3067\u306e\u8aad\u307f\u66f8\u304d\u56de\u6570<\/th>\n<\/tr>\n<tr>\n<th style=\"text-align:left\">memcached<\/th>\n<td style=\"text-align:right; background: none repeat scroll 0 0 #E5E4E8;\">19958.5<\/td>\n<\/tr>\n<tr>\n<th style=\"text-align:left\">redis<\/th>\n<td style=\"text-align:right; background: none repeat scroll 0 0 #E5E4E8;\">19627.5<\/td>\n<\/tr>\n<tr>\n<th style=\"text-align:left\">redis \u30b3\u30f3\u30d0\u30fc\u30c8\u306a\u3057<\/th>\n<td style=\"text-align:right; background: none repeat scroll 0 0 #E5E4E8;\">20638.5<\/td>\n<\/tr>\n<tr>\n<th style=\"text-align:left\">DB postgres<\/th>\n<td style=\"text-align:right; background: none repeat scroll 0 0 #E5E4E8;\">956.5<\/td>\n<\/tr>\n<tr>\n<th style=\"text-align:left\">\u30cf\u30c3\u30b7\u30e5<\/th>\n<td style=\"text-align:right; background: none repeat scroll 0 0 #E5E4E8;\">3402237.3<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>KVS\u3060\u3051\u3067\u306f\u305d\u308c\u307b\u3069\u5dee\u304c\u306a\u304b\u3063\u305f\u306e\u3067\u3001DB\u3068\u30cf\u30c3\u30b7\u30e5\u3082\u6bd4\u8f03\u5bfe\u8c61\u306b\u8ffd\u52a0\u3057\u3066\u307f\u307e\u3057\u305f\u3002<br \/>\n\u4eca\u56de\u306f\u30b9\u30c8\u30ec\u30fc\u30b8\u304c\u30ed\u30fc\u30ab\u30eb\u306b\u3042\u308b\u306e\u3067\u3053\u306e\u6570\u5b57\u304c\u51fa\u307e\u3057\u305f\u304c\u3001\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u8d8a\u3057\u306b\u3042\u308c\u3070\u3001\u3082\u3046\u5c11\u3057\u901f\u5ea6\u306f\u4f4e\u4e0b\u3059\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u901f\u5ea6\u306f<br \/>\n\u30e1\u30e2\u30ea > KVS > DB<br \/>\n\u3067\u305d\u308c\u305e\u308c200\u500d\u307b\u3069\u306e\u5dee\u304c\u3042\u308b\u307f\u305f\u3044\u3067\u3059\u306d\u3002<\/p>\n<p>\u5f53\u7136\u3061\u3083\u5f53\u7136\u306e\u7d50\u679c\u3067\u3059\u304c\u3002<\/p>\n<p>KVS\u3092\u4f7f\u3046\u3053\u3068\u304c\u3042\u308c\u3070\u53c2\u8003\u306b\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f<\/p>\n<p>\u6751\u4e0a\u3067\u3059\u3002<\/p>\n<p>\u3061\u3087\u3063\u3068\u9ad8\u901f\u306b\u8aad\u307f\u66f8\u304d\u3067\u304d\u308b\u5171\u6709\u30b9\u30c8\u30ec\u30fc\u30b8\u304c\u6848\u4ef6\u3067\u5fc5\u8981\u306b\u306a\u3063\u305f\u306e\u3067\u3001KVS\u306ememcached\u3068redis\u3092\u89e6\u3063\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<br \/>\n\u3069\u3061\u3089\u3082yum\u3067\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3067\u304d\u308b\u3068\u601d\u3044\u307e\u3059\u3002<br \/><a href=\"https:\/\/www.techscore.com\/blog\/2013\/01\/09\/memcached%e3%81%a8redis%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6\/\">\u7d9a\u304d\u3092\u8aad\u3080...<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[76,24],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/1515"}],"collection":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/comments?post=1515"}],"version-history":[{"count":1,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/1515\/revisions"}],"predecessor-version":[{"id":17981,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/1515\/revisions\/17981"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=1515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=1515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=1515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}