Yii Framework Official Guide Series 28--cache: overview

Source: Internet
Author: User
Tags apc



Caching is a simple and effective way to improve the performance of your Web application. By storing relatively static data in the cache and retrieving the cache when the request is received, we save the time it takes to generate the data.

Using caching in Yii primarily includes configuring and accessing an application component. The following application configuration sets up a cache component that uses two memcache cache servers.


Array (...    ') Components ' =>array (...        ') Cache ' =>array ' (            ' class ' = ' System.caching.CMemCache ',            ' Servers ' =>array ('                host ' = = ') Server1 ', ' Port ' =>11211, ' weight ' =>60),                Array (' host ' = ' server2 ', ' Port ' =>11211, ' weight ' =>40),            ),        ),    ),);

When the app is running, the cache component is Yii::app()->cache accessible.

YII provides different caching components that can store cached data in different media. For example, the Cmemcache component encapsulates the PHP memcache extension and uses memory as the cache storage medium. The Capccache component encapsulates the PHP APC extension; The Cdbcache component then stores the cached data in the database. The following is a list of available cache components:

    • Cmemcache: Use PHP memcache extensions.

    • Capccache: Use the PHP APC extension.

    • Cxcache: Use PHP XCache extensions. Note that this is supported starting from version 1.0.1.

    • Ceacceleratorcache: Use PHP eaccelerator extensions.

    • Cdbcache: Use a data table to store cached data. By default, it creates and uses a SQLite3 database in the runtime directory. You can also specify a database to use for it by setting its ConnectionID property.

    • Czenddatacache: Use Zend Data cache as the background cache medium. Note that this is supported starting from version 1.0.4.

    • CFileCache: Use file storage to cache data. This is especially useful for storing large chunks of data (such as pages). Note that this is supported starting from version 1.0.6.

    • Cdummycache: Currently, the dummy cache does not implement caching capabilities. The purpose of this component is to simplify the code that needs to check the availability of the cache. For example, we can use this cache component during the development phase or when the server has not yet supported the actual caching functionality. When the actual cache support is enabled, we can switch to using the appropriate cache component. In both cases, we can use the same code to get a Yii::app()->cache->get($key) piece of data without worrying about Yii::app()->cache what might be null . This component starts with support from version 1.0.5.

tip: Since all of these cache components inherit from the same base class CCache, you can switch to using a different caching method without changing the code that uses the cache.

The cache can be used at different levels. At the lowest level, we use the cache to store a single piece of data, such as a variable, which we call the data caching. In the next level, we store a page fragment generated by a portion of the view script in the cache. At the highest level, we store the entire page in the cache and retrieve it when needed.

In the next few sections, we'll explain in detail how to use caching at these levels.

Note: by definition, caching is an unstable storage medium. Even if there is no timeout, it does not ensure that the cached data must exist. Therefore, do not use the cache as a persistent memory. (for example, do not use the cache to store Session data).

Cache Series Articles:

Yii Framework Official Guide Series 29--cache: Data cache

Yii Framework Official Guide Series 30--cache: Fragment cache (Fragment Caching)

Yii Framework Official Guide Series 31--cache: page Cache

Yii Framework Official Guide Series 32--cache: Dynamic Content

The above is the official Yii Framework Guide Series 28--cache: At a glance, more about topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.