How to use Memcache in Yii framework

Source: Internet
Author: User
This article mainly introduces the method of using Memcache in Yii framework, analyzes the configuration, usage and precautions of using memcache in the YII framework, and the need of friends can refer to

This paper describes the method of using Memcache in the YII framework. Share to everyone for your reference, as follows:

The memcache can be easily used in Yii

I. Configuration

Add the cache configuration to the main.php components


Array (' Components  ' =>array ('    cache ' =>array ('      class ' = ' Cmemcache ',      ' servers ' =>array (        Array (          ' host ' = ' server1 ',          ' Port ' =>11211,          ' weight ' =>60,        ),        array (          ' Host ' = ' server2 ',          ' Port ' =>11211,          ' weight ' =>40,        ),    ),),  

Description

1) class indicates all cache classes, Cmemcache is Memcache
2) You can assign a weight (weight) to each cache when you want to use multiple cache polling. If you have only one cache, do not add this configuration.

Two. Use

The method of calling Memcache is also very simple


$key = ' People '; $value = Json_encode (Array (' name ' = ' ball ', ' age ' = ' male ')); $expire = 10;yii::app ()->cache- >set ($key, $value, $expire); Yii::app ()->cache->get ($key); Yii::app ()->cache->delete ($key);

For more APIs, see the Yii Manual Cmemcache section.

Three. Questions

When you use the extension of Yii to access memcache, there is no problem. But in reality there is a situation where you are using YII to develop the backend, but the front-end students do not use YII, but use the native Memcache API to manipulate the data. You will encounter some problems at this time.

As shown in the example above, you will find that if you use the native Memcache api,memcache::get, you cannot get the value with the People key!

The reason for this is that Yii's set handles both key and value. By default, the key is prefixed with the MD5 transform, and value is serialize (). It is no wonder that the use of memcache::get (' people ') does not take the value.

Solve:

The simple method can be configured so that Yii's memcache extension does not perform additional "gestures".


' Cache ' = = Array (  ' class ' = ' System.caching.CMemCache ',  ' servers ' = = Array (' host ' = ') ' 127.0.0.1 ', ' port ' = + 11211),  ' keyprefix ' = ', '  hashkey ' = False,  ' serializer ' = false)

which

keyprefix: The prefix of key, which is generated by default with the Capplication::getid () method.
HashKey: Hash operation on key, default MD5
Serializer: Serialization method of value, default serialize

By placing these all empty, the extended behavior is consistent with the native memcache.

Four. More Memcache method calls

In the extension of Yii, only the most common operations of memcache, and there is no such operations as native memcache decrement and increment. What if you want to work with these methods? You can use the Getmemcache method in the extension to get the native memcache, and then you can use the native method.


$mem = Yii::app ()->cache->getmemcache () $res = $mem->decrement ($key);

When using native methods, it is better to set the configuration mentioned in (iii) above, otherwise it would be troublesome to use both the Yii extension method and the Memcache native method.

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.