Yii2 simple parsing using cache

*文
Release: 2023-03-18 19:36:01
Original
1676 people have browsed it

A cached framework can be said to be a good framework. The editor below will introduce to you how to use cache in yii2 through this article. It is very valuable for reference. Friends who are interested can learn together. I hope to be helpful.

A good framework is definitely inseparable from the use of cache. On the contrary, a framework without cache is definitely not a good framework. It seems to mean the same thing. Regardless, let’s take a look at how it works in yii2. Use caching.

It’s time for our first step again. Let’s configure the components first.

For the sake of convenience, our cache component is configured in the common\config\main.php file. First, let’s briefly configure the file cache.

'components' => [ 'cache' => [ 'class' => 'yii\caching\FileCache', 'cachePath' => '@runtime/cache2', ], ],
Copy after login

The so-called file cache is actually the file we want to cache. The data is stored in the file, but where is the data cached?

//The default cache path is in the @app\runtime\cache directory. If you want to modify the cache path, you can configure cachePath like the above configuration.

Let’s go directly Take a look at the operation

$cache = Yii::$app->cache; $data = $cache->get('cache_data_key'); if ($data === false) { //这里我们可以操作数据库获取数据,然后通过$cache->set方法进行缓存 $cacheData = ...... //set方法的第一个参数是我们的数据对应的key值,方便我们获取到 //第二个参数即是我们要缓存的数据 //第三个参数是缓存时间,如果是0,意味着永久缓存。默认是0 $cache->set('cache_data_key', $cacheData, 60*60); } var_dump($data);
Copy after login

The above content is the method of using Yii2 cache introduced by the editor. You can refer to it.

The following will introduce how to set up Cache cache in Yii

First add:

'cache'=>array( 'class'=>'CFileCache'),
Copy after login
## to the components array of the configuration file

#Set Cache:

Yii::app()->cache->set('testcache', array(1,3,4,6));//默认有效期为一年 Yii::app()->cache->set('testcache', array(1,3,4,6), 3600);//一个钟,秒为单位
Copy after login

Get Cache:

$data = Yii::app()->cache->get('testcache');
Copy after login

Delete a single Cache:

Yii::app()->cache->delete('testcache');
Copy after login

Clear all Cache:

Yii::app()->cache->flush();
Copy after login

Related recommendations:

PHP caching mechanism

PHP cache class

Yii2 implements rbac permission control

The above is the detailed content of Yii2 simple parsing using cache. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!