Full-process parsing _php instance of installing configuration memcache in Mac OS PHP environment

Source: Internet
Author: User
Tags install php memcached zts yii

Install service side
Memcache is the name of the project, and the hosting process on the server is called memcached (Linux daemon is typically followed by a D). Using brew under OS X allows you to quickly install 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 extensions, PHP extensions have two options memcache and memcached, where the classic installation of the former. Choose from here a version download source code compression package, decompression, into the source directory after the implementation:

$ sudo phpize

Phpize is a script that helps you compile PHP extensions using the compiled PHP, to generate files such as configure, make, and so on. Sometimes executing this command will cause an error:

Cannot find autoconf. Please check your autoconf installation and the
$PHP _autoconf environment variable. Then, rerun this script.

Missing dependencies, 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

Compiled memcache.so are 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:


Open the Phpinfo () page to see if Memcache has been successfully loaded:

Set up Yii
This can actually be used directly in PHP to use memcache, here is not tired, if used in Yii, you need to add a component:

' Components ' =>array ('

  cache ' =>array ('
    class ' => ' Cmemcache ',
    ' servers ' =>array "(
      Array (
        ' host ' => ' ',
        ' Port ' =>11211),),

Refer to 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 simple to add memcached support to code that simply uses database queries, 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 caching 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 first to memcached check whether there are userrow:userid data, if there is a direct return to the results, if it does not exist then go to the database query, and put the results into the memcached.

In the memcached has already had the cache information to update the database data, the above procedure will catch the old data, this is belongs to the cache coherency problem. One way to solve this problem is to update the database while updating the information in the memcached:

function Update_foo (int userid, string dbupdatestring) {Result
  = Db_execute (dbupdatestring);
  if (result) {
    data = createuserdatafromdbstring (dbupdatestring);
    Memcached_set ("Userrow:" +userid, data);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.