Php5.4 distributed cache for memcache (installation and configuration in Windows 7)
I. Install memcachemcached on windows7 installation problem now installation package: http://www.jb51.net/softs/44843.html memcache installation package error: run the cmd command to D: \ webEve \ memcached (decompress directory after download) run memcached.exe-d install and report the error "failed to install service or service already installed". Solution: install the website administrator ID on www.bkjia.com. First, find the original file of cmd.exe and right-click it as an administrator, next, let's get it right (users in win7 are really troublesome ).
Install Memcache in Windows: 1. download the stable version of memcache for windows and decompress it to a disk. For example, on D: \ webEve \ memcached2. on the terminal (that is, the cmd command interface), enter 'd: \ webEve \ memcached \ memcached.exe-d install 'install 3. enter 'd: \ webEve \ memcached \ memcached.exe-D start' to start. NOTE: in the future, memcached will be used as a windows Service and will be automatically started every time it is started. In this way, the server has been installed. 4. download php_memcache.dll. Find the corresponding php version file by yourself. in php. ini adds a line 'extension = php_memcache.dll '6. restart Apache and check phpinfo. If memcache exists, the installation is successful! Basic settings of memcached:-p listening port-l connection IP address, the default value is local-d start to start memcached service-d restart to restart memcached service-d stop | shutdown to disable the running memcached service-d install memcached service www.bkjia.com-d uninstall to uninstall memcached service- run as u (only valid when running as root) -MB maximum memory usage, in MB. By default, an error is returned when 64MB-M memory is used up, instead of deleting the maximum number of concurrent connections in item-c. The default value is 1024-f, and the default value is 1.25-n, key + value + flags are 48-H display help by default
Php_memcached.dll csdn resource II. Install memcache for memcached extension in php. Configure php_memcached.dll csdn resource in php. paste and enable extension in ini and put this file into the ext extension file. Now memcache is ready for use. Let's try it!
Header ("Content-type: text/html; charset = UTF-8 ");
$ Host = '127. 0.0.1: 80801 ';
$ User = 'root ';
$ Passwd = '';
$ Db = 'test ';
$ Conn = mysql_connect ($ host, $ user, $ passwd );
Mysql_select_db ($ db, $ conn );
Mysql_query ("set names utf8", $ conn );
$ SQL = 'select * from syl_rollback order by id desc ';
$ Result = mysql_query ($ SQL, $ conn );
While ($ row = mysql_fetch_array ($ result, MYSQL_ASSOC )){
$ Test_key [] = $ row;
}
Echo"
";
var_dump($test_key);
echo "
";
Echo" ";
$ SQL = md5 ($ SQL );
$ Mem = new Memcache; // instantiate a memcache object
$ Mem-> connect ("192.168.1.134", 11211); // connect to the local ip address. 11211 is the memcache port number.
$ Mem-> set ($ SQL, $ test_key, MEMCACHE_COMPRESSED, 600); // store the $ test_key array in $ SQL. Note: memcache cached data, if there is a survival time, it will expire in a small period by default. Of course, you can set an expiration time by yourself. 600 is the survival time corresponding to the $ SQL key value.
Echo"
";
Print_r ($ mem-> get ($ SQL); // obtain the value of $ SQL
Echo"
";
?>
Example 2:
Header ("Content-type: text/html; charset = utf8 ");
$ SQL = 'select * from syl_rollback order by id desc ';
$ SQL = md5 ($ SQL );
$ Mem = new Memcache;
$ Mem-> connect ("192.168.1.134", 11211 );
// $ Mem-> flush (); // clears the cached data
$ Mem-> add ("hehe", "memcahce"); // set the key value to memcache.
Echo $ mem-> get ("hehe ")," "; // Read the hehe Value
Echo"
";
print_r($mem->get($sql));
echo "
";
?>
Now, you can basically use memcache. How can we apply it to your website? First, encapsulate a class instantiated by memcache and then call it. When the website obtains data, it reads data from the advanced memcache cache. If it reads data, it displays the data on the page, in this case, database operations are not performed, saving a lot of time! If no data is read from the cache, you can obtain the data from the database!
That is to say, you need data first with memcache. If you do not want to read data from the database, read the data and store it in memcache immediately.