Installing the service side
Memcache is the project name, and the hosting process on the server is called memcached (the Linux daemon typically adds a d in the back). Using brew under OSX can quickly install the memcache:
$ sudo brew install memcached
Memcache dependencies: OpenSSL and libevent are automatically downloaded and installed.
When the installation is complete, start with the following command:
$ sudo memcached-m 32-p 11211-d
Installing PHP Extensions
Before using PHP to operate memcache, you need to install PHP extension, PHP extension has two can choose Memcache and memcached, here install the former more classic. Choose a version from here to download the source code compression package, unzip, into the source directory after the execution:
$ sudo phpize
Phpize is a script used to compile PHP extensions to generate files such as configure, make, and so on, outside of compiled PHP. Sometimes executing this command will cause an error:
Cannot find autoconf. Please check your autoconf installation and THE$PHP_AUTOCONF environment variable. Then, rerun the this script.
If there is a lack of dependency, install it or use brew:
$ sudo brew install autoconf
Once the phpize is complete, the following commands are implemented to compile and install:
$ sudo./configure$ sudo make$ sudo make install
The compiled memcache.so is typically installed in the following directory:
Installing Shared extensions:/usr/lib/php/extensions/no-debug-non-zts-xxxxxx/
This allows you to configure this extension in php.ini:
Extension=/usr/lib/php/extensions/no-debug-non-zts-xxxxxx/memcache.so
Open the Phpinfo () page to see if the memcache has been loaded successfully:
Set Yii
In fact, it is possible to use memcache directly in PHP, where it is not tiring, if used in Yii, you need to add a component:
' Components ' =>array (' cache ' =>array ( ' class ' = ' Cmemcache ', ' servers ' =>array ( array ( ' host ' = ' 127.0.0.1 ', ' Port ' =>11211 ) ,) ,...
See its documentation for more yii configurations. Finally, use Memcache in Yii:
Yii::app ()->cache->set (' Key1 ', ' value1 '); Yii::app ()->cache->get (' Key1 ');
Memcached Use Example
It is easy to add code that uses purely database queries with memcached support, assuming this is the original code:
function Get_foo (int userid) { result = Db_select ("SELECT * from users WHERE userid =?", userid); return result;}
After adding the memcached cache mechanism:
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 go to memcached to check whether there is Userrow:userid data, if there is a direct return to the results, if not present, then to the database query, and put the results in memcached.
The above program will catch the old data, which is the problem of the cache coherency, after the data of the database has been updated in memcached. One way to resolve this is to update the information in the memcached while the database is being updated:
function Update_foo (int userid, string dbupdatestring) { result = Db_execute (dbupdatestring); if (result) { data = createuserdatafromdbstring (dbupdatestring); Memcached_set ("Userrow:" +userid, data);} }