The specific operations are as follows:
(Recommended graphic tutorial: yii)
1. Configure the memcache server server
1. Download the memcached software package. Download address:
http://pan.baidu.com/s/1boAnghp
Unzip and put memcached.exe in any place, such as: d:/memcached/ (I unzipped it to my wampserver The bin directory is at the same level as the php directory).
2. Enter cmd using the Windows R shortcut key, enter the code in the command line, and switch to the directory of memcache.exe:
3. Enter Use the following code to install memcache.exe: memcached.exe -d install
4. Enter memcached.exe -d start to start
Configuration successful
//memcache常用的函数: Memcache::add //添加一个值,如果已经存在,则返回false Memcache::addServer // 添加一个可供使用的服务器地址 Memcache::close //关闭一个Memcache对象 Memcache::connect // 创建一个Memcache对象 memcache_debug // 控制调试功能 Memcache::decrement // 对保存的某个key中的值进行减法操作 Memcache::delete //删除一个key值 Memcache::flush // 清除所有缓存的数据 Memcache::get //获取一个key值 Memcache::getExtendedStats //获取进程池中所有进程的运行系统统计 Memcache::getServerStatus// 获取运行服务器的参数 Memcache::getStats //返回服务器的一些运行统计信息 Memcache::getVersion //返回运行的Memcache的版本信息 Memcache::increment //对保存的某个key中的值进行加法操作 Memcache::pconnect // 创建一个Memcache的持久连接对象 Memcache::replace //对一个已有的key进行覆写操作 Memcache::set //添加一个值,如果已经存在,则覆写 Memcache::setCompressThreshold //对大于某一大小的数据进行压缩 Memcache::setServerParams //在运行时修改服务器的参数
2. Install php_memcache.dll extension
1. First download the php_memcache.dll extension that matches your php version
Address:
http://pan.baidu.com/s/1skJnZIl
2. Copy and post it in your php/ext folder
3. Add the following code in php.ini and import the extension:
extension=php_memcache.dll
3. Configuration in yii
1. If you are using the basic framework: open config/web.php
If you are using the advanced framework: open config/main.php
Add the following Code:
'cache' => [ 'class' => 'yii\caching\MemCache', 'servers' => [ [ 'host' => 'localhost', 'port' => 11211, 'weight' => 100, ], [ 'host' => 'localhost', 'port' => 11211, 'weight' => 50, ], ], ],
Now you can use the following code in the action method of your controller:
//缓存MemCache $key='key'; $value='this is the value'; <pre name="code" class="php">Yii::$app->cache->set($key,$value); echo "lixian"; echo Yii::$app->cache->get($key); exit();
But when you use the expiration time:
Yii::$app->cache->set($key,$value,6);//过期时间为6s echo "lixian"; echo Yii::$app->cache->get($key); exit();
The output cannot be done. This is a bug in the framework itself. The solution is as follows:
Configure the corresponding parameters according to the document before using memcache in yii2. You will find this when using memcache (non-memcached). Yii::$app->cache->set('key','value'); When this sentence does not set the validity time in seconds, the value of the key in the cache can be obtained normally, but it means that the key will not expire.
When the valid time is set, Yii::$app->cache->set('key','value',60); For example, after 60 seconds, it will appear that the value corresponding to the key cannot be obtained. I have searched online and in forums, but I have not found a solution. Many PHPers have encountered similar problems. So I went to take a look at the underlying code. The referenced memcache class is yii\caching\MemCache that comes with Yii2. Yii::$app->cache->set('key','value') This code will automatically call the setValue method under the memcache class to set the cache, so the problem may arise here.
After testing later, I found the last sentence of this method return $this->useMemcached ? $this->_cache->set($key, $value, $expire) : $this->_cache->set($key, $value, 0, $expire ) problem, change this code to return $this->useMemcached ? $this->_cache->set($key, $value, $expire) : $this->_cache->set($key, $value, 0, $duration ) Afterwards, I tested again and found that the problem had been solved.
The reason is that it is called when memcache is used in this return code. $this->_cache->set($key, $value, 0, $expire) , equivalent to $mem = new Memcache();$mem->set($key,$value,0,$expire); The most critical point here is: the time to be set by memcache means how many seconds the effective time is, and the time to be set by memcached means the second at which it will expire (equivalent to time() $duration), so the problem is found Afterwards, the problem was solved by changing the second $expire in the original code to $duration.
The above is the detailed content of How to use MemCache cache correctly in Yii framework. For more information, please follow other related articles on the PHP Chinese website!