YiiMemCache application instance

Source: Internet
Author: User
Okay, data processing is complete. now we only need to call the relevant data in views/main. php. The keyword is used as an example.

1. create a table structure

?
123456 Create table if not exists 'config' ('name' varchar (128) not null, 'value' varchar (128) not null, KEY 'name' ('name '), KEY 'value' ('value') ENGINE = MyISAM default charset = utf8 COMMENT = 'site info table ';

First, we need to explain that because the website name, keyword, and other information are a single call, we do not need to create a primary key or auto-increment ID, as long as we do the two-dimensional assembly during storage or extraction. The following is an example!

2. configure memcache

Because Yii can configure multiple cache instances, it is very convenient to call. if one of the configurations is memcache, we only need Yii: app () -> memcache-> set ($ key, $ value );

Configure the instance config/main. php

?
123456789101112 'Components' => array ('memcache' => array ('class' => 'cmcache', 'servers' => array ('host' => '123. 0.0.1 ', 'port' => 11211, 'weight' => 60 ,),),),),

3. configure full-site call

How can I configure full-site calls? Because each Controller inherits components/Controller, we can configure a global array attribute here to call this attribute anywhere on the entire site!

Create Public attributes

?
1 Public $ global = array ();
Attribute value


?
1234 Publicfunctioninit () {$ this-> global = $ this-> setKey ();}
Assemble an array?
1234567891011121314 PrivatefunctionsetKey () {if (! Yii: app ()-> memcache-> get ('keyword') {$ cache = Config: model ()-> findAll (); foreach ($ cacheas $ key = >$ n) {$ keys [$ n-> attributes ['name'] = $ n-> attributes ['value'];} yii: app ()-> memcache-> set ('keyword', $ keys);} returnYii: app () -> memcache-> get ('keyword ');}
Through the above array assembly, we can get how to assemble the array to update the database.

Okay, data processing is complete. now we only need to call the relevant data in views/main. php. The keyword is used as an example.

?
1
Done!

Original article. For more information, see the source!

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.