Home > Article > Backend Development > How to modify ThinkPHP cache to Memcache_PHP tutorial
Generally speaking, ThinkPHP's default caching method is implemented in File file mode, and many cache files will be generated under /Runtime/Temp during runtime.
In some cases, after memcached is installed on the server, it is necessary to change ThinkPHP's caching method to memecache
The specific steps are as follows:
Add in Conf/config.php:
'DATA_CACHE_TYPE' => 'Memcache', 'MEMCACHE_HOST' => 'tcp://127.0.0.1:11211',
After updating the cache, I refreshed the page and found that the cache did not take effect.
Get debugging information:
[ 2010-09-30T11:41:56+08:00 ] NOTIC: [8] MemcachePool::set(): Server 127.0.0.1 (tcp 11211, udp 0) failed with: CLIENT_ERROR bad command line format (0) CacheMemcache.class.php 第 107 行.
So I found this sentence:
return $this->handler->set($name, $value, 0, $expire);
After further debugging, it was found that the value of $expire is -1; the cache duration was not specified when writing the program, and the -1 here may not be accepted by memcached.
So add in Conf/config.php:
'DATA_CACHE_TIME' => '3600',
Specify the default cache duration as 3600 seconds; run it again, the error disappears, and the cache is successful!
In addition, when you need to clear all caches in a certain action, you can do this:
$cache = Cache::getInstance(); $cache ->clear();