This article describes how to install and configure MemCache in the MacOS PHP environment. MemCache is a distributed high-speed cache system. For more information, see
Install server
Memcache is the project name, while the resident process on the server is memcached (linux daemon generally adds d to the end ). You can use brew in OSX to quickly install memcache:
$ sudo brew install memcached
Memcache dependencies: openssl and libevent are automatically downloaded and installed.
After the installation is complete, run the following command to start:
$ sudo memcached -m 32 -p 11211 -d
Install php extension
Before using php to operate memcache, you need to install php Extensions. for php extensions, you can select memcache and memcached. here, the classic version of memcache is installed. Select a version from here to download the source code compressed package, decompress the package, go to the source code directory, and execute:
$ sudo phpize
Phpize is a script used to compile php extensions outside the compiled php, and generate files such as configure and make. Sometimes an error occurs when you execute this command:
Cannot find autoconf. Please check your autoconf installation and the$PHP_AUTOCONF environment variable. Then, rerun this script.
If the dependency is missing, install it or use brew:
$ Sudo brew install autoconf
After phpize is complete, execute the following commands in sequence to compile and install phpize:
$ sudo ./configure$ sudo make$ sudo make install
The compiled memcache. so is generally installed in the following Directory:
Installing shared extensions: /usr/lib/php/extensions/no-debug-non-zts-xxxxxx/
In this way, you can configure this extension in php. ini:
extension=/usr/lib/php/extensions/no-debug-non-zts-xxxxxx/memcache.so
Open the phpinfo () page to check whether memcache has been successfully loaded:
Set yii
In this way, you can use memcache directly in php. here we will not go over it. if you use memcache in yii, you need to add a component:
'components'=>array( 'cache'=>array( 'class'=>'CMemCache', 'servers'=>array( array( 'host'=>'127.0.0.1', 'port'=>11211 ) ), ),...
For more information about yii configuration, see its documentation. Finally, use memcache in yii:
Yii::app()->cache->set('key1','value1');Yii::app()->cache->get('key1');
Memcached usage example
Adding memcached to the code that is purely used for database query is very simple. suppose this is the original code:
function get_foo (int userid) { result = db_select("SELECT * FROM users WHERE userid = ?", userid); return result;}
After the memcached Cache mechanism is added:
function get_foo (int userid) { result = memcached_fetch("userrow:" + userid); if (!result) { result = db_select("SELECT * FROM users WHERE userid = ?", userid); memcached_add("userrow:" + userid, result); } return result;}
The above program will first check whether there is userrow: userid data in memcached. If yes, it will directly return the results. if no data exists, it will query the database and put the results in memcached.
When the database data is updated when memcached already has Cache information, the above program will catch the old data, which is a problem of Cache coherency. One solution is to update the information in memcached when updating the database:
function update_foo(int userid, string dbUpdateString) { result = db_execute(dbUpdateString); if (result) { data = createUserDataFromDBString(dbUpdateString); memcached_set("userrow:"+userid, data); }}