ThinkPHP3.2. 3 Framework Memcache Cache Usage Example Summary

  • 2021-12-09 08:24:28
  • OfStack

This article illustrates the use of Memcache cache in ThinkPHP 3.2. 3 framework. Share it for your reference, as follows:

The previous article described the installation of Memcached server and client under Linux, so here is a summary of the use of ThinkPHP 3.2. 3 framework Memcache under 1.

Method 1: Writing of native Memcache


public function test1() {
  $mc = new \Memcache(); // Create Memcache Object 
  $mc->connect("127.0.0.1", 11211); // Connect Memcached Server 
  $mc->set('test1','hello world',0,10); // Store data 
  echo $mc->get('test'); // Get data 
  $mc->delete('test'); // Delete data 
  $mc->flush(); // Force the whole cache to be refreshed, that is, empty Memcached Server 
  $mc->close(); // Disconnect from Memcached Connection to the server 
}

Method 2: Call the integrated Memcache cache driver directly


public function test2() {
  $mc = new \Think\Cache\Driver\Memcache(); // Instantiation Memcache Drive 
  $mc->set('test2','hello world',60); // Write cache 
  echo $mc->get('test2'); // Read cache 
  $mc->rm('test2'); // Delete the specified cache 
  $mc->clear(); // Empty all caches 
}

Method 3: S method

① Call S method directly


public function test3() {
  // Cache initialization 
  S(array(
    'type'=>'memcache', // Cache type 
    'host'=>'127.0.0.1', //Memcache Server address 
    'port'=>'11211', //Memcache Port number 
    'prefix'=>'think', // Cache identification prefix  
    'expire'=>10,) // Cache validity period (in seconds) 
  );
  // Setting cache 
  S('test3','hello world'); // You can change the cache time: S('test3','hello world',60);
  // Read cache 
  echo S('test3');
  // Delete cache 
  S('test3',null);
}

Secondly, the cache is operated by object mode


public function test4() {
  $cache = S(array('type'=>'memcache','prefix'=>'think','expire'=>10)); // Cache initialization 
  $cache->test4 = 'hello world'; // Setting cache 
  echo $cache->test4; // Get the cache 
  unset($cache->test4); // Delete cache 
}

About S method initialization in the manual: if type parameter is not passed in, DATA_CACHE_TYPE set in the configuration file will be read as the default cache type; If the prefix parameter is not passed in, DATA_CACHE_PREFIX is read as the default cache identification prefix; If the expire parameter is not passed in, DATA_CACHE_TIME is read as the default cache validity period.

In the configuration file config. php, the Memcache cache configuration is as follows:


'DATA_CACHE_TYPE' => 'Memcache', // Data cache type 
'DATA_CACHE_PREFIX' => '', // Cache prefix 
'DATA_CACHE_TIME' => 10, // Validity period of data cache  0 Indicates permanent cache 
'DATA_CACHE_COMPRESS' => false, // Does the data cache compress the cache 

Calling the S method after configuration does not need to be initialized again


public function test5() {
  // Setting cache 
  S('test5','hello world');
  // Read cache 
  echo S('test5');
  // Delete cache 
  S('test5',null);
}

For more readers interested in thinkPHP related contents, please check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".

I hope this article is helpful to the PHP programming based on ThinkPHP framework.


Related articles: