Memcache Introduction
What is memcached?
Memcached is a high-performance, distributed memory object caching system developed by Danga Interactive,
Used to reduce database load and increase access speed in dynamic applications.
What can memcached cache?
By maintaining a unified, huge hash table in memory, memcached can be used to store data in a variety of formats,
Includes images, videos, files, and results of database retrieval.
Memcached, are you quick?
Very fast. Memcached uses libevent (if you can, use Epoll under Linux) to balance any number of open links,
Use non-blocking network I/O to implement reference counting for internal objects (so objects can be in a variety of states for multiple clients).
Use your own page block allocator and hash table, so virtual memory is not fragmented and the time complexity of virtual memory allocation is guaranteed to be O (1).
Danga Interactive developed memcached for the speed of Danga Interactive.
Features of memcached
The memcached cache is distributed and can be accessed by multiple users on different hosts at the same time.
Therefore, it solves the limitation that shared memory can only be used on a single machine, and it does not occur when using database to do similar things.
Disk overhead and blocking occur.
Cache data:
1. Cache data in memory 2. Data form with Key->value structure 3. Very poor security.
Memcache Installation
Operations under Windows
1 Download the Memcached-1.2.1-win32.zip online. Unzip and put under a disk, for example in c:\memcached
2 under Terminal (CMD):
D:\appserv>memcached.exe-d Install Installation
d:\appserv>memcached.exe-d Uninstall Uninstall
d:\appserv>memcached.exe-d start
D:\appserv>memcached.exe-d Stop Stop
Memcached.exe-h get all the Help
3 After starting the connection:
d:\appserv> telnet 127.0.0.1 11211--Connect memcache Port 11211
Quit with quit.
4 Additional command parameters:
Start Memcache Common parameters
-P <num> Set port number (default is not set to: 11211)
-U <num> UDP Listening port (default: 11211, 0 o'clock off)
-L <ip_addr> Bind address (default: All allowed, regardless of internal or external network or native replacement IP,
There is a security risk, if set to 127.0.0.1 can only be native access)
-D Standalone Process run
...-D start memcached service
...-d Restart Restart memcached service
...-d Stop|shutdown Close the running memcached service
...-D install memcached service
...-d uninstall uninstall memcached service
-U <username> bindings use specified for running processes <username>
-M <num> allowable maximum memory usage, Unit m (default: MB)
-P <file> writes the PID to the file <file>, which allows for fast process termination behind and needs to be used with-D
-M running out of memory and returning an error instead of deleting an item
-c Maximum number of simultaneous connections, default is 1024
-F Block size growth factor, default is 1.25
-N Minimum allocated space, key+value+flags default is 48
-H Display Help
PHP Open memcache Module
1.phpinfo () detects Memcache expansion
2.php Extension Catalog Extension_dir
Php_memcache.dll placed in the PHP extensions directory
3.php Configuration file Modification
Extension=php_memcache.dll
4. Restart Apache Service
Installation operation under Linux:
2.1 #为php环境安装memcache扩展
First download the php7.0 supported expansion pack Pecl-memcache-php7.zip
Address: Https://github.com/websupport-sk/pecl-memcache/archive/php7.zip
1. Upload the Pecl-memcache-php7.zip file to the server/root
2. Enter this directory after extracting the files
Unzip Pecl-memcache-php7.zip
CD PECL-MEMCACHE-PHP7
3. Because there is no configure configuration environment file in the directory
Execute/usr/local/php/bin/phpize to prepare a compilation environment
4. Configuration: Add configuration
./configure--with-php-config=/usr/local/php/bin/php-config
5. Make && make install compilation and installation
6. A memcache.so file is generated when the installation is successful
/usr/local/php/lib/php/extensions/no-debug-non-zts-20151012/
7. Modify the PHP configuration file
Vim/usr/local/php/etc/php.ini
Add the following information
722 lines extension_dir= '/usr/local/php/lib/php/extensions/no-debug-non-zts-20151012/';
Extension= "memcache.so";
8. Restart Apache Service (view Phpinfo ())
2.2 #安装memcache服务
memcached-1.4.4-3.el6.i686.rpm
A. Mount the ISO image file DVD1 first. Iso
mount/dev/sr0/mnt/cdrom/
B. Installing memcached
Yum-y Install memcached
C. adding Memcache Users
Useradd memcache
D. Start the Memcache service
Memcached-d-M 128-l 127.0.0.1-p 11211-u memcache
View ports
netstat–tunpl|grep:11211
Win+r opening a command-line window connection test
Telnet 192.168.185.252 11211
Attention:
Telnet is not an internal command when you enter the Telnet command
Windows systems, Control Panel, programs and features, turn on and off Windows features, tick the Telnet client
Stats memcache command: View current status
E. Setting up boot from
Chkconfig memcached on
F. How to kill the background process
Pkill memcached
Memcache related Operations
# # # #stats View status
STAT PID 1552 Process ID of the service process
STAT uptime 3792 The elapsed time, in seconds, from the start of the service to the current.
The time, in seconds, of the current system of the host on which the STAT 1262517674 server resides.
Version of the STAT version 1.2.6 component. Here is the 1.2.6 I am currently using.
STAT Pointer_size 32 The size of the pointer to the host operating system of the server, typically 32 or 64.
STAT Curr_items 1 Indicates the number of all cached objects that are currently stored. Does not include objects that have been removed from the cache.
STAT Total_items 2 represents the number of objects that have been stored by the system from boot to current, including deleted objects.
STAT bytes 593 represents the storage space used by the system to store cached objects in bytes.
STAT Curr_connections 2 Indicates the number of connections that are currently open on the system.
STAT Total_connections 28 represents the total number of connections that have been opened by the system since the Memcached service started to the current time.
Set Write Cache
Set key name mark length of valid time data
Set Love 10 0 10
I love You
Key Name
Maximum of 250 characters
Mark
is a 16-bit unsigned integer that will be stored with the data that needs to be stored and returned when the client get data
Effective time
Unit is seconds
Data length
Unit is byte
The value of the data
Maximum no more than 1024kb
Get Get Cache
Get Key Name
Get Love
# # # #delete Delete Cache
Delete key Name
Delete Love
Flush_all Empty All Caches
# # #PHP与Memcache的使用
Instantiate Memcache
$memcache = new Memcache ();
Connection Memcache
$memcache->connect (' 127.0.0.1 ', ' 11211 ');
$memcache->addserver (' 127.0.0.1 ', ' 11211 ');
Save data
$memcache->set (' name ', ' Xiaohigh ', memcache_compressed,60);
Get Data
$memcache->get (' name ')
Clear All data
$memcache->flush ();
# # #PHP Memcahe class
Connect (IP, port) to a single memcache server
Addserver (IP, port) add a memcache server
Getextendedstats () Get all the server information in the connection pool
Getserverstatus (host, port) Gets the connection status of the specified server
Add (key, value, tag, expires) adds a piece of data
Replace (key, value, tag, expires) modifies a single piece of data
Set (key, value, tag, expires) Add or modify a piece of data
Get (key) get value by key
Delete (key) deletes a specified piece of data
Flush () Delete all
Close () Closes the connection
GetStats () Get Memcache server information return array
GetVersion () get m Emcache Server version number
Memcached Quick Start