PHP Operation Memcache

Source: Internet
Author: User
Tags delete key scalar

first, The basic introduction of Memcache

is a high-performance distributed Memory Object caching System.

Compare with MySQL to understand Memcache

(1 ) The software is a c/s architecture,

(2 ) MySQL where is the data stored? is stored inside the Disk.

memcache the data is stored in Memory.

(3 ) MySQL the data storage is, by the database, the data table, the data record, the storage data is according to the table structure carries on the Storage.

memcache the data storage format Is: Key/value format, to store the equivalent of a two-column hash table in Memory. an associative array similar to the inside of php

key ( keys Name) value

second, the basic principle:

For example, There is a query system that queries the information of a student:

third, Install the Software

(1) prepare the software, in order to facilitate management, copy the file to the same directory as Apache or MySQL or php.

(2) Start the Installation:

Install the software as a service in Windows for easy Management.

Steps:

In the Administrator's way, open cmd (command prompt) and go to the directory where the memcached file is Located.

You can use Memcahced.exe–h to view Help COMMANDS.

Memcache–d Install to start installation:

After the installation is complete, to finish starting the service,

Memcached–d start

To see if the startup is successful, you can view it through the service options in computer Management.

The default port number for Memcache is 11211, which can be viewed by looking at the port to determine if the service is turned On.

Use the Netstat–an command to view the services that are listening on the current computer.

Memcached–d stop stops the Memcache service.

(3) at the time of installation, the corresponding parameters are Set.

such as settings, the size of the Cache.

- P ports to listen on
-l
connected IP address, default is native
-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 Run as (only valid when running as Root)
- m maximum memory usage, per MB . Default 64MB

-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

-V output warnings and error messages.

-VV request and return information for the print client

-i Print copyright information for memcached and libevent

Iv. software operation 1, directly using Telnet remote protocol connection (similar to the MySQL client's black Window) (learn)

Open cmd for input:

Telnet IP Address (domain name) port number

(1) setting data, adding data to Memcache

Add Data:

Grammar:

Add key name 0|1(whether compressed) The length of the cache period value [ Press ENTER to enter the data to be added]

0 indicates no compression, 1 indicates that you want to compress

Cache cycles: units are seconds,

The length of the value: the unit of length is related to the client, and here is the Byte.

Note: if the key already exists, the add Fails.

To modify the Data:

Replace

The syntax format and add are the Same.

Note: if the key does not exist, the modification fails and only the key that already exists can be modified.

Setting Data:

The syntax for the SET command and add is the Same.

If the SET command is used, if the key already exists, the modification is completed and the add operation if the key does not exist.

(2) Data acquisition

Get Key Name

(3) Delete Data

Delete key Name

Flush_all clears all the data,

You can set an expiration date,

Flush_all seconds, and after how many seconds, clear all the Keys.

(4) Data Append

Append append data after the original value

Preappend appends data to the previous Value.

(5) a command to view the current status:

The stats command enables you to obtain, how many times a get query is executed, how many times you can use a GET query to data, how many times you can use a Get query to fail, calculate the hit rate,

calculates the number of times that the Hit-rate get was successfully queried,/get the total number of queries, and uses the hit rate to determine Memcache service Performance.

2, use PHP program as client, connect memcache to install Memcache Extension.

(1) to prepare the memcache PHP extension, when installing the extension, and PHP version of the Same.

(2) copy the corresponding extension to the PHP installation directory ext directory.

(3) Open the php.ini file, to enable the extension, after completion, to restart apache,

(4) test, Whether the installation extension is successful, using phpinfo (); function

3, PHP Operation Memcache.

(1) Instantiate an Object.

$mem = new Memcache ();

(2) connect the Memcache and use the Connect Function.

$mem->connect (host name, Port Number)

$mem->add/set (key name, value, whether compressed, cache Time)

$mem->get (key Name)

(3) discuss the type of data stored.

PHP Data type: How scalar data types are Stored.

The scalar type of PHP is stored in the MEMCAC he store in the form of a string.

PHP Data type: The form of an array.

When an array is stored, the array is serialized, stored in memcache, and automatically deserialized when the array data is Fetched. The serialization and deserialization of arrays is done with the add and get functions without our own Intervention.

4, the use of the introduction Case.

According to the data taken out by a sql, the data fetched by SQL is cached into memcache, and the next time the SQL statement is manipulated, it is obtained directly from Memcache.

5. A typical case application

Vii. memcache Details Discussion 1, Cache period (validity)

There are two ways to set the cache cycle:

If the cache period is set to 0, it indicates that this data never expires

First use: seconds, maximum no more than 30 days, that is, 2.592 million seconds,

Second use: use the timestamp, which is the time point to which the timestamp is. Like we're going to cache a data for 60 days,

$mem->add ($key, $value, 0,time () +60*24*3600)

2, Lazy deletion,

That is, the cached data, after expiration, is not automatically deleted when the get data, the view is not expired, if expired, then delete the Data.

Memcache does not open a process to monitor whether the key is out of date, save Memcache resources, and also save the CPU.

3, Least recentlyused (LRU)

The least recently used principle,

The current stored data in the memcache inside the space is already full, then the memcache will be inactive (get or set number of Less) data (whether or not expired) clear, let the new data to be stored in.

4. Security

Memcache positioning is to do the data cache, not the persistent storage of data, so do not do some user verification, the purpose of improving the connection speed, to play their unique features.

Memcahe The software itself does not provide some security mechanisms, if you want to limit the use, it can only be resolved at the operating system level, such as window system, the installation of firewall software, under Linux use Iptables,seclinux (security mechanism)

5, memcache more suitable for storing what Data.

Characteristics of the stored Data:

Access is more frequent,

The security is not high, the loss does not matter,

Modify more frequent data, such as the online status of some users,

PHP Operation Memcache

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.