Explanation of memcache in php

Source: Internet
Author: User
For details about memcache in php, memcahe is a distributed storage system, which is mainly reflected in the absence of connection between servers and the maintenance of a hash table through memory, the hash table is mainly stored in key-value pairs, which is a cs software package.

Key

Value

Mystr

"Abc"

Myarr

Array ("aa", "cc ");

Object

Object value


Working Principle

Memcache software, memcached

Memcached runs in one or more services as a daemon and operates and connects to the customer service end at any time.


About memcache installation

Installation in linux

Libevent-based events
Linux
When installing libevent
./Configure-with-libevent =/usr
Make & make install

Install memcached
./Configure-with-libevent =/usr

Make & make install

Start Memcahced-d-m 128-l 192.168.1.111-p 11211-u root
Stop: kill 'cat/tmp/memcached. Pi ';
Killall memcached

Windows
Memcahced.exe-d install [uninstall]
Memcached.exe-d-m 50-l 127.0.0.1-p 11211 start


, Memcached server management (start)
Basic settings of memcached:
-P listening port
-L connected IP address. the default value is local
-D start: start the memcached service.
-D restart: restart the memcached service.
-D stop | shutdown the running memcached service
-D install the memcached service
-D uninstall memcached service
-U runs as the identity (only valid when running as root)
-MB maximum memory usage, in MB. The default value is 64 MB. The maximum value is 2 GB.
-An error is returned when M memory is used up, instead of deleting items.
-C: maximum number of simultaneous connections. the default value is 1024.
-F block size growth factor, default value: 1.25
-N: Minimum allocation space. the default value of key + value + flags is 48.
-H Show Help


VII. how to traverse memcache

8. use Memcached in a PHP program
A. install Memcache extension in PHP

Process-oriented

Object-oriented approach

Where does B use memcache in PHP?

1. The data read from the database (select) is processed using memcache

II. Use in session control sessions

C instance

9. Memcache security (preventing access from others)

1. intranet
2. set a fire wall
Iptables-a input-p tcp-s 192.168.1.111-dport 11211-j ACCEPT
Iptables-a input-p udp-s 192.168.1.111-dpost 11211-j ACCEPT


Install. then start memcached-d start

Command

Description

Example

Get

Reads a value

Get mykey

Set

Set a key unconditionally

Set mykey 0 60 5

Add

Add a new key

Add newkey 0 60 5

Replace

Overwrite existing key

Replace key 0 60 5

Append

Append data to existing key

Append key 0 60 15

Prepend

Prepend data to existing key

Prepend key 0 60 15

Incr

Increments numerical key value by given number

Incr mykey 2

Decr

Decrements numerical key value by given number

Decr mykey 5

Delete

Deletes an existing key

Delete mykey

Flush_all

Invalidate specific items immediately

Flush_all

Invalidate all items in n seconds

Flush_all 900

Stats

Prints general statistics

Stats

Prints memory statistics

Stats slabs

Prints memory statistics

Stats malloc

Print higher level allocation statistics

Stats items


Stats detail


Stats sizes

Resets statistics

Stats reset

Version

Prints server version.

Version

Verbosity

Increases log level

Verbosity

Quit

Terminate telnet session

Quit

Pid

Memcache server process ID

Uptime

Number of seconds that the server has run

Time

Current unix timestamp of the server

Version

Memcache version

Pointer_size

Pointer size of the current operating system (32-bit system is generally 32bit)

Rusage_user

Cumulative user time of the process

Rusage_system

Cumulative system time of processes

Curr_items

Number of items currently stored on the server

Total_items

Total number of items stored after Server startup

Bytes

Number of bytes occupied by items stored on the current server

Curr_connections

Number of currently opened connections

Total_connections

Number of connections that have been opened since the server is started

Connection_structures

Number of connections allocated by the server

Pai_get

Total get Command (get) requests

Performance_set

Total number of set command (save) requests

Get_hits

Total hits

Get_misses

Total number of missed hits

Evictions

Number of items deleted to get idle memory (the old items need to be deleted after the space allocated to memcache is full to get the space allocated to the new items)

Bytes_read

Total number of read bytes (number of request bytes)

Bytes_written

Total number of sent bytes (number of returned bytes)

Limit_maxbytes

Memory size (bytes) allocated to memcache)

Threads

Current thread count




Pid memcache server process ID
Number of seconds that the uptime server has run
Current unix timestamp of the time server
Version memcache version
Pointer_size pointer size of the current operating system (32-bit systems are generally 32bit)
Cumulative user time of the rusage_user process
Cumulative system time of the rusage_system process
Number of currently stored items on the curr_items server
Total number of items stored after total_items is started from the server
Bytes the number of bytes occupied by the current server storage items
Curr_connections current number of opened connections
Total_connections number of connections that have been opened since the server was started
Connection_structures server-assigned connection constructor count
Cmd_get command (get) Total number of requests
Cmd_set set command (save) Total number of requests
Total get_hits hits
Total number of get_misses missed
The number of items deleted by evictions to get idle memory (after the space allocated to memcache is used up, you need to delete the old items to get the space allocated to the new items)
Bytes_read total number of read bytes (number of request bytes)
Bytes_written total number of sent bytes (number of result bytes)
The memory size (in bytes) allocated to memcache by limit_maxbytes)
Threads current thread count


For more information, see "LXXXIV. Memcache Functions" in the PHP Manual.
Common Memcache object-oriented interfaces include:
Memcache: connect -- open a connection to Memcache
Memcache: pconnect -- open a persistent connection to Memcache
Memcache: close -- close a Memcache connection
Memcache: set -- save data to the Memcache server
Memcache: get -- extract data stored on the Memcache server
Memcache: replace -- replace a project that already exists on the Memcache server (similar to Memcache: set)
Memcache: delete -- delete a saved project from the Memcache server
Memcache: flush -- refresh the items saved on all Memcache servers (similar to deleting all saved items)
Memcache: getStats -- get the running status of the current Memcache server
Memcache: addServer -- add a server to a distributed server

Use php to operate memcache


$ Mem = new Memcache;

$ Mem-> connect ("localhost", 11211 );

/*
* Note:
* 1. install the same project twice. the key must have a prefix.
*
*
*/

$ SQL = "select * from shops ";
$ Key = substr (md5 ($ SQL), 10, 8 );

$ Data = $ mem-> get ($ key );

If (! $ Data ){

$ Mysqli = new mysqli ("localhost", "root", "123456", "xsphpdb ");

$ Result = $ mysqli-> query ($ SQL );

$ Data = array ();

While ($ row = $ result-> Fetch_assoc ()){
$ Data [] = $ row;
}

$ Result-> free ();
$ Mysqli-> close ();

$ Mem-> set ($ key, $ data, memcache_compute, 3600 );

Echo $ SQL;

}
Echo'

';
print_r($data);
echo '
';

$ Mem-> close ();

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.