cachedmodelbehavior Handles model caching and its periodic synchronization with main data storage.

  1. Overview
  2. Features
  3. Requirements
  4. Usage

CachedModelBehavior

Overview

This component creates middle storage for used models in cache. Thus, it helps significantly alleviate DB server load. You may define time interval between data synchronizations from cache to main data storage.

Component is recommended for frequently requested/updated small pieces of data, for high-loaded Yii applications. Stored data should be not so significant, because it may be (rarely, but...) removed from cache storage: it is fast but trustless storage. For example, component may be used to store user visits, user likes etc in cache. It would be great, if you could use more reliable storage for data, such as Redis.

Note: component is not recommended for important user data (like passwords, payment transactions etc.), for large size or rarely requested/updated data (blog posts, logs etc.).

All api methods have the same names and arguments, as CActiveRecord class. So, if you set CachedModelBehavior::isEnabled to false or if there is no cached attributes, all methods will be simply downgraded to CActiveRecord methods, without using cache storage. No code changes needed anymore.

Features

  • Using of cache storage via Yii provided abstraction level.
  • Allows to update model attributes in cache using different operators.
  • Periodic synchronization (saving) changed cached models to DB.
  • Allows to define synchronization time interval and how long to keep cached models in cache storage.
  • Delayed insert for new models, which are saved in cache instead of DB between synchronizations. They are saved to DB then in multiinsert SQL command during synchronization.
  • Allows to select cached or not cached fields to avoid cache overflow with big size fields (BLOB, TEXT etc).
  • Log warnings for unwanted situations.

Requirements

Yii 1.1 or above

Usage

Extract the class file from the package of this extension in your protected/components directory.

You should add behavior to model to use extension:

class MyModel extends CActiveRecord
{
	...
	public function behaviors()
	{
 		return array(
 			'cachedModel' => array(
 				'class' => 'CachedModelBehavior',
				'fields' => '',
 				'excludedFields' => 'action,userid',
 				'syncInterval' => 15,
				'delayedInsert' => true,
 			),
 		);
	}
	...
}

Then, in controller class you can use component methods:

$model = new MyModel;
$model->title = 'New Story';
...
$model->cm()->save();

Please, see component comments for full explanation.

And also please, please feel you free to comment, ask, suggest impovements and point to bugs or design mistakes. I try to do my best in programming.

2 1
5 followers
501 downloads
Yii Version: 1.1
License: BSD-2-Clause
Category: Caching
Developed by: vittron
Created on: Sep 29, 2012
Last updated: 11 years ago

Downloads

show all

Related Extensions