Home >Backend Development >PHP Tutorial >thinkphp caching introductory tutorial

thinkphp caching introductory tutorial

WBOY
WBOYOriginal
2016-07-25 08:52:18947browse
  1. return array(
  2. 'DB_TYPE'=>'mysql',
  3. 'DB_HOST'=>'127.0.0.1',
  4. 'DB_NAME'=>'w3note',
  5. ' DB_USER'=>'root',
  6. 'DB_PWD'=>'123456',
  7. 'DB_PORT'=>'3306',
  8. 'DB_PREFIX'=>'w3_',
  9. 'DATA_CACHE_TYPE'=>' file',//Set the cache mode to file
  10. 'DATA_CACHE_TIME'=>'600',//The cache period is 600 seconds
  11. );
  12. ?>
Copy code

Usage of Thinkphp cache function Use the shortcut cache function S() in thinkphp for caching, for example:

  1. //This class is automatically generated by the system and is for testing purposes only
  2. class IndexAction extends Action{
  3. public function index(){
  4. //If there is a cache, read the cache data
  5. //If there is no cache, read the data from the database and put it into the cache
  6. $lists=S('lists');
  7. if(emptyempty($lists)){
  8. $news=M('news');
  9. $ lists=$news->select();
  10. S('lists',$lists,600);
  11. echo 'This is data directly read from the database';
  12. }
  13. dump($list);
  14. ?>
Copy the code

Visit http://127.0.0.1/Home/index.php/Index/index Output Read data from the database directly:

  1. array(10) {
  2. [0] => array(12) {
  3. ["id"] => string(1) "1"
  4. ["catid"] => string( 2) "13"
  5. ["title"] => string(4) "thinkphp's caching technology"
  6. ["content"] => string(8) "thinkphp's caching technology"
  7. ["tags"] = > string(4) "caching"
  8. ["thumb"] => string(0) ""
  9. ["description"] => string(7) "thinkphp's caching technology"
  10. ["inputtime"] = > string(10) "1348370202"
  11. ["posid"] => string(1) "1"
  12. ["ord"] => string(1) "2"
  13. ["hits"] => string(1) "1"
  14. ["status"] => string(1) "1"
  15. }
Copy the code

Note that when running for the first time, the information shown above will be printed, After refreshing the page, there is no "This is data directly read from the database", that is, the previously generated cached data is read.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn