Home php教程 php手册 yii2缓存的介绍和使用 - 白狼栈

yii2缓存的介绍和使用 - 白狼栈

May 20, 2016 am 10:14 AM

作者:白狼 出处:http://www.manks.top/yii2_cache.html 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

一个好的框架是肯定离不开缓存的使用,相反,一个没有缓存的框架也肯定不是一个好框架,好像是一个意思,不管了,我们先来看看yii2中是如何使用缓存的吧。

又是到我们第一步操作的时候了,我们先来配置组件。

方便起见,我们的缓存组件配置在common\config\main.php文件,先来简单配置下文件缓存

'components' => [ <br>  'cache' => [ <br>    'class' => 'yii\caching\FileCache', <br>    'cachePath' => '@runtime/cache2', <br>  ], <br>],
Copy after login
<span style="font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; line-height: 1.5;">所谓的文件缓存,其实就是把我们要缓存的数据存放到文件内,那数据又缓存到哪里了呢?</span>
Copy after login

//默认的缓存路径是@app\runtime\cache目录下,如果你想要修改缓存路径,可以像上面配置一样,配置下cachePath即可

我们直接来操作看看

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

继续阅读

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)