I would like to ask where there is a learning material for PHPmemcached, a recent graduate. I want to learn about memcached and have already set up the environment. Unfortunately, I cannot find the specific learning materials for PHP & nbsp; memcached, the PHP Manual is incomplete. could you tell me the website or materials? thank you ~ ------ Solution ---------------------- & lt ;? Php ** & nbsp; * & nbsp; CMemCac: where are PHP memcached learning materials?
I was a fresh graduate who wanted to learn about memcached and set up the environment. Unfortunately, I cannot find the specific learning materials for PHP memcached,
The PHP Manual is incomplete. could you tell me the website or materials? thank you ~
------ Solution ----------------------
/**
* 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 be configured with a list of memcache servers by settings
* Its [email protected] setServers servers} property. By default, CMemCache assumes
* There is a memcache server running on localhost at port 11211.
*
* See [email protected] CCache} manual for common cache operations that are supported by CMemCache.
*
* Note, there is no security measure to protected data in memcache.
* All data in memcache can be 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, 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 deployments. 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 deployments
*/
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
------ Solution ----------------------
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 invocations. Each element is a [email protected] CMemCacheServerConfiguration }.
*/
Public function getServers ()
{
Return $ this-> _ servers;
}
/**
* @ Param array $ config list of memcache server invocations. 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
------ Solution ----------------------
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 shoshould be flagged as online upon a failure
*/
Public $ status = true;
/**
* Constructor.
* @ Param array $ config list of memcache server deployments.
* @ 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 .'));
}
}
CCache inherited by the cache class attached to YII can implement CRUD4 methods by default .. Add a value under your Baidu account. just get it. don't think it's too complicated.
------ Solution ----------------------
Let's take a look at the PHP Manual. In fact, the most useful thing is get set replace delete.