2019-09-09 memcache


What is the cache it? ? ?
Cache is a temporary place to store data (frequently used data) of the
cache can be considered to be a large pool of data


First, the data cache
are talking about here is the database query cache data cache, each time the page is accessed, that they will first test whether the corresponding cache data exists, and if not, to connect to the database, get data, and the sequence of query results after saving the file, after the same query result obtained directly from the cache table or file.
Look at the most widely used example Discuz search function, the results of ID cache to a table, first search the cache table next time search for the same keywords.
For commonly used method, the associated multi-table when the contents of the schedule stored array to generate a field in the main table, when the array into a desired look, this advantage is a read-only table data disadvantage is two synchronization will be a lot more steps, the database is always a bottleneck, with the hard drive swap rate, this is the key point.
Second, the page cache
every time you visit the page, will first test whether the corresponding cache page file exists, if not, to connect to the database, get data, display the page and at the same time generate the cache page file, so when the next visit the page file on the role. (Template engine and some common web cache classes usually have this feature).
Third, the time-triggered cache
to check whether a file exists and the timestamp is less than set the expiration time, if the file modification timestamp than the current timestamp minus big expired timestamp, then use the cache, or update the cache.
Fourth, the contents of the cache is triggered
when the insert data or update data, the forced update the cache.
Fifth, static cache
are talking about here refers to the static cache is static, such as directly generating HTML or XML text file when an update is re-generated once, is not suitable for changing the page, this is not to say.
Above is the code-level solutions, and I direct CP other framework, do not bother to change the contents are similar, it is easy to do, and will be used together in several ways, but the following is a server-side caching scheme, non-code level, to have multi-party cooperation can do.
Sixth, the memory cache

Memcached is a high-performance, distributed memory object caching system, for reducing the database load in dynamic applications, to enhance the access speed.

memcache distributed cache is a set of software for distributed data caching and Web site session storage. Use key => value stored support data compression processing and data storage processing expired. value only support string, such as the need for other formats can be converted to json then save.


Windows installation memcahce

Memcache installed under Windows:
1. Download the memcache stable version of windows, put a pan under decompression, such as c: \ memcached
2. Enter 'c in the terminal (ie cmd command interface): \ memcached \ memcached.exe -d install 'installation
3. enter:' c: \ memcached \ memcached.exe -d start ' promoter. NOTE: After memcached starts automatically as a service when the windows of each boot. Such a server has been installed.
4. Download php_memcache.dll, please find their own version of the corresponding php file
5. In the C: \ winnt \ php.ini add the line 'Extension = php_memcache.dll'
6. Restart Apache, then look at phpinfo, if memcache , then the installation is successful!

$mem = new Memcache;
$mem->connect("127.0.0.1", 11211);
$mem->set('key1', 'This is a test!', 0, 60);
$val = $mem->get('key1');
echo $val;

Initializing a Memcache object:
$ = MEM new new Memcache;

Memcache connected to our server, the first parameter is the IP address of the server, it can be a host name, the second parameter is the Memcache of open ports:
$ MEM-> Connect ( "localhost", 11211);

Memcache save data to a server, the first parameter is the key data used for positioning data, the second parameter is the need to save the contents of the data, this is a string, the third parameter is a flag is generally provided 0 or MEMCACHE_COMPRESSED on the line, the fourth argument is valid data, that data at this time to be effective, if in the past this time, it will be cleared out of the Memcache server-side data, in seconds, if set to 0, it is always effective, we are here to set up 60, that is, the effective time of one minute:
$ MEM-> the sET ( 'key1', 'this iS First value', 0, 60);

Memcache acquired from a server-side data, it is only one parameter, the need to obtain key data, we are here key1 step on set, now get this data output:
$ $ MEM-Val => GET ( 'key1') ;
echo "the Get key1 value:." $ Val;

It is using the replace method to replace in the above values of key1, with the parameters set replace method is the same, but the first argument is key1 must be to replace the data content of the key, the final output:
$ MEM-> replace ( ' key1 ',' Replace This value IS ', 0, 60);
$ $ = MEM-Val> GET (' key1 ');
echo "key1 the Get value:." $ Val;

Similarly, the array can be saved Memcache is, the following is saved in an array Memcache above, then acquires and outputs back
$ ARR = Array ( 'AAA', 'BBB', 'CCC', 'ddd');
$ MEM- > SET ( 'key2', $ ARR, 0, 60);
$ $ = MEM-val2> GET ( 'key2');
print_r ($ val2);

Now delete data, use delte interface is a key parameter, then you can put Memcache server to delete this key data, when the final output is not the result of
$ MEM-> the Delete ( 'key1');
$ = $ MEM-Val> GET ( 'key1');
echo "the Get key1 value:.". $ Val "<br>";

Finally, we put all the data stored on the server Memcache are clear, you will find the data are gone, the final output data key2 is empty, and finally close the connection
$ MEM-> flush ();
$ MEM-$ val2 => GET ( 'key2');
echo "the Get key2 value:";
print_r ($ val2);
echo "<br>";

Memcache used
may be a combination of user name and user ID into memcache token, then the corresponding data is stored. Set the expiration time, to obtain value through this unique token when in use. When data is updated, the value of the token before the first deleted and then the new values are stored.
The desired value. When data is updated, the value of the token before the first deleted and then the new values are stored.


if(check_key_exists($mem, 'foo')){
echo 'foo key存在<br>';
}else{
echo 'foo key不存在<br>';
}
判断 key
function check_key_exists($mem, $name){
$data = $mem->get($name);
return $data!==false;
}

The principle MemCache

First of all to point out, MemCache data stored in memory, stored in the memory means personally believe that:

1, the speed of accessing data faster than traditional relational databases, because Oracle, MySQL these traditional relational database in order to maintain persistent data, data stored in the hard disk, IO operation is slow

2, MemCache data stored in memory at the same time means that as long MemCache restart, the data will disappear

3, since MemCache data stored in memory, it is bound to be machine-digit limit, this article written many times before, and can only use a maximum of 2GB of memory 32-bit machines, 64 machines that there is no upper limit

1.memcache maximum validity is 30 days
2. Default Port: 11211
3. The concept hit
when the client initiates a request, our application to accept the request, and if the first check the cache when the database needs to be read product information.
If the cache, an entry is found by a mark, the entry will be used, we call it cache hit.

Guess you like

Origin www.cnblogs.com/zhangxu-fasu/p/11491620.html
09