Ask where to find PHP memcached learning materials
Originally a fresh graduate, want to learn a memcached, has set up the environment, but can not find PHP memcached specific learning materials,
PHP Manual is very incomplete, ask God to introduce Web site or information thank you ~
------to solve the idea----------------------
/**
* Cmemcache class file
*
* @author Qiang Xue <[email protected]>
* @link http://www.yiiframework.com/
* @copyright 2008-2013 Yii software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* Cmemcache implements a cache application component based on [email protected] http://memcached.org/memcached}.
*
* Cmemcache can configured with a list of memcache servers by settings
* Its [e-mail protected] setservers servers} property. By default, Cmemcache assumes
* There is a memcache server running on localhost at Port 11211.
*
* See [e-Mail protected] CCache} manual for common caches operations that is supported by Cmemcache.
*
* Note, there is no security measure to protected data in Memcache.
* All data in memcache can is accessed by any process running in the system.
*
* To use Cmemcache as the cache application component, configure the application as follows,
*
* Array (
* ' components ' =>array (
* ' cache ' =>array (
* ' class ' = ' Cmemcache ',
* ' servers ' =>array (
* Array (
* ' host ' = ' server1 ',
* ' Port ' =>11211,
* ' weight ' =>60,
* ),
* Array (
* ' host ' = ' Server2 ',
* ' Port ' =>11211,
* ' weight ' =>40,
* ),
* ),
* ),
* ),
* )
* In the above, the Memcache servers is 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 is used with [email protected] http://pecl.php.net/package/memcached memcached}.
* To does 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 C Omponent.
* @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} would be used.
* If false [email protected] http://pecl.php.net/package/memcache Memcache}. would 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 was 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
------to solve the idea----------------------
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 is 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 the 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
------to solve the idea----------------------
Boolean the value stored in cache, False if the value was 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 would expire. 0 means never expire.
* @return Boolean True if the value is successfully stored to 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 to the 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 would expire. 0 means never expire.
* @return Boolean True if the value is successfully stored to 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 of the 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'll be used for connecting to the server
*/
Public $timeout = 15;
/**
* @var integer How often a failed server would 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 has ' host ' value. '));
}
Else
throw new CException (Yii::t (' Yii ', ' Cmemcache server configuration must is an array. '));
}
}
The CCache inherited by a Yii cache class can be CRUD4 regardless of the default implementation. Oneself Baidu Next is add a value in get on the line don't think too complicated
------to solve the idea----------------------
This look at the PHP manual, in fact, with more than the Get set replace delete key to see it.