{"id":26,"date":"2014-02-20T00:00:00","date_gmt":"2014-02-19T15:00:00","guid":{"rendered":"http:\/\/taitan916.info\/blog\/?p=26"},"modified":"2024-04-18T18:06:54","modified_gmt":"2024-04-18T09:06:54","slug":"post-26","status":"publish","type":"post","link":"https:\/\/taitan916.info\/blog\/archives\/26","title":{"rendered":"PHP\u304b\u3089Redis\u3092\u5229\u7528\u3059\u308b\u969b\u306e\u3088\u304f\u4f7f\u3046\u69cb\u6587\u306b\u3064\u3044\u3066"},"content":{"rendered":"<p>PHP\u304b\u3089Redis\u3092\u5229\u7528\u3059\u308b\u969b\u306e\u3088\u304f\u4f7f\u3046\u69cb\u6587\u306b\u3064\u3044\u3066\u4ee5\u4e0b\u306b\u30e1\u30e2\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u63a5\u7d9a\u958b\u59cb<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">$redis = new Redis();\r\n$redis-&gt;connect('127.0.0.1', 6379) or die('connect error');<\/pre>\n<p>\u30a8\u30e9\u30fc\u3067\u6b62\u3081\u305f\u304f\u306a\u3044\u5834\u5408\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5206\u5c90\u3055\u305b\u308b\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">if( $redis-&gt;connect('127.0.0.1', 6379) ){\r\n    \/\/\u63a5\u7d9aOK\u6642\r\n} else {\r\n    \/\/\u63a5\u7d9aNG\u6642\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h2>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u9078\u629e<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">$redis-&gt;select(\u3053\u3053\u306b\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30ca\u30f3\u30d0\u30fc);<\/pre>\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30ca\u30f3\u30d0\u30fc\u3092\u6307\u5b9a\u3057\u306a\u3044\u5834\u5408\u30c7\u30d5\u30a9\u30eb\u30c8\u3067<strong>db0\u304c\u6307\u5b9a\u3055\u308c\u308b<\/strong>\u3002<\/p>\n<p>\u307e\u305f\u3001\u30c7\u30d5\u30a9\u30eb\u30c8\u3060\u30680~15\u307e\u3067\u6307\u5b9a\u53ef\u80fd\u3067\u3001\u305d\u308c\u4ee5\u4e0a\u306b\u5897\u3084\u3057\u305f\u3044\u5834\u5408\u306f\u4ee5\u4e0b\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u306edatabase\u90e8\u5206\u3092\u7de8\u96c6\u3059\u308b\u3053\u3068\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/etc\/redis.conf<\/pre>\n<p>&nbsp;<\/p>\n<h2>\u63a5\u7d9a\u7d42\u4e86<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">$redis-&gt;close();<\/pre>\n<p>&nbsp;<\/p>\n<h2>\u30c7\u30fc\u30bf\u306e\u683c\u7d0d\u3068\u8aad\u307f\u8fbc\u307f<\/h2>\n<h3>string(\u6587\u5b57\u5217)<\/h3>\n<h4>\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u7b49<\/h4>\n<p>1\u3064\u306ekey\u306b\u5bfe\u3057\u30661GB\u307e\u3067\u683c\u7d0d\u3067\u304d\u308b\u3068\u306e\u3053\u3068\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">$redis-&gt;set('hoge','huga');\r\nvar_dump($redis-&gt;get('hoge')); \/\/string(4) \"huga\" \r\nvar_dump($redis-&gt;get('fuge')); \/\/bool(false)<\/pre>\n<p>\u5b58\u5728\u3057\u306a\u3044key\u3092\u6307\u5b9a\u3059\u308b\u3068false\u304c\u8fd4\u3063\u3066\u304f\u308b\u306e\u3067\u6761\u4ef6\u5206\u5c90\u306e\u969b\u7b49\u306b\u5229\u7528\u3059\u308b\u3002<\/p>\n<p>\u5c1a\u3001<strong>\u540c\u3058key\u3067\u9055\u3046\u5024\u3092\u5165\u308c\u308b\u3068\u4e0a\u66f8\u304d\u3055\u308c\u308b<\/strong>\u70b9\u306b\u6ce8\u610f\u3002<\/p>\n<p>\u307e\u305f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u6307\u5b9a\u3059\u308b\u3053\u3068\u3067\u6709\u52b9\u671f\u9593\u304c\u6307\u5b9a\u3067\u304d\u308b(\u3053\u306e\u5834\u5408\u3060\u306810\u79d2\u9593)\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">$redis-&gt;setex('hoge',10,'huga');<\/pre>\n<h4>\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8<\/h4>\n<p><a href=\"http:\/\/redis.shibu.jp\/commandreference\/strings.html\" target=\"_blank\" rel=\"noopener\">http:\/\/redis.shibu.jp\/commandreference\/strings.html<\/a><\/p>\n<p>&nbsp;<\/p>\n<h3>list(\u30ea\u30b9\u30c8\u578b)<\/h3>\n<h4>\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u7b49<\/h4>\n<p>\u91cd\u8907OK\u306a\u6587\u5b57\u5217\u306e\u30ea\u30b9\u30c8\u578b\u3002\u524d\u5f8c\u304b\u3089\u51fa\u3057\u5165\u308c\u3001<br \/>\n\u7bc4\u56f2\u6307\u5b9a\u7b49\u3067\u53d6\u51fa\u3057\u304c\u53ef\u80fd\u3002\u30ce\u30fc\u30de\u30eb\u306a\u914d\u5217\u3068\u4f3c\u305f\u3088\u3046\u306a\u611f\u89e6\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">$redis-&gt;rpush('hoge','ho');\r\n$redis-&gt;lpush('hoge','ho2');\r\n$redis-&gt;rpush('hoge','ho3');\r\nvar_dump($redis-&gt;lrange('hoge',0,$redis-&gt;llen('hoge'))); \/\/array(3) { [0]=&gt; string(3) \"ho2\" [1]=&gt; string(2) \"ho\" [2]=&gt; string(3) \"ho3\" }<\/pre>\n<p>key\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u306ffalse\u304c\u8fd4\u308b\u3002<\/p>\n<p>\u5c1a\u3001\u6709\u52b9\u671f\u9593\u3092\u6307\u5b9a\u3057\u305f\u3044\u6642\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3059\u308b\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">$redis-&gt;expire('key',\u79d2\u6570);<\/pre>\n<p>\u9006\u306b\u79d2\u6570\u3092\u78ba\u8a8d\u3057\u305f\u3044\u6642\u306f\u4ee5\u4e0b\u3067\u78ba\u8a8d\u3067\u304d\u308b\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">var_dump($redis-&gt;ttl('hoge'));<\/pre>\n<p>key\u304c\u5b58\u5728\u3057\u306a\u304b\u3063\u305f\u308a\u3001\u6709\u52b9\u671f\u9593\u304c\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u306a\u3044\u5834\u5408\u306f-1\u304c\u8fd4\u308b\u306e\u3067\u6ce8\u610f\u3059\u308b\u3053\u3068\u3002<\/p>\n<h4>\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8<\/h4>\n<p><a href=\"http:\/\/redis.shibu.jp\/commandreference\/lists.html\" target=\"_blank\" rel=\"noopener\">http:\/\/redis.shibu.jp\/commandreference\/lists.html<\/a><\/p>\n<p>&nbsp;<\/p>\n<h3>hash(\u30cf\u30c3\u30b7\u30e5\u578b)<\/h3>\n<p>1\u3064\u306ekey\u306b\u5bfe\u3057\u3066field-value\u3067\u683c\u7d0d\u3059\u308b\u3002\u591a\u6b21\u5143\u9023\u60f3\u914d\u5217\u3063\u307d\u3044\u3082\u306e\u3002<\/p>\n<h4><span id=\"i-7\">\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u7b49<\/span><\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">$redis-&gt;hset('age','tanaka',19);\r\n$redis-&gt;hset('age','yamada',22);\r\nvar_dump($redis-&gt;hget('age','tanaka')); \/\/string(2) \"19\"\r\nvar_dump($redis-&gt;hget('age','suzuki')); \/\/bool(false)<\/pre>\n<p>expire\u3067\u79d2\u6570\u3082\u6307\u5b9a\u3067\u304d\u308b\u3002\u79d2\u6570\u304c\u904e\u304e\u305f\u5834\u5408\u306f<br \/>\n\u305d\u306ekey\u306e\u5024\u304c\u5168\u3066\u6d88\u3048\u308b\u3002<\/p>\n<p>\u307e\u305f\u3001\u91cd\u8907\u767b\u9332\u306f\u4e0d\u53ef\u3068\u306a\u3063\u3066\u304a\u308a\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3057\u305f\u5834\u5408\u300133\u6b73\u306b\u4e0a\u66f8\u304d\u3055\u308c\u308b\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">$redis-&gt;hset('age','tanaka',33);<\/pre>\n<h4><span id=\"i-8\">\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8<\/span><\/h4>\n<p><a href=\"http:\/\/redis.shibu.jp\/commandreference\/hashes.html\" target=\"_blank\" rel=\"noopener\">http:\/\/redis.shibu.jp\/commandreference\/hashes.html<\/a><\/p>\n<p>&nbsp;<\/p>\n<h3>set(\u30bb\u30c3\u30c8\u578b) \/ sorted set(\u30bd\u30fc\u30c8\u6e08\u307f\u30bb\u30c3\u30c8\u578b)<\/h3>\n<h4><span id=\"i-7\">\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u7b49<\/span><\/h4>\n<p>\u9806\u4e0d\u540c\u306e\u6587\u5b57\u5217\u578b\u96c6\u5408\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">$redis-&gt;sadd('huga','fufu');\r\nvar_dump($redis-&gt;SPOP('huga')); \/\/string(4) \"fufu\"<\/pre>\n<p>\u3053\u308c\u306e\u4f7f\u3044\u65b9\u3001\u4f7f\u3044\u9053\u306f\u3088\u304f\u5206\u304b\u3089\u306a\u304b\u3063\u305f\u3002<\/p>\n<h4><span id=\"i-8\">\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8<\/span><\/h4>\n<p><a href=\"http:\/\/redis.shibu.jp\/commandreference\/sets.html\" target=\"_blank\" rel=\"noopener\">http:\/\/redis.shibu.jp\/commandreference\/sets.html<\/a><\/p>\n<p><a href=\"http:\/\/redis.shibu.jp\/commandreference\/sortedsets.html\" target=\"_blank\" rel=\"noopener\">http:\/\/redis.shibu.jp\/commandreference\/sortedsets.html<\/a><\/p>\n<p>&nbsp;<\/p>\n<h2>\u305d\u306e\u4ed6<\/h2>\n<h3>\u30b5\u30fc\u30d0\u30fc\u60c5\u5831\u3084\u7d71\u8a08\u60c5\u5831\u3001\u30d0\u30fc\u30b8\u30e7\u30f3\u7b49\u3092\u51fa\u529b<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">var_dump($redis-&gt;info());<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u304b\u3089Redis\u3092\u5229\u7528\u3059\u308b\u969b\u306e\u3088\u304f\u4f7f\u3046\u69cb\u6587\u306b\u3064\u3044\u3066\u4ee5\u4e0b\u306b\u30e1\u30e2\u3002 &nbsp; ... <\/p>\n","protected":false},"author":1,"featured_media":1103,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2,66],"tags":[],"class_list":["post-26","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","category-database"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/posts\/26","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/comments?post=26"}],"version-history":[{"count":5,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/posts\/26\/revisions"}],"predecessor-version":[{"id":6051,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/posts\/26\/revisions\/6051"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/media\/1103"}],"wp:attachment":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/media?parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/categories?post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/tags?post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}