How to use MemCache cache correctly in Yii framework
Nov 18, 2020 pm 03:29 PMThe 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常用的函數(shù): Memcache::add //添加一個值,如果已經(jīng)存在,則返回false Memcache::addServer // 添加一個可供使用的服務(wù)器地址 Memcache::close //關(guān)閉一個Memcache對象 Memcache::connect // 創(chuàng)建一個Memcache對象 memcache_debug // 控制調(diào)試功能 Memcache::decrement // 對保存的某個key中的值進(jìn)行減法操作 Memcache::delete //刪除一個key值 Memcache::flush // 清除所有緩存的數(shù)據(jù) Memcache::get //獲取一個key值 Memcache::getExtendedStats //獲取進(jìn)程池中所有進(jìn)程的運行系統(tǒng)統(tǒng)計 Memcache::getServerStatus// 獲取運行服務(wù)器的參數(shù) Memcache::getStats //返回服務(wù)器的一些運行統(tǒng)計信息 Memcache::getVersion //返回運行的Memcache的版本信息 Memcache::increment //對保存的某個key中的值進(jìn)行加法操作 Memcache::pconnect // 創(chuàng)建一個Memcache的持久連接對象 Memcache::replace //對一個已有的key進(jìn)行覆寫操作 Memcache::set //添加一個值,如果已經(jīng)存在,則覆寫 Memcache::setCompressThreshold //對大于某一大小的數(shù)據(jù)進(jìn)行壓縮 Memcache::setServerParams //在運行時修改服務(wù)器的參數(shù)
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!

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Evaluating the cost/performance of commercial support for a Java framework involves the following steps: Determine the required level of assurance and service level agreement (SLA) guarantees. The experience and expertise of the research support team. Consider additional services such as upgrades, troubleshooting, and performance optimization. Weigh business support costs against risk mitigation and increased efficiency.

The learning curve of a PHP framework depends on language proficiency, framework complexity, documentation quality, and community support. The learning curve of PHP frameworks is higher when compared to Python frameworks and lower when compared to Ruby frameworks. Compared to Java frameworks, PHP frameworks have a moderate learning curve but a shorter time to get started.

The lightweight PHP framework improves application performance through small size and low resource consumption. Its features include: small size, fast startup, low memory usage, improved response speed and throughput, and reduced resource consumption. Practical case: SlimFramework creates REST API, only 500KB, high responsiveness and high throughput

The main differences between Laravel and Yii are design concepts, functional characteristics and usage scenarios. 1.Laravel focuses on the simplicity and pleasure of development, and provides rich functions such as EloquentORM and Artisan tools, suitable for rapid development and beginners. 2.Yii emphasizes performance and efficiency, is suitable for high-load applications, and provides efficient ActiveRecord and cache systems, but has a steep learning curve.

The steps to containerize and deploy Yii applications using Docker include: 1. Create a Dockerfile and define the image building process; 2. Use DockerCompose to launch Yii applications and MySQL database; 3. Optimize image size and performance. This involves not only specific technical operations, but also understanding the working principles and best practices of Dockerfile to ensure efficient and reliable deployment.

Java framework learning roadmap for different fields: Web development: SpringBoot and PlayFramework. Persistence layer: Hibernate and JPA. Server-side reactive programming: ReactorCore and SpringWebFlux. Real-time computing: ApacheStorm and ApacheSpark. Cloud Computing: AWS SDK for Java and Google Cloud Java.

Choose the best Go framework based on application scenarios: consider application type, language features, performance requirements, and ecosystem. Common Go frameworks: Gin (Web application), Echo (Web service), Fiber (high throughput), gorm (ORM), fasthttp (speed). Practical case: building REST API (Fiber) and interacting with the database (gorm). Choose a framework: choose fasthttp for key performance, Gin/Echo for flexible web applications, and gorm for database interaction.

There are five misunderstandings in Go framework learning: over-reliance on the framework and limited flexibility. If you don’t follow the framework conventions, the code will be difficult to maintain. Using outdated libraries can cause security and compatibility issues. Excessive use of packages obfuscates code structure. Ignoring error handling leads to unexpected behavior and crashes.
