但行好事,莫问前程

挖了太多坑,一点点填回来

安装phpredis扩展

linux, php, redis, ubuntu

redis官方推荐的PHP扩展有两个——Predisphpredis。phpredis是使用 C 编写的 PHP module ,速度应该会比较快,而且 CakePHP 使用的 redis 库默认就是 phpredis ,先安装这个试试看。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ sudo apt-get install php5-dev

$ git clone git://github.com/nicolasff/phpredis.git

$ cd phpredis
$ phpize
$ ./configure
$ make
$ sudo make install

$ sudo -s
$ echo "extension=redis.so" > /etc/php5/conf.d/redis.ini
$ exit

$ sudo service apache2 reload

之后phpinfo一下就能看到phpredis的扩展信息。

redis
Redis Support enabled
Redis Version 2.2.3

在CakePHP中做测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class TestsController extends AppController {
    public $uses = array();

    public function index() {
        $this->autoRender = false;

        $r = new Redis();
        $r->connect('localhost');

        debug($r->keys('*c*'));

        foreach ($r->keys('*c*') as $index => $key) {
            debug($key);
            debug($r->get($key));
        }
    }
}

输出结果:

/app/Controller/TestsController.php (line 11)
array(
(int) 0 => ‘counter:rand:000000000000’,
(int) 1 => ‘caiknife:name’
)
/app/Controller/TestsController.php (line 14)
‘counter:rand:000000000000’
/app/Controller/TestsController.php (line 15)
‘110000’
/app/Controller/TestsController.php (line 14)
‘caiknife:name’
/app/Controller/TestsController.php (line 15)
‘Cai’

Have a nice day!