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!

Migratingalaravel Projecttoyiiishallingbutachieffable WITHIEFLEFLANT.1) Mapoutlaravel component likeroutes, Controllers, Andmodels.2) Translatelaravel's SartisancommandeloequentTooyii's giiandetiverecordeba

Soft skills are crucial to Yii developers because they facilitate team communication and collaboration. 1) Effective communication ensures that the project is progressing smoothly, such as through clear API documentation and regular meetings. 2) Collaborate to enhance team interaction through Yii's tools such as Gii to improve development efficiency.

Laravel'sMVCarchitectureoffersenhancedcodeorganization,improvedmaintainability,andarobustseparationofconcerns.1)Itkeepscodeorganized,makingnavigationandteamworkeasier.2)Itcompartmentalizestheapplication,simplifyingtroubleshootingandmaintenance.3)Itse

Yiiremainsrelevantinmodernwebdevelopmentforprojectsneedingspeedandflexibility.1)Itoffershighperformance,idealforapplicationswherespeediscritical.2)Itsflexibilityallowsfortailoredapplicationstructures.However,ithasasmallercommunityandsteeperlearningcu

Yii frameworks remain strong in many PHP frameworks because of their efficient, simplicity and scalable design concepts. 1) Yii improves development efficiency through "conventional optimization over configuration"; 2) Component-based architecture and powerful ORM system Gii enhances flexibility and development speed; 3) Performance optimization and continuous updates and iterations ensure its sustained competitiveness.

Yii is still suitable for projects that require high performance and flexibility in modern web development. 1) Yii is a high-performance framework based on PHP, following the MVC architecture. 2) Its advantages lie in its efficient, simplified and component-based design. 3) Performance optimization is mainly achieved through cache and ORM. 4) With the emergence of the new framework, the use of Yii has changed.

Yii and PHP can create dynamic websites. 1) Yii is a high-performance PHP framework that simplifies web application development. 2) Yii provides MVC architecture, ORM, cache and other functions, which are suitable for large-scale application development. 3) Use Yii's basic and advanced features to quickly build a website. 4) Pay attention to configuration, namespace and database connection issues, and use logs and debugging tools for debugging. 5) Improve performance through caching and optimization queries, and follow best practices to improve code quality.

The Yii framework stands out in the PHP framework, and its advantages include: 1. MVC architecture and component design to improve code organization and reusability; 2. Gii code generator and ActiveRecord to improve development efficiency; 3. Multiple caching mechanisms to optimize performance; 4. Flexible RBAC system to simplify permission management.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
