您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

求问哪里有PHP memcached的学习资料,该如何处理

2024/6/27 21:50:31发布26次查看
求问哪里有php memcached的学习资料
本来应届毕业生一枚,想要学习一下memcached,已经搭建好了环境,可惜找不到php memcached具体的学习资料,
php手册上的十分不全,求大神介绍网址或者资料谢谢~
------解决思路----------------------
array(
* 'cache'=>array(
* 'class'=>'cmemcache',
* 'servers'=>array(
* array(
* 'host'=>'server1',
* 'port'=>11211,
* 'weight'=>60,
* ),
* array(
* 'host'=>'server2',
* 'port'=>11211,
* 'weight'=>40,
* ),
* ),
* ),
* ),
* )
*
* 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
* @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
* @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的cache类  继承的ccache可以不管  默认实现crud4个方法。。自己百度下  就是add个值 在get就行 别想太复杂
------解决思路----------------------
这个看php手册吧,其实用得取多的是get set replace delete 重点看看就可以了.
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product