Home  >  Article  >  Backend Development  >  Introduction to using memcache in YII

Introduction to using memcache in YII

巴扎黑
巴扎黑Original
2017-08-06 16:14:261161browse

This article mainly introduces the method of using memcache in the YII framework, and analyzes the configuration, usage and precautions of using memcache in the Yii framework in the form of examples. Friends in need can refer to the following

The examples of this article are described Learn how to use memcache in the YII framework. Share it with everyone for your reference, the details are as follows:

Memcache can be easily used in yii

1. Configuration

Add cache configuration to the components of main.php


array(
  'components'=>array(
    'cache'=>array(
      'class'=>'CMemCache',
      'servers'=>array(
        array(
          'host'=>'server1',
          'port'=>11211,
          'weight'=>60,
        ),
        array(
          'host'=>'server2',
          'port'=>11211,
          'weight'=>40,
        ),
      ),
    ),
  ),
)

Instructions:

1) class specifies all cache classes, CMemCache is memcache
2) When multiple cache polling is to be used, each cache can be assigned a weight. If there is only one cache, you can do it without adding this configuration.

2. The method of calling memcache using

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, please refer to the CMemCache section of the Yii manual.

3. Problem

When you uniformly use the yii extension to access memcache, there will be no problem. But in reality, there will be a situation: you are using Yii to develop the backend, but the classmates at the front desk do not use Yii, but use the native memcache API to operate the data. You will encounter some problems at this time.

As shown in the example in Part 2 above, you will find that if you use the native memcache api, Memcache::get, and use people as the key, you will not be able to get the value!

The reason is that yii's set processes both key and value. By default, the key is prefixed and md5 transformed, and the value is serialized(). It's no wonder that you can't get the value using Memcache::get('people').

Solution:

The simple method is to configure the memcache extension of yii not to perform additional "small actions".


'cache' => array(
  'class' => 'system.caching.CMemCache',
  'servers' => array( array('host' => '127.0.0.1', 'port' => 11211)),
  'keyPrefix' => '',
  'hashKey' => false,
  'serializer' => false)

Among them:

keyPrefix: The prefix of the key, generated by the CApplication::getId() method by default.
hashKey: perform hash operation on key, default md5
serializer: serialization method of value, default serialize

will leave these blank. The extended behavior is consistent with native Memcache.

4. More memcache method calls

In the yii extension, there are only the most common operations of memcache, and there is no decrement similar to the native memcache. and increment operations. What if you want to use these methods? You can use the getMemCache method in the extension to obtain the native memcache object, and then you can use the native method.


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

When using the native method, it is best to set the configuration mentioned in (3) above, otherwise using yii's extension method and memcache's native method at the same time will cause trouble.

The above is the detailed content of Introduction to using memcache in YII. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn