PHP memcached の学習教材とその対処方法はどこで見つかりますか?

WBOY
リリース: 2016-06-13 12:10:23
オリジナル
967 人が閲覧しました

PHP memcached の学習教材はどこで見つかりますか?
私は新卒で memcached を学習したいと思っていますが、残念ながら、PHP memcached の特定の学習教材は見つかりません。 > PHP マニュアルには 10 個のポイントがあります。完全ではありません。URL または情報を教えてください。ありがとうございます~

-----解決策のアイデア----------- -----------

<br /><?php<br />/**<br /> * CMemCache class file<br /> *<br /> * @author Qiang Xue <[email&#160;protected]><br /> * @link http://www.yiiframework.com/<br /> * @copyright 2008-2013 Yii Software LLC<br /> * @license http://www.yiiframework.com/license/<br /> */<br /><br />/**<br /> * CMemCache implements a cache application component based on [email&#160;protected] http://memcached.org/ memcached}.<br /> *<br /> * CMemCache can be configured with a list of memcache servers by settings<br /> * its [email&#160;protected] setServers servers} property. By default, CMemCache assumes<br /> * there is a memcache server running on localhost at port 11211.<br /> *<br /> * See [email&#160;protected] CCache} manual for common cache operations that are supported by CMemCache.<br /> *<br /> * Note, there is no security measure to protected data in memcache.<br /> * All data in memcache can be accessed by any process running in the system.<br /> *<br /> * To use CMemCache as the cache application component, configure the application as follows,<br /> * <pre class="brush:php;toolbar:false"><br /> * array(<br /> *     'components'=>array(<br /> *         'cache'=>array(<br /> *             'class'=>'CMemCache',<br /> *             'servers'=>array(<br /> *                 array(<br /> *                     'host'=>'server1',<br /> *                     'port'=>11211,<br /> *                     'weight'=>60,<br /> *                 ),<br /> *                 array(<br /> *                     'host'=>'server2',<br /> *                     'port'=>11211,<br /> *                     'weight'=>40,<br /> *                 ),<br /> *             ),<br /> *         ),<br /> *     ),<br /> * )<br /> * 

* In the above, two memcache servers are used: server1 and server2.
* You can configure more properties of every server, including:
* host, port, persistent, weight, timeout, retryInterval, status.
* See [email protected] http://www.php.net/manual/en/function.memcache-addserver.php}
* for more details.
*
* CMemCache can also be used with [email protected] http://pecl.php.net/package/memcached memcached}.
* To do so, set [email protected] useMemcached} to be true.
*
* @property mixed $memCache The memcache instance (or memcached if [email protected] useMemcached} is true) used by this component.
* @property array $servers List of memcache server configurations. Each element is a [email protected] CMemCacheServerConfiguration}.
*
* @author Qiang Xue <[email protected]>
* @package system.caching
* @since 1.0
*/
class CMemCache extends CCache
{
/**
* @var boolean whether to use memcached or memcache as the underlying caching extension.
* If true [email protected] http://pecl.php.net/package/memcached memcached} will be used.
* If false [email protected] http://pecl.php.net/package/memcache memcache}. will be used.
* Defaults to false.
*/
public $useMemcached=false;
/**
* @var Memcache the Memcache instance
*/
private $_cache=null;
/**
* @var array list of memcache server configurations
*/
private $_servers=array();

/**
* Initializes this application component.
* This method is required by the [email protected] IApplicationComponent} interface.
* It creates the memcache instance and adds memcache servers.
* @throws CException if memcache extension is not loaded
*/
public function init()
{
parent::init();
$servers=$this->getServers();
$cache=$this->getMemCache();
if(count($servers))
{
foreach($servers as $server)
{
if($this->useMemcached)
$cache->addServer($server->host,$server->port,$server->weight);
else
$cache->addServer($server->host,$server->port,$server->persistent,$server->weight,$server->timeout,$server->retryInterval,$server->status);
}
}
else
$cache->addServer('localhost',11211);
}

/**
* @throws CException if extension isn't loaded
* @return Memcache
------解决思路----------------------
Memcached the memcache instance (or memcached if [email protected] useMemcached} is true) used by this component.
*/
public function getMemCache()
{
if($this->_cache!==null)
return $this->_cache;
else
{
$extension=$this->useMemcached ? 'memcached' : 'memcache';
if(!extension_loaded($extension))
throw new CException(Yii::t('yii',"CMemCache requires PHP {extension} extension to be loaded.",
array('{extension}'=>$extension)));
return $this->_cache=$this->useMemcached ? new Memcached : new Memcache;
}
}

/**
* @return array list of memcache server configurations. Each element is a [email protected] CMemCacheServerConfiguration}.
*/
public function getServers()
{
return $this->_servers;
}

/**
* @param array $config list of memcache server configurations. Each element must be an array
* with the following keys: host, port, persistent, weight, timeout, retryInterval, status.
* @see http://www.php.net/manual/en/function.Memcache-addServer.php
*/
public function setServers($config)
{
foreach($config as $c)
$this->_servers[]=new CMemCacheServerConfiguration($c);
}

/**
* Retrieves a value from cache with a specified key.
* This is the implementation of the method declared in the parent class.
* @param string $key a unique key identifying the cached value
* @return string
------解决思路----------------------
boolean the value stored in cache, false if the value is not in the cache or expired.
*/
protected function getValue($key)
{
return $this->_cache->get($key);
}

/**
* Retrieves multiple values from cache with the specified keys.
* @param array $keys a list of keys identifying the cached values
* @return array a list of cached values indexed by the keys
*/
protected function getValues($keys)
{
return $this->useMemcached ? $this->_cache->getMulti($keys) : $this->_cache->get($keys);
}

/**
* Stores a value identified by a key in cache.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function setValue($key,$value,$expire)
{
if($expire>0)
$expire+=time();
else
$expire=0;

return $this->useMemcached ? $this->_cache->set($key,$value,$expire) : $this->_cache->set($key,$value,0,$expire);
}

/**
* Stores a value identified by a key into cache if the cache does not contain this key.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function addValue($key,$value,$expire)
{
if($expire>0)
$expire+=time();
else
$expire=0;

return $this->useMemcached ? $this->_cache->add($key,$value,$expire) : $this->_cache->add($key,$value,0,$expire);
}

/**
* Deletes a value with the specified key from cache
* This is the implementation of the method declared in the parent class.
* @param string $key the key of the value to be deleted
* @return boolean if no error happens during deletion
*/
protected function deleteValue($key)
{
return $this->_cache->delete($key, 0);
}

/**
* Deletes all values from cache.
* This is the implementation of the method declared in the parent class.
* @return boolean whether the flush operation was successful.
* @since 1.1.5
*/
protected function flushValues()
{
return $this->_cache->flush();
}
}

/**
* CMemCacheServerConfiguration represents the configuration data for a single memcache server.
*
* See [email protected] http://www.php.net/manual/en/function.Memcache-addServer.php}
* for detailed explanation of each configuration property.
*
* @author Qiang Xue <[email protected]>
* @package system.caching
* @since 1.0
*/
class CMemCacheServerConfiguration extends CComponent
{
/**
* @var string memcache server hostname or IP address
*/
public $host;
/**
* @var integer memcache server port
*/
public $port=11211;
/**
* @var boolean whether to use a persistent connection
*/
public $persistent=true;
/**
* @var integer probability of using this server among all servers.
*/
public $weight=1;
/**
* @var integer value in seconds which will be used for connecting to the server
*/
public $timeout=15;
/**
* @var integer how often a failed server will be retried (in seconds)
*/
public $retryInterval=15;
/**
* @var boolean if the server should be flagged as online upon a failure
*/
public $status=true;

/**
* Constructor.
* @param array $config list of memcache server configurations.
* @throws CException if the configuration is not an array
*/
public function __construct($config)
{
if(is_array($config))
{
foreach($config as $key=>$value)
$this->$key=$value;
if($this->host===null)
throw new CException(Yii::t('yii','CMemCache server configuration must have "host" value.'));
}
else
throw new CException(Yii::t('yii','CMemCache server configuration must be an array.'));
}
}

ログイン後にコピー
YII キャッシュ クラスをポストします。継承された CCache はデフォルトで CRUD4 メソッドを実装できます。 。 Baidu で値を追加するだけで取得できます。あまり複雑に考える必要はありません

-----ソリューションのアイデア------ -- これについては PHP マニュアルを読んでください。実際、最も実用的なのは get set replace delete であり、それに焦点を当てることです。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート