Memcache is a project of danga.com. It was first used to serve LiveJournal. Currently, many people around the world use this caching project to build their own large-load websites to share the pressure on the database. It can handle any number of connections and uses non-blocking network IO. Since its working mechanism is to open up a space in the memory and then create a HashTable, Memcached manages these HashTables by itself. Memcache official website: http://www.danga.com/memcached, more detailed information can be found here.
Why are there two names: Memcache and memcached? In fact, Memcache is the name of this project, and memcached is the name of its main program file on the server side. You know what I mean~~~~. One is the project name and the other is the main program file name.
It is divided into two processes: memcache server installation and memcached client installation.
The so-called server-side installation is to install Memcache on the server (usually a Linux system) to store data.
The so-called client installation refers to PHP (or other programs, Memcache also has other good API interfaces) to use the functions provided by Memcache on the server side. PHP needs to be added with extensions.
The memcache module is an efficient daemon process that provides procedural programs and object-oriented convenient interfaces for memory caching, especially for reducing database access when designing dynamic web programs.
Memcache also provides processing for communication conversations (session_handler).
More information about the Memcache module can be found at http://www.danga.com/memcached/.
名称 | 默认值 | 是否可变 | 改变日志 |
memcache.allow_failover | “1” | PHP_INI_ALL | Available since memcache 2.0.2. |
memcache.max_failover_attempts | "20" | PHP_INI_ALL | Available since memcache 2.1.0. |
memcache.chunk_size | "8192" | PHP_INI_ALL | Available since memcache 2.0.2. |
memcache.default_port | "11211" | PHP_INI_ALL | Available since memcache 2.0.2. |
memcache.hash_strategy | "standard" | PHP_INI_ALL | Available since memcache 2.2.0. |
memcache.hash_function | "crc32" | PHP_INI_ALL | Available since memcache 2.2.0. |
session.save_handler | "files" | PHP_INI_ALL | Supported since memcache 2.1.2 |
session.save_path | "" | PHP_INI_ALL | Supported since memcache 2.1.2 |
For further details and definitions of PHP_INI_* constants, see the PHP manual php.ini configuration options.
memcache.allow_failover Boolean
Whether to transparently failover to other servers when an error occurs (note: failover is a verb).
memcache.max_failover_attempts integer
Define the server's quantity class settings and get data, only used in conjunction with memcache.allow_failover.
memcache.chunk_size integer
Data will be divided into chunks of the specified size (chunk_size) for transmission. The smaller this value (chunk_size), the more write operation requests are required. If you find other unexplained slowdowns, please try to increase this value. Up to 32768.
memcache.default_port string
When connecting to the memcache server, if no port is specified, the default tcp port will be used.
memcache.hash_strategy string
Control which strategy is used when mapping keys to servers. Setting this value consistently enables the hashing algorithm to be used consistently and will not be remapped when the server accepts additions or removals of variables from the pool. Setting this value gives standard results when the old strategy is used.
memcache.hash_function string
Control which hsah function is applied to the key mapping process to the server. The default value "crc32" uses the CRC32 algorithm, while "fnv" indicates the use of the FNV-1a algorithm.
session.save_handler string
Determine to use memcache for communication session processing (session handler) by setting this value to memcache.
session.save_path string
Define the delimiter for each server link used for call storage, for example: "tcp://host1:11211, tcp://host2:11211".
Each server link can contain parameters that are accepted by that server, similar to servers added using Memcache::addServer(), for example: "tcp://host1:11211?persistent=1&weight=1&timeout=1& amp ; amp;retry_interval=15".
名称 | 类型 | 描述 |
MEMCACHE_COMPRESSED | integer | 用于调整在使用 Memcache::set(), Memcache::add() 和 Memcache::replace() 几个函数时的压缩比率。 |
MEMCACHE_HAVE_SESSION | integer | 如果通信对话的处理(session handler)被允许使用其值为 1,其他情况值为 0。 |
Connect to memcache server
$host(string) Server domain name or ip
$port(int) Server tcp port number, the default value is 11211
$timeout The expiration time of the connection memcache process. Think twice before modifying its default value of 1, so as not to lose all the advantages of memcache caching and cause the connection to become very slow.
Returns true if successful, false if failed
Connect to the server in constant connection mode
$host(string) Server domain name or ip
$port(int) Server tcp port number, the default value is 11211
$timeout The expiration time of the connection memcache process. Think twice before modifying its default value of 1, so as not to lose all the advantages of memcache caching and cause the connection to become very slow.
Returns true if successful, false if failed
Close the object (does not work for constant connections)
Returns true if successful, false if failed
Add a server to the object (Note: addServer does not have the action of connecting to the server, so when the memcache process is not started, successful execution of addServer will also return true)
host Server domain name or IP
port Port number, the default is 11211
persistent Whether to use persistent connections, the default is TRUE
weight Weight, the proportion in multiple server settings
timeout The number of seconds after which the connection to the server fails. Please think twice when changing the default value of 1. You may lose all cache advantages and cause the connection to become very slow
retry_interval The retry frequency when the server connection fails. The default is once every 15 seconds. If set to -1, automatic retry will be disabled. When the extension is loaded dynamically via dl(), regardless of this parameter or the constant connection setting parameter All will fail.
Each failed server has its own lifetime before it expires. When selecting a backend request, it will be skipped and the request will not be served. An expired connection will be successfully reconnected or marked as a failed connection waiting for the next time Try again. This effect means that the retry connections of each web server's child process when serving the page are related to their own retry frequency.
status Controls whether the server is marked as online, set this parameter to FALSE and set retry_interval is -1 Servers that fail to connect can be put into a server pool that describes unresponsive requests. Requests to this server will fail. Accept the settings set to failed servers. The default parameters are TRUE, indicating that the server can be defined as online.
failure_callback The callback function when failure occurs. The two parameters of the function are the hostname and port of the failed server
Returns TRUE on success and FALSE on failure.
Note: When testing the addServer function, we mainly tested its parameters retry_interval and status
2.4.4.1.Testing of retry_interval parameter
$mem = new Memcache;
In the above example, if the localhost server goes down or the memcache daemon crashes, the connection will be automatically retried 15 seconds after the request fails to connect to the server. Connect to the server, but will not connect to the server within these 15 seconds. That is, as long as there is a request, it will try to connect to the server within 15 seconds, but each server connection retry is independent. For example, if I add two One of the servers is localhost and the other is 172.16.100.60. They are counted from the time when their respective connections failed. As long as there is a request for their respective servers, they will go every 15 seconds. Connect to their respective servers.
2.4.4.2. Combination of retry_interval and status
$mem = new Memcache;retrt_interval=-1 above, status=false In this case, the server that failed to connect is placed in a pool that does not respond to requests, so it has no impact on the key allocation algorithm, and it returns an error immediately. Whether it fails or fails over depends on the setting of memcache.allow_failover. Execute set, add, Requests such as replace and get will fail and return false, even if the memcache process is running normally.
In addition to being used in combination with retry_interval, the use of status alone will have an impact on the results obtained by the function memcache::getServerStatu
Regardless of whether the memcache process is running normally or crashed, when the status is true, the result of getServerStatus is true, otherwise it is false
But when the memcache process is running normally, it has no impact on functions such as set, add, replace, get, etc.
Add a data to be cached. If the key as the cached data does not yet exist on the server,
key The key of cached data. Its length cannot exceed 250 characters
var value, integer type will be stored directly, other types will be serialized and stored, the maximum value is 1M
flag Whether to use zlib compression? When flag=MEMCACHE_COMPRESSED, zlib compression will not be used when the data is very small. Only when the data reaches a certain size, zlib compression will be performed on the data. (There is no specific test data for the minimum value for compression)
expire Expiration time, 0 means never expires, you can use unix timestamp format or the number of seconds from the current time, when set to seconds, it cannot be greater than 2592000 (30 days)
Returns TRUE on success and FALSE on failure. If the key already exists, memcache:;add() behaves similarly to memcache::set in other respects
Replace the cache variable content of a specified existing key
key The key of cached data, its length cannot exceed 250 characters
var value, integer type will be stored directly, other types will be serialized and stored, the maximum value is 1M
flag Whether to use zlib compression? When flag=MEMCACHE_COMPRESSED, zlib compression will not be used when the data is very small. Only when the data reaches a certain size, zlib compression will be performed on the data. (There is no specific test data for the minimum value for compression)
expire Expiration time, 0 means never expires, you can use unix timestamp format or the number of seconds from the current time, when set to seconds, it cannot be greater than 2592000 (30 days)
Returns TRUE on success and FALSE on failure.
Set the cache variable content of a specified key
key The key of cached data, its length cannot exceed 250 characters
var value, integer type will be stored directly, other types will be serialized and stored, the maximum value is 1M
flag Whether to use zlib compression? When flag=MEMCACHE_COMPRESSED, zlib compression will not be used when the data is very small. Only when the data reaches a certain size, zlib compression will be performed on the data. (There is no specific test data for the minimum value for compression)
expire Expiration time, 0 means never expires, you can use unix timestamp format or the number of seconds from the current time, when set to seconds, it cannot be greater than 2592000 (30 days)
Returns TRUE on success and FALSE on failure.
string Memcache::get ( string $key [, int &$flags ] )
array Memcache::get ( array $keys [, array &$flags ] )
Get the variable cache value of a key
key The key of the cached value
flags If a variable is passed, the flag result of getting the cached value set or added will be stored in the variable
Returns the cached variable content of the specified key or returns FALSE on failure or the value of the variable does not exist
If none of the keys in the outgoing key array exist, the returned result is an empty array, otherwise an associative array of keys and cached values is returned
删除某一个变量的缓存
key 缓存的键 键值不能为null和‘’,当它等于前面两个值的时候php会有警告错误。
timeout 删除这项的时间,如果它等于0,这项将被立刻删除反之如果它等于30秒,那么这项被删除在30秒内
成功返回 TRUE,失败返回 FALSE。
2.9.4.范例
/* procedural API */清空所有缓存内容,不是真的删除缓存的内容,只是使所有变量的缓存过期,使内存中的内容被重写
成功返回 TRUE,失败返回 FALSE。
Get all server extension static information
type Static information type. Valid values include {reset, malloc, maps, cachedump, slabs, items, sizes}. This optional parameter is agreed according to certain rules. This optional parameter is a title entered to facilitate developers to view different categories of information
slabid is used to jointly set the cache heap to be a valid slice into the heap according to the specified type. The cache heap is command bound to the server and is strictly for debugging purposes
limit is used to jointly set the size of the cache heap to the heap limited by the entered number according to the specified type. The default value is 100
Returns a two-dimensional array of static information extended by the server, and returns FALSE on failure
<br />输出结果
2.12.2. Parameters
slabid is used to jointly set the cache heap to be a valid slice into the heap according to the specified type. The cache heap is command bound to the server and is strictly for debugging purposes
limit is used to jointly set the size of the cache heap to the heap limited by the entered number according to the specified type. The default value is 100
2.12.3. Return value
2.13.Memcache::getServerStatus
host server domain name or IP
port port number, default is 11211
Returns server status, 0 means failure, otherwise returns a non-0 number
Get the version number information of the server
Returns the version number string of the server successfully, returns FALSE on failure
Set compression limit
threshold sets the minimum value for controlling the variable length of automatic compression
min_saving specifies the minimum compression ratio. The value must be between 0 - 1. The default is 0.2, which represents a compression ratio of 20%.
Returns TRUE on success and FALSE on failure.
Function added after Memcache version 2.1.0, set server parameters at runtime
host Server domain name or IP
port port number, default is 11211
timeout The number of seconds for timeout connection failure. Please think twice when changing the default value of 1. You may lose all cache advantages and cause the connection to become very slow
retry_interval The retry frequency when the server connection fails. The default is once every 15 seconds. If set to -1, automatic retry will be disabled. When the extension is loaded dynamically via dl(), regardless of this parameter or the constant connection setting parameter All will fail. Each failed server has its own lifetime before it expires. When selecting a backend request, it will be skipped and the request will not be served. An expired connection will be successfully reconnected or marked as failed pending the next retry. This effect means that the retry connections of each web server's child process when serving the page are related to their own retry frequency.
status controls whether the server is marked online. Setting this parameter to FALSE and setting retry_interval to -1 can cause the server that failed to connect to be placed in a server pool that describes the server that does not respond to requests. Requests to this server will fail. Accepts the setting of a failed server. The default parameter is TRUE, which means that the server can be defined as online.
failure_callback The callback function when failure occurs. The two parameters of the function are the hostname and port of the failed server
Returns TRUE on success and FALSE on failure.
Give an increment to the cache variable of the specified key. If the variable is not a number, it will not be converted into a number. This increment will be added to the original number of the variable. If the variable does not exist, no new variable will be added. For Do not use this function to compress stored variables because the corresponding value method will fail.
key key of cached value
var value, integer type will be stored directly, other types will be serialized and stored
Returns the new variable value successfully, returns FALSE on failure.
Give a decrement value to the cache variable of the specified key. Similar to the increment operation, this value will be subtracted from the original variable. The value of the item will be subtracted after being converted into a number. The value of the new item will not be less than 0. Do not use this function for compressed stored variables because the corresponding value method will fail.
key key of cached value
var value, integer type will be stored directly, other types will be serialized and stored
Returns the new variable value successfully, returns FALSE on failure.
Set whether the memcache debugger is enabled, the value is TRUE or FALSE. Affected by whether the --enable-debug option is used during php installation, this function will only return TRUE if used, otherwise it will always return FALSE.
on_off sets whether debugging mode is on, TRUE is on, FALSE is off
If php is installed with the --enable-debug option, it returns TRUE, otherwise it returns FALSE.
Redirected to: http://www.cnblogs.com/whoamme/p/3437146.html