Yii framework distributed cache Implementation Scheme

Source: Internet
Author: User

Yii framework distributed cache Implementation Scheme

Disclaimer: This article is intended for users who have some knowledge about Yii and distributed cache. Otherwise, they may have some difficulties in understanding and understanding. We suggest you make the above two basic preparations before reading this article.

YiiFramework is short for Yii, which is easy to read and represents simple, efficient, and scalable. Yii brings object-oriented ideas and code reusability to the extreme, yii is one of the most efficient PHP frameworks, especially for cache support.

The Yii cache can be encapsulated in the caching folder at the core of the framework, such as the cache storage media supported by Yii.

CFileCache can be used for cross-Session and request distributed caching. If cross-WebServer distributed caching is required, CMemCache or CRedisCache must be used.

Yii User Login Mechanism

Introduce js and css files in Yii

Incomplete Yii Solution

Yii CGridView basic usage

1. distributed cache processing across sessions and requests (only one Web server uses cache dependencies to update the cache in real time)

Yii cache support requires you to add corresponding configuration support in the main configuration file of the project.

'Components' => array (

// Set Cache
'Cache' => array (
'Class' => 'cfilecac ',
),
),

In Yii, no matter whether the cache medium is disk, database, or memory, the value assignment and value are consistent. In this way, when we change the cache medium, we can change the primary configuration file, the program code does not need to be moved at all, which is also a good embodiment of Yii's code reusability to the extreme.

A. cache assignment:

Yii: app ()-> cache-> set (key, value, expiration time, dependency condition );

The following is a specific call case:

$ Products = ['id' => 1, 'name' => 'moutai liquor '];

Yii: app ()-> cache-> set ('product ',
$ Products,
30,
NewCGlobalStateCacheDependency ('version ')
);

The above is a common case. We use 'product' as the key, $ products array as the value, and the expiration time is 30 seconds (if it never expires, enter 0 here ), depends on the value of the global cache variable 'version.

After the cache dependency is set, when the value of 'version' changes, the cache expires. After 30 seconds, the cache expires. either of the two conditions is true and the cache expires.

B. Obtain the cache Value

Yii: app ()-> cache-> get (key );

The following is a specific call case:

Demo:

Read thread code:

$ Products = Yii: app ()-> cache-> get ('product ');
// The cache is not read.
If ($ products = false)
{
$ Pro = Yii: app ()-> db;
$ Products = $ pro-> createCommand ("select * from {product}")-> query ();

// Sets the cache to never expire and depends on the global variable 'version'
Yii: app ()-> cache-> set ('product ',
$ Products,
0,
NewCGlobalStateCacheDependency ('version ')
);
Echo "get from database <br/> ";
}
Echo json_encode ($ products );

Write thread code:

$ Pro = Yii: app ()-> db;

$ Command = $ pro-> createCommand ("INSERTINTO {product} ('name', 'color', 'version') VALUES ('dependency', 'black ', 1 )");

$ Command-> execute ();

// Set cache dependencies to expire the cache

$ Version = Yii: app ()-> getGlobalState ('version ');

Yii: app ()-> setGlobalState ('version', ++ $ version );

The preceding business logic is as follows:

Each time the read thread obtains the cache, it verifies whether the cache dependency value has changed. If the cache dependency value changes, the cache cannot be obtained. If the cache dependency value does not exist, the read thread queries the database, after the results are queried, reset the cache. If the cache dependency value does not change, directly obtain the cache, no longer connect to the database, and return the value to the browser. In this way, the read thread completes the task.

The write thread directly operates the database and updates the data to the database. Then, he only needs to update the Denp cache dependency to return to the browser. Of course, the write thread can also directly clear the corresponding cache, in this way, the write thread completes the task.

When you want to update cache media, such as Memcached for distributed cache media, the premise is that the web server installs the memcached extension of php, in the web program, you only need to make the following changes in the main configuration file:

'Components' => array (
// Set Cache
'Cache' => array (
'Class' => 'cmcache ',
'Servers' => array (
Array ('host' => '192. 168.1.16', 'Port' => 192, 'weight' => 60 ),
Array ('host' => '192. 168.1.16', 'Port' => 192, 'weight' => 40 ),
),
),
),

For more details, please continue to read the highlights on the next page:

  • 1
  • 2
  • Next Page

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.