In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
one。 What is memcache?
Memcache is a distributed cache system, which can significantly improve the access speed of some large-scale websites that need to access the database frequently. It is a set of open source software. It maintains a unified huge hash table in memory, which can be used to store data in various formats, including images, videos, files and database retrieval results. To put it simply, the data is called into memory and then read from memory, thus greatly improving the reading speed. The server-side main program memcached runs in one or more servers as a daemon (listening) and can receive connections and operations from the client at any time, up to a maximum of 200 simultaneous connections. Memcache supports both TCP and UDP protocols.
II. Work flow of memcache
Memcache first checks whether the client's request data is in memcached, and if so, returns the request data directly without any operation on the database; if the requested data is not in memcached, check the database, return the data obtained from the database to the client, and cache a copy of the data in memcached (the memcached client is not responsible and needs to be explicitly implemented by the program) Update the data in memcached each time you update the database to ensure consistency; when the memory space allocated to memcached is used up, the LRU (Least Recently Used, least recently used) strategy plus expiration policy is used, and the invalid data is replaced first, and then the recently unused data is replaced.
3. Memcached and redis
Redis is an open source log-based, Key-Value database. Compared with memcache, it has the following differences:
All data in 1.memcached is stored in memory all the time, while redis is not
2.memcached only supports simple key/value type data, while redis supports the storage of list, set, hash and other data structures.
3.redis supports data backup, that is, master-slave data replication.
4.redis supports data persistence. You can save the data in memory on disk and load it again when you restart it.
4. Simple application of Memcached in php
System environment: rhel6 x32 selinux and iptables disabled
Host role: 172.25.46.1 nginx php memcached
The installation of nginx and php is no longer explained in this lab, please refer to my previous article "Dsicuz Forum based on lnmp Architecture"
Get the source package and decompress it
# tar zxf memcache-2.2.5.tgz
Cd memcache-2.2.5
Configure environment variables
# vi ~ / .bash_profile
PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin
# source ~ / .bash_profil
Expand the php module with phpzie, that is, add the memcached module to the php
# phpize
Configuring for:
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525
#. / configure-enable-memcache
# make & & make install
Installing shared extensions: / usr/local/lnmp/php/lib/php/extensions/no-debug-non-zts-20100525/
# cd / usr/local/lnmp/php/lib/php/extensions/no-debug-non-zts-20100525/
# ll memcache.so
-rwxr-xr-x 1 root root 259040 Jan 16 14:00memcache.so
Memcached caches for php
# cd / usr/local/lnmp/php/etc
# vim php.ini
844; extension=msql.so
845 extension=memcache.so
Write php function
# cd / usr/local/lnmp/nginx/html
# vim index.php
Enable nginx and php services
# nginx
# nginx-s reload
# / etc/init.d/fpmrestart
At this point, you can use a browser to test the php page. I visited 172.25.46.1/index.php in the experiment, as shown in the figure:
Install and start the memcached service
# yum install-y memcached.x86_64
# / etc/init.d/memcached start
Test port
# telnetlocalhost 11211
Trying:: 1...
Connected to localhost.
Escape character is'^]'.
Stats # # View memcached status
STAT pid 4664
STAT uptime 72
STAT time 1452925893
STAT version 1.4.4
.
STAT curr_items 0
STAT total_items 0
STAT evictions 0
END
Quit
Put the memcached monitoring page in the release directory of nignx
# cd memcache-2.2.5
# cp memcache.php / usr/local/lnmp/nginx/html/
# cd / usr/local/lnmp/nginx/html/
# vim memcache.php
$VERSION='$Id: memcache.php,v 1.22008 Universe 09 mikl Exp 11 19:21:06 mikl Exp $'
Define ('ADMIN_USERNAME','memcache'); / / Admin Username
Define ('ADMIN_PASSWORD','westos'); / / Admin Password
Define ('DATE_FORMAT','Y/m/d HRV iRV')
Define ('GRAPH_SIZE',200)
Define ('MAX_ITEM_DUMP',50)
$MEMCACHE_SERVERS [] = '172.25.46.1 11211 add more as an array # listens on port 11211 of this machine
# $MEMCACHE_SERVERS [] = 'mymemcache-server2:11211'; / / add more as an array
Note: in this lab, you need to comment out the last two lines when editing this file. I was able to access the monitoring page at the beginning of the experiment, but could not display the monitoring screen.
Write a test page
# vim test.php
At this point, you can use the browser to access the memcached monitoring page. I visited 172.25.46.1/memcache.php in the experiment, and the user and password are edited by the file memcache.php, as shown in the figure:
On the monitoring page, you can see that the cache usage is 0 and the hit rate is 50%:
Visit the test.php page to test memcached. In the experiment, I visited 172.25.46.1/test.php, as shown in the figure:
Refresh this page and observe that the memcached monitoring page changes. The cache usage is 119.0 and the hit rate is 85.7%, as shown in the figure:
This is a simple application of memcached in php, if you find problems in the blog post or if you have better suggestions and comments, please give me feedback in time ~ Thank you for your visit!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.