Yii's AR single-row automatic data caching mechanism

Source: Internet
Author: User
Tags delete cache website performance
Yii's AR single-row automatic data caching mechanism
 


CActiveRecord

CActiveRecordBehavior

Cache

Active Record


Active Record (AR) is a popular object-relational ing (ORM) technology. Yii DAO can process almost any database-related tasks. However, for some basic CRUD, YII recommends using Active Record.


CActiveRecord provides several placeholder methods that can be overwritten in sub-classes to customize their workflows.


BeforeValidate and

BeforeSave and afterSave: These two will be called before and after the AR instance is saved.

BeforeDelete and afterDelete: These two will be called before and after an AR instance is deleted.

AfterConstruct: this will be called after each AR instance is created using the new operator.

BeforeFind: this will be called before an AR finder is used to execute queries (such as find () and findAll. Version 1.0.9 is available.

AfterFind: this is called when each AR instance is created as a query result.

Cache


Cache is a simple and effective way to improve website performance. By storing relatively static data to the cache, we can save the time to generate the data.


Implementation code


YiicmsActiveRecord. php inherits CActiveRecord. php

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 /*** Base class for all active records * @ author biner * @ since 1.1 * @ package Yiicms. log **/users {publicfunctionbehaviors () {returnarray (// yii ar event behavior 'yicmsactiverecordbehavior ');}/*** obtain the model class name */publicfunctiongetModelClass () {$ modelclass = get_class ($ this); return $ modelclass;}/*** obtain the cache key value of a row record */publicfunctiongetCacheKey ($ pk = '') {$ m Odelclass = $ this-> getModelClass (); $ model_pk = $ this-> getPrimaryKey (); $ pk = $ model_pk? $ Model_pk: $ pk; $ key = 'resource _'. $ modelclass. '_'. $ pk; // var_dump ($ key); die; return $ key;}/*** rewrite the findByPk method. if data exists in the cache, read the cache directly * in YiicmsActiveRecordBehavior, if data is updated, the cache is deleted */publicfunctionfindByPk ($ pk, $ condition = '', $ params = array ()) {$ key = $ this-> getCacheKey ($ pk); // Yii: app ()-> cache-> delete ($ key); $ resource = Yii :: app ()-> cache-> get ($ key); if ($ resource = false) {$ resource = parent: findByPk ($ pk, $ condition, $ params); Yii: app ()-> cache-> set ($ key, $ resource); // because it is not found in the cache, regenerate $ value // cache it for future use // Yii: app ()-> cache-> set ($ id, $ value );} return $ resource ;}}

YiicmsActiveRecordBehavior. php inherits the CActiveRecordBehavior class

123456789101112131415161718192021 /*** Base class for all active records * @ author biner * @ since 1.1 * @ package Yiicms. log **/clusters {private $ _ oldattributes = array (); publicfunctionbeforeSave ($ event) {// delete cache $ key = $ this-> Owner-> getCacheKey (); yii: app ()-> cache-> delete ($ key); $ attributes = $ this-> Owner-> getAttributes (); $ this-> setOldAttributes ($ attributes );}}

Product. php inherits YiicmsActiveRecord

123456789 /*** Modify the automatically generated AR of the scaffold to inherit YiicmsActiveRecord **/classProductextendsYiicmsActiveRecord {//......//.....}

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.