Use of Yii's own cache

Source: Internet
Author: User

Yii's own cache inherits the CCache class, which basically has no difference in usage.
The cache base class CCache provides two of the most common methods: Set () and get ().
To store a variable $value in the cache, we select a unique ID and call set () to store it:

Yii::app ()->cache->set ($id$value);

The cached data remains in the cache until it is deleted due to some caching policies (such as cache space is full and the oldest data is deleted). To change this behavior, we can also add an expiration parameter when calling set (), so that the data is automatically purged from the cache over time.

Yii::app ()->cache->set ($id$value// retain this value in the cache for up to 30 seconds

When we need to access the variable later (whether it's not the same Web request), we call Get () (the incoming ID) to get it from the cache. If the return value is false, it means that the cache is not available and requires us to regenerate it.

$value=yii::app ()->cache->get ($id); if ($value= = =false) {    //  because it is not found in the cache, regenerate $value//    cache again for next use    / /Yii::app ()->cache->set ($id, $value);}

When selecting an ID for a variable to be cached, ensure that the ID is unique in the app. It is not necessary to ensure that the IDs are unique across applications because the cache component has enough intelligence to differentiate the cache IDs of different applications.
To remove a cached value from the cache, call Delete (); To clear all caches, call flush (). Be very careful when you call Flush (), because it will also empty the cache for other applications.
Tip: Because CCache implements the Arrayaccess interface, you can use the cache component like an array. For example:

$cache=yii::app (),cache; $cache [' var1 ']=$value 1;  // equivalent: $cache->set (' var1 ', $value 1); $value 2=$cache[' var2 '];  // equivalent to: $value 2= $cache->get (' var2 ');

It is also easy to use these caches, as long as the server is supported, and then simply modify the configuration file to use

Use of Memcache
1. Edit the configuration file config/main.php add memcache configuration

Array(        ' Components ' =Array(            ' Memcache ' =Array(                ' Class ' = ' System.caching.CMemCache ', ' servers ' =Array(                    Array(                        ' Host ' = ' server1 ', ' Port ' =>11211, ' weight ' =>60,                    ),Array(                        ' Host ' = ' server2 ', ' Port ' =>11211, ' weight ' =>40,                    ),                ),            ),        ),    )

2. Use in the framework

Yii::app ()->memcache->set ($key$value$expire); Yii:: App ()->memcache->get ($key); Yii:: App ()->memcache->deletevalue ($key);

Use of the database cache
1. Edit the configuration file config/main.php add Dbcache Configuration

return Array (        ...        ') Components ' = =array(            ...            ') Dbcache ' = =array(                ' class ' = ' System.caching.CDbCache ',            ),            ' db ' = =array(                ' class ' = ' system.db.CDbConnection ',                ' connectionString ' = ') Sqlite:/wwwroot/blog/protected/data/blog.db ',                ' schemacachingduration ' =>3600,            ),         ),    );

2. Use in the framework

Yii::app ()->dbcache->set ($key$value$expire); Yii:: App ()->dbcache->get ($key);

Use of the file cache
1. Edit the configuration file config/main.php add Dbcache Configuration

// Applicationcomponents ' components ' +array(       ' filecache ' = =array  (             ' class ' = ' System.caching.CFileCache ',                 // we use CFileCache to implement the cache, the cache file is stored in the runtime folder             ' Directorylevel ' = ' 2 ',   // directory depth of cache file       ),

2. Use in the framework

Yii::app ()->filecache->set ($key$value$expire); Yii:: App ()->filecache->get ($key);

APC Use
1. Edit the configuration file config/main.php add Dbcache Configuration

' Components ' =array(       ' class ' = ' System.caching.CApcCache ',),

Use of Yii's own cache

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.