{"id":5454,"date":"2011-03-20T06:31:18","date_gmt":"2011-03-19T21:31:18","guid":{"rendered":"http:\/\/fukata.org\/?p=5454"},"modified":"2017-08-11T22:58:49","modified_gmt":"2017-08-11T22:58:49","slug":"codeigniter2-cache","status":"publish","type":"post","link":"https:\/\/blog.fukata.org\/archives\/5454\/","title":{"rendered":"[CodeIgniter2.x]Cache\u306e\u5229\u7528\u306b\u3064\u3044\u3066"},"content":{"rendered":"
CodeIgniter 1.x\u306e\u9803\u306e\u30ad\u30e3\u30c3\u30b7\u30e5\u3068\u3044\u3046\u3068\u30da\u30fc\u30b8\u30ad\u30e3\u30c3\u30b7\u30e5\u3067\u3057\u305f\u304c\u3001CodeIgntier2\u304b\u3089Memcache\u7b49\u3092\u5229\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u3001\u30da\u30fc\u30b8\u30ad\u30e3\u30c3\u30b7\u30e5\u3060\u3051\u3067\u306a\u304f\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30ad\u30e3\u30c3\u30b7\u30e5\u3082\u5229\u7528\u53ef\u80fd\u306b\u306a\u308a\u307e\u3057\u305f\u3002\u3068\u3044\u3046\u3053\u3068\u3067\u3001Cache\u306e\u5229\u7528\u306b\u3064\u3044\u3066\u5c11\u3057\u66f8\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002\u7d50\u679c\u304b\u3089\u8a00\u3048\u3070\u3001\u306a\u304b\u306a\u304b\u4f7f\u3044\u3084\u3059\u3044\u3082\u306e\u3067\u3057\u305f\u3002<\/p>\n
\u76ee\u6b21<\/p>
\u73fe\u5728\u3001CodeIgniter 2.0.1\u3067\u30b5\u30dd\u30fc\u30c8\u3057\u3066\u3044\u308b\u30ad\u30e3\u30c3\u30b7\u30e5\u306f\u4ee5\u4e0b\u306e4\u7a2e\u985e\u3067\u3059\u3002<\/p>\n
<\/p>\n
\u6700\u521d\u306b\u8ff0\u3079\u305f4\u7a2e\u985e\u306e\u30ad\u30e3\u30c3\u30b7\u30e5\u306b\u3064\u3044\u3066\u306e\u5b9f\u88c5\u304c\u305d\u308c\u305e\u308c\u542b\u307e\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n
Cache\/drivers\/\u306e\u5404\u5b9f\u88c5\u306eCache\u306e\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u3067\u3059\u3002\u3053\u3053\u3092\u898b\u308c\u3070\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u3067\u3069\u3046\u3044\u3046\u884c\u52d5\uff08\u767b\u9332\u3001\u53d6\u5f97\u3001\u524a\u9664\u306a\u3069\uff09\u304c\u51fa\u6765\u308b\u304b\u308f\u304b\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n
CI_Driver\u3092\u7d99\u627f\u3057\u305f\u30b5\u30d6\u30af\u30e9\u30b9\u304b\u3089__get, __set, __call\u304c\u547c\u3070\u308c\u305f\u5834\u5408\u3001\u89aa\u30af\u30e9\u30b9\u3001\u4eca\u56de\u306e\u5834\u5408\u3060\u3068\u3001Cache\u30af\u30e9\u30b9\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u6a5f\u80fd\u3092\u4ed8\u4e0e\u3059\u308b\u3002\uff08CI_Driver#decorate\uff09<\/p>\n
CI_Driver_Library\u3092\u7d99\u627f\u3059\u308b\u30af\u30e9\u30b9\u304b\u3089__get\u304c\u547c\u3070\u308c\u305f\u5834\u5408\u306b\u3001valid_drivers\u306b\u57fa\u3065\u304d\u6307\u5b9a\u3055\u308c\u305f\u30af\u30e9\u30b9\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u751f\u6210\u3059\u308b\u3002Factory\u7684\u306a\u632f\u308b\u821e\u3044\u3092\u3059\u308b\u3002<\/p>\n
cache\u306e\u57fa\u672c\u7684\u306a\u8a2d\u5b9a\u3092\u8a18\u8ff0\u3057\u307e\u3059\u3002adapter\u306ffile\u3001apc\u3001memcached\u3001dummy\u306e\u3044\u305a\u308c\u304b\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n
\r\n< ?php\r\n$config = array(\r\n 'adapter' => 'memcached',\r\n);\r\n<\/pre>\napplication\/config\/memcached.php<\/span><\/h3>\n\u4e0a\u8a18\u306ecache.php\u3067memcached\u3092\u6307\u5b9a\u3057\u305f\u5834\u5408\u306b\u3001system\/libraries\/Cache\/drivers\/Cache_memcached.php<\/strong>\u306e\u521d\u671f\u5316\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002$config\u5909\u6570\u306b\u9023\u60f3\u914d\u5217\u5f62\u5f0f\u3067\u30ad\u30e3\u30c3\u30b7\u30e5\u30b5\u30fc\u30d0\u306e\u8b58\u5225\u5b50\u3068\u8a2d\u5b9a\u60c5\u5831\u3092\u8a18\u8ff0\u3057\u307e\u3059\u3002\u4e0b\u8a18\u306e\u4f8b\u3067\u306fcache1\u3001cache2\u3092\u6307\u5b9a\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u3082\u3061\u308d\u3093\u4e00\u3064\u3067\u3082\u554f\u984c\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n\r\n< ?php\r\n$config = array (\r\n 'cache1' => array(\r\n 'hostname' => '127.0.0.1',\r\n 'port' => 11211,\r\n 'weight' => 1,\r\n ),\r\n 'cache2' => array(\r\n 'hostname' => '127.0.0.1',\r\n 'port' => 11212,\r\n 'weight' => 1,\r\n ),\r\n);\r\n<\/pre>\n\u3061\u306a\u307f\u306bmemcached\u3092\u6307\u5b9a\u3057\u3066memcached.php\u3092\u4f5c\u6210\u3057\u306a\u304b\u3063\u305f\u5834\u5408\u3001\u6b63\u5e38\u306b\u52d5\u4f5c\u3057\u307e\u305b\u3093\u306e\u3067\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u3053\u308c\u306f\u3001\u5b9f\u88c5\u30d0\u30b0\u3060\u3068\u601d\u308f\u308c\u307e\u3059\u3002\u5177\u4f53\u7684\u306b\u306f\u3001system\/libraries\/Cache\/drivers\/Cache_memcached.php<\/strong>\u306e\u4e0b\u8a18\u306e\u90e8\u5206\u306b\u73fe\u308c\u308b$this->_default_options\u3068\u3044\u3046\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u8a2d\u5b9a\u304c\u4e00\u5ea6\u3082\u521d\u671f\u5316\u3055\u308c\u3066\u3044\u306a\u3044\u70ba\u3067\u3059\u3002<\/p>\n\r\n \/**\r\n * Setup memcached.\r\n *\/\r\n private function _setup_memcached()\r\n {\r\n \/\/ Try to load memcached server info from the config file.\r\n $CI =& get_instance();\r\n if ($CI->config->load('memcached', TRUE, TRUE))\r\n {\r\n if (is_array($CI->config->config['memcached']))\r\n {\r\n $this->_memcache_conf = NULL;\r\n\r\n foreach ($CI->config->config['memcached'] as $name => $conf)\r\n {\r\n $this->_memcache_conf[$name] = $conf;\r\n } \r\n } \r\n }\r\n \r\n $this->_memcached = new Memcached();\r\n\r\n foreach ($this->_memcache_conf as $name => $cache_server)\r\n {\r\n if ( ! array_key_exists('hostname', $cache_server))\r\n {\r\n $cache_server['hostname'] = $this->_default_options['default_host'];\r\n }\r\n \r\n if ( ! array_key_exists('port', $cache_server))\r\n {\r\n $cache_server['port'] = $this->_default_options['default_port'];\r\n }\r\n \r\n if ( ! array_key_exists('weight', $cache_server))\r\n {\r\n $cache_server['weight'] = $this->_default_options['default_weight'];\r\n }\r\n \r\n $this->_memcached->addServer(\r\n $cache_server['hostname'], $cache_server['port'], $cache_server['weight']\r\n );\r\n }\r\n }\r\n<\/pre>\n\u8aac\u660e\u304c\u9577\u304f\u306a\u308a\u307e\u3057\u305f\u304c\u3001\u5b9f\u969b\u306b\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u306a\u3069\u3067\u5229\u7528\u3059\u308b\u969b\u306b\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n
\r\n$this->load->driver('cache');\r\n$this->cache->save('hoge', 'foo', 3600);\r\n$foo = $this->cache->get('hoge');\r\n<\/pre>\n\u30b7\u30b9\u30c6\u30e0\u5168\u4f53\u3067Cache\u30c9\u30e9\u30a4\u30d0\u3092\u30ed\u30fc\u30c9\u3057\u305f\u304b\u3063\u305f\u306e\u3067\u3001autoload\u51fa\u6765\u306a\u3044\u304b\u30bd\u30fc\u30b9\u3092\u8ffd\u3063\u3066\u307f\u305f\u306e\u3067\u3059\u304c\u3001\u3067\u304d\u306a\u3044\u3088\u3046\u3060\u3063\u305f\u306e\u3067\u3001CI_Controller\u3092\u7d99\u627f\u3057\u3066\u72ec\u81ea\u306e\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u3092\u4f5c\u6210\u3057\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306b__construct\u306e\u4e2d\u3067\u30ed\u30fc\u30c9\u3059\u308b\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n
application\/core\/MY_Controller.php<\/span><\/h3>\n\r\nclass MY_Controller extends CI_Controller {\r\n function __construct() {\r\n parent::__construct();\r\n $this->load->driver('cache');\r\n }\r\n}\r\n<\/pre>\n\u3053\u306e\u30ad\u30e3\u30c3\u30b7\u30e5\u306e\u6a5f\u69cb\u3067\u3059\u304c\u3001\u4eca\u4ed5\u4e8b\u3067\u4f7f\u3063\u3066\u3044\u308b1.7.3\u306b\u3082backport\u51fa\u6765\u306a\u3044\u304b\u8abf\u3079\u3066\u3044\u307e\u3059\u3002\u51fa\u6765\u308b\u3088\u3046\u3067\u3042\u308c\u3070\u30d1\u30c3\u30c1\u3067\u3082\u4f5c\u6210\u3057\u3088\u3046\u304b\u306a\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n
\u4eca\u56de\u3001Cache\u5468\u308a\u306e\u30bd\u30fc\u30b9\u3092\u8aad\u3093\u3067\u3044\u3066CI_Driver_Library\u3001CI_Driver\u306e\u30bd\u30fc\u30b9\u304c\u975e\u5e38\u306b\u9762\u767d\u304f\u3001\u7279\u306bCI_Driver\u306e\u90e8\u5206\u3067\u7279\u5b9a\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306e\u30e1\u30bd\u30c3\u30c9\u3001\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u30de\u30b8\u30c3\u30af\u30e1\u30bd\u30c3\u30c9\u7d4c\u7531\u3067CI_Driver\u3092\u7d99\u627f\u3057\u305f\u30af\u30e9\u30b9\u304b\u3089\u547c\u3079\u308b\u3088\u3046\u306b\u30a2\u30bf\u30c3\u30c1\u3057\u3066\u3044\u308b\u90e8\u5206\u3067\u3057\u305f\u3002\u81ea\u5206\u3067\u3082\u6b63\u3057\u304f\u4f7f\u3048\u308b\u5834\u9762\u306b\u906d\u9047\u3059\u308c\u3070\u662f\u975e\u4f7f\u3063\u3066\u307f\u305f\u3044\u6a5f\u69cb\u3067\u3057\u305f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
CodeIgniter 1.x\u306e\u9803\u306e\u30ad\u30e3\u30c3\u30b7\u30e5\u3068\u3044\u3046\u3068\u30da\u30fc\u30b8\u30ad\u30e3\u30c3\u30b7\u30e5\u3067\u3057\u305f\u304c\u3001CodeIgntier2\u304b\u3089Memcache\u7b49\u3092\u5229\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u3001\u30da\u30fc\u30b8\u30ad\u30e3\u30c3\u30b7\u30e5\u3060\u3051\u3067\u306a\u304f\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30ad\u30e3\u30c3\u30b7 … \u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":2,"featured_media":9223372036854775807,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1980],"tags":[850],"_links":{"self":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/posts\/5454"}],"collection":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/comments?post=5454"}],"version-history":[{"count":0,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/posts\/5454\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/media\/9223372036854775807"}],"wp:attachment":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/media?parent=5454"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/categories?post=5454"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/tags?post=5454"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}