A detailed description of the function of adding, deleting, changing and checking the database cache of micro-engine

Source: Internet
Author: User

File where function is located: framework/function/cache.mysql.func.php


Current micro-personal real call:

$setting = $this->module[' config '];

$AAAAA = $setting [' Copyright '];


In the micro-public number system can be divided into three types of cache: Save to the database table Core_cache in the database cache, save to the file cache and Memcahe cache, this tutorial is about the database cache, is also the default configuration of the micro-system cache.


Because the functions that handle the database cache are relatively simple, the Phpos network makes these functions a tutorial. Because it is a database table operation, so, how to manipulate the table, nothing more than to insert (increment), delete, modify and query the table records, so, corresponding to the database cache function also has to insert cache data, delete cached data, modify the cached data and query cache data operations.


In the micro-public number system, the cache data is stored in the cache table Core_cache inside, that is, the following function is the table of the increase, delete, change and check the operation.


Table Core_cache:


One, insert (write) cache data


Cache_write ($key, $data) function: This site has been explained in detail, there is no more explanation.


Example


Load ()->func (' Cache.mysql ');

Cache_write (' Phpos ', Array (' webname ' = ' development ', ' url ' = ' phpos.net ', ' title ' =>array (' development ', ' public Number '));


Results:



Second, delete the cached data


Delete function:cache_delete ($key)


This function is to delete a record in the Core_cache table.


$key the value of the field key in the table to be deleted, for example, delete the Phpos record, then $key= ' Phpos '.


The SQL statement used by this function is:

DELETE from '. TableName (' Core_cache '). ' WHERE ' key ' =:key


This parameter $key is exactly what you want to delete, so as long as you pass a value $key you can delete the record for the key field in the table.


In this function, we use the database query function Pdo_query ($sql, $params), which is encapsulated by the micro-public number system, which executes an SQL statement similar to mysql_query () in the process.


Example


Delete the record for the field Phpos:

Load ()->func (' Cache.mysql ');

Cache_delete (' Phpos ');


Delete records from the entire cache table, or delete records that contain the same name.


Delete function: Cache_clean ($prefix = ")


The function above is to delete a certain record, and this function deletes the entire table or a record containing the values of fields such as Phpos:, Web:, Site: And so on.


1) If $prefix is empty, the data in the Core_cache table is deleted.


Also, delete the data saved in the global variable $_w:

if ($result) {

unset ($_w[' cache ');

}


where the $_w[' cache ' is defined in the file bootstrap.inc.php:


if (!in_array ($_w[' config '] [' setting '] [' cache '], array (' MySQL ', ' file ', ' Memcache ')) {

$_w[' config ' [' Setting '] [' cache '] = ' mysql ';

}

The $_w[' config ' [' Setting '] [' cache '] in this condition is configured in the configuration file data/config.php, which by default is MySQL, which means using the cached data for the database cache.


2) If $prefix is not empty, delete the data that matches {$prefix}:%.


Example:


Load ()->func (' Cache.mysql ');

Cache_clean (' website ');

Or

Cache_clean ();


Iv. Querying cached data


1) Read the cache data function:


Cache_read ($key)

Read the value of one of the cached data in the table:


SELECT ' value ' from '. TableName (' Core_cache '). ' WHERE ' key ' =:key '


From this SQL statement, you can see that this function queries the value of the key in table Core_cache to $key, and uses the function Iunserializer ($val) to convert the serialized string of the query into a string that is deserialized.


Example


Load ()->func (' Cache.mysql ');

Print_r (Cache_read (' website '));


Result: Array ([url] = = www.phpos.net [title] + net)

2) query all eligible cache data functions:


Cache_search ($prefix)


To query the SQL statement:


$sql = ' SELECT * from '. TableName (' Core_cache '). ' WHERE ' key ' Like:key ';

$params = Array ();

$params [': key '] = "{$prefix}%";

$rs = Pdo_fetchall ($sql, $params);


From the above code you can see that this function is the value of the Query field key contains all records of {$prefix}, where the function Pdo_fetchall () is used to query out all eligible records.


Finally, the return is $result and an array.


Example


Load ()->func (' Cache.mysql ');

Print_r (Cache_search (' website '));


Result: Array ([website] + = array ([url] = www.phpos.net [title] = net))

V. Update cached data

In this file cache.mysql.func.php, we did not find the update cache data, can't update it? In fact, the Write data function Cache_write () inside the call function Pdo_insert (' Core_cache ', $record, True), using the Insert data function of the third parameter true, if using this parameter, use replace Into statement to insert data, this SQL statement will determine if there is a certain data in the table, delete it if it exists, and then insert the data to insert the table into the table.


So, in fact, this write data function itself has the function of updating data, so in this file there is no separate definition of "update" cache data.


A detailed description of the function of adding, deleting, changing and checking the database cache of micro-engine

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.