LoginSignup
5
4

More than 5 years have passed since last update.

CakePHP3でRedis使ってみた

Posted at

手順

インストール(CentOS6.6)

yum -y install redis
yum -y install  --enablerepo=remi --enablerepo=remi-php70 php-redis

php.ini修正

php.ini
extension=redis.so

apache再起動とredis-server起動

sudo service httpd restart
redis-server

CakePHP3のCacheをRedisに修正

config/app.php
         'default' => [
             'className' => 'Redis',
             'prefix' => 'shinderuman_', // defaultは'cake_'
         ],

実装

単純なKeyValue

HogeController.php
         $key = 'bar';
         $foo = 'abc';
         Cache::write($key, $foo);
         $cache = Cache::read($key);
         var_dump($cache); // 'abc'

SimpleXMLElement保存できないかしら

HogeController.php
         $key = 'foo';
         $xml = '<?xml version="1.0" encoding="utf-8"?><project name="hogehoge" default="hogehoge"></project>';
         $foo = new \SimpleXMLElement($xml);
         Cache::write($key, $foo); // やっぱりserializeしようとして怒られるorz(Serialization of 'SimpleXMLElement' is not allowed)

cliからも取得してみる

~% redis-cli
redis 127.0.0.1:6379> get shinderuman_bar
"s:3:\"abc\";"
redis 127.0.0.1:6379> get shinderuman_foo
(nil) // 当然いない

参考資料

5
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4