Yii Framework-Data caching

Source: Internet
Author: User

Yii Framework-Data caching
    1. Configuration of Yii cache:
Yii directory/config/web.php file, configure the value of class array under the cache array, default to Yii\caching\filecache.
2. Use of the cache:
To get the cache component:
$cache=\yii::$app->cache;

To add cached data:
$cach->add (key, value[,time][,dependency]);
Parameters:
Key Key
Value Value
Time Optional, cache expiration
Dependency Optional, dependency mode
return value:
To modify the cached data:
$cach->set (key, value[,time]);
Parameters:
Key Key
Value Value
Time Optional, cache expiration
return value:
Note: If multiple modifications are made to the same key at the same time, only the first modification is valid.
To delete cached data:
$cach->delete (key);
Parameters: Key Key
return value:
Get Cached data:
$cach->get (key);
Parameters: Key Key
return value:
Empty the cache:
$cach,flush();
return value:
3. Cache dependency:
There are three ways to rely on caching: file dependencies, expression dependencies, db dependencies, chain dependencies, group dependencies. Only the first three dependencies are described here.
File dependencies:
// declaring dependent methods and dependent files $dependency=new  \yii\caching\filedependency ([' filename ' = ' test.txt ']); // associated caches and dependencies $cache->add (' key1 ', ' Hello world! ',$dependency);

If the file changes, the associated cache is invalidated.

Expression dependency:
// declaring dependency patterns and dependency expressions $dependency=new  \yii\caching\expressiondependency ([' expression ' = ' = ' \yii\:: $app- >requests->get ("name") ']); // associating a cache with a dependency $cache->add (' key1 ', ' Hello world! ',$dependency);

If the expression result changes, the associated cache is invalidated.

DB dependency:
// declaring dependency patterns and relying on SQL statements $dependency=new  \yii\caching\dbdependency ([' sql ' = ' = ' SELECT coun (*) from Yii ']); // associated caches and dependencies $cache->add (' key1 ', ' Hello world! ',$dependency);

If the result of the SQL statement execution changes, the associated cache is invalidated.

Yii Framework-Data caching

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.