yii2缓存的介绍和使用,yii2缓存介绍使用
作者:白狼 出处: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 />],
<span>所谓的文件缓存,其实就是把我们要缓存的数据存放到文件内,那数据又缓存到哪里了呢?</span>
//默认的缓存路径是@app\runtime\cache目录下,如果你想要修改缓存路径,可以像上面配置一样,配置下cachePath即可
我们直接来操作看看
<span>$cache</span> = Yii::<span>$app</span>-><span>cache; </span><span>$data</span> = <span>$cache</span>->get('cache_data_key'<span>); </span><span>if</span> (<span>$data</span> === <span>false</span><span>) { </span><span>//</span><span>这里我们可以操作数据库获取数据,然后通过$cache->set方法进行缓存 </span> <span>$cacheData</span> = ...... <span>//</span><span>set方法的第一个参数是我们的数据对应的key值,方便我们获取到 //第二个参数即是我们要缓存的数据 //第三个参数是缓存时间,如果是0,意味着永久缓存。默认是0 </span> <span>$cache</span>->set('cache_data_key', <span>$cacheData</span>, 60*60<span>); } </span><span>var_dump</span>(<span>$data</span>);
继续阅读

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

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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1
Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
