Home Backend Development PHP Tutorial Introduction to the extension functions of the memcache class

Introduction to the extension functions of the memcache class

Jul 04, 2018 pm 03:52 PM

This article mainly introduces the extension function of the memcache class. It has a certain reference value. Now I share it with you. Friends in need can refer to it

Memcache — Memcache class

/****connect****/
1.Memcache::connect – Create a Memcache object
Syntax: bool Memcache::connect ( string $host [, int $port [, int $timeout ]] )
Returns TRUE if successful, returns FALSE if failed
Case reference: $memcache_obj->connect('memcache_host', 11211);

2.Memcache::pconnect – Create a Memcache Persistent connection object
Syntax: bool Memcache::pconnect (string $host [, int $port [, int $timeout ]] )
Returns TRUE if successful, returns FALSE if failed
Parameters:
$host: Points to the host of the link that memcached is listening to. This parameter will have another special connection method unix:///path/to/memcached.sock, which uses unix domain name sockets. In this case, The port must be set to 0
$port: Points to the port of the link that memcached is listening to. In the case of unix domain name sockets, the port must be set to 0
$timeout: The number of seconds used to connect to the daemon. When When you change the default value of 1 second, you need to consider that if your connection is too slow, you may lose the advantages of caching.

/****Add to****/ ※Please note that when adding an object, you must use a new class name
1.Memcache::set – Add a value, if it already exists, overwrite it
Syntax: bool Memcache::set ( string $key , mixed $var [, int $flag [, int $expire ]] )
Add a value, if it already exists, overwrite it; return TRUE if successful, return FALSE if failed .

2.Memcache::add – Add a value, if it already exists, return false
Syntax: bool Memcache::add ( string $key , mixed $var [, int $flag [, int $expire ]] )
Returns TRUE if successful, returns FALSE if failed. If the $key value already exists, FALSE will be returned
Case reference: $memcache_obj->add('var_key', 'test variable', false, 30);

3.Memcache::replace - Overwrite an existing key
Syntax: bool Memcache::replace (string $key, mixed $var [, int $flag [, int $expire]])
Return TRUE if successful , returns FALSE on failure. If the $key value already exists, FALSE will be returned.
Parameters:
$key: The key value to be stored.
$var: The stored value, character type and integer type will be saved as the original value, other types will be automatically serialized and saved later.
$flag: Whether to use MEMCACHE_COMPRESSED to compress the stored value, true means compression, false means no compression.
$expire: The expiration time of the stored value. If it is 0, it means it will not expire. You can use a unix timestamp or description to represent the time from now, but when you use seconds to express it, it should not exceed 2592000 seconds. (meaning 30 days).

/****Get value****/
1.Memcache::get – Get a key value
Syntax: string Memcache::get ( string $key [, int &$flags ] )
        array Memcache::get (array $keys [, array &$flags])
If successful, return the value corresponding to the key, if failed, return false.
Parameters:
$key is Key value or an array value of a key.
$flags If this parameter exists, then $flags is related to the value written to this parameter. These $flags are similar to the $flags in the Memcache::set() function.

/****delete****/
1.Memcache::delete – delete a key value
Syntax: bool Memcache::delete ( string $key [, int $timeout ] )
Return TRUE if successful, return FALSE if failed.

2.Memcache::flush – Clear all cached data
Syntax: bool Memcache::flush (void)
Returns TRUE if successful, returns FALSE if failed.

/****Modify value****/ ※Change the stored value
1.Memcache::decrement – ​​Subtract the value in a saved key
Syntax: int Memcache: :decrement ( string $key [, int $value ] )
If successful, return the reduced value, if failed, return false.

2.Memcache::increment - Add the value in a saved key
Syntax: int Memcache::increment (string $key [, int $value])
If On success, the reduced value is returned, and on failure, false is returned.
Parameters:
Key: the name of the key you want to reduce
Value: the value you want to reduce
Case reference: $memcache->increment('test_item', 4);

/****closure****/
1.Memcache::close – Close a Memcache object
Syntax: bool Memcache::close (void)
Returns TRUE if successful, returns if failed FALSE.

/****Configuration****/
1.Memcache::addServer – Add a server address that can be used
Syntax: bool Memcache::addServer (string $host [, int $ port [, bool $persistent [, int $weight [, int$timeout [, int $retry_interval [, bool $status [, callback $failure_callback ]]]]]] )
Returns TRUE if successful, otherwise Return FALSE.
Parameters:
Whether $persistent is a persistent connection
$weightThe weight of this server among all servers

2.Memcache::setServerParams – Modify server parameters at runtime
Syntax: bool Memcache::setServerParams ( string $host [, int $port [, int $timeout [, int$retry_interval [, bool $ status [, callback $failure_callback ]]]]] )
Returns TRUE if successful and FALSE if failed.
Parameters:
$host server address
$port server port
$timeout duration of connection
$retry_interval Interval time between connection retries, the default is 15, set to -1 No retry
$status controls the online status of the server
$failure_callback allows setting a callback function to handle error messages.

/****Get parameters****/
2.Memcache::getServerStatus – Get the status of the running server
Syntax: int Memcache::getServerStatus ( string $host [, int $port ] )
Returns the server status successfully. If the server is not started, 0 will be returned. Other numbers indicate that the server is started.
Parameters:
$host: The host that is listening for the connection
$port The port of the host that is listening for the connection, the default is 11211

3.Memcache::getStats – Returns the server’s Some running statistics
Syntax: array Memcache::getStats ([ string $type [, int $slabid [, int $limit ]]] )
Parameters:
$type indicates the requested return type: reset , malloc, maps, cachedump, slabs, items, sizes;
Used when the first parameter of $slabid is set to "cachedump".
$limit is used when the first parameter is set to "cachedump".

4.Memcache::getVersion – Returns the version information of the running Memcache
Syntax: string Memcache::getVersion (void)
Returns the version information of the server successfully, and returns false when it fails.

/****debug****/
1.memcache_debug – Control debugging function
Syntax: bool memcache_debug (bool $on_off)
If php is compiled using -enable- debug option, returns true, otherwise returns false
Parameters:
$on_off: true means turning on debugging, false means turning off debugging

2.Memcache::getExtendedStats – Get the running status of all processes in the process pool System statistics
Syntax: array Memcache::getExtendedStats ([ string $type [, int $slabid [, int $limit ]]] )
If successful, statistical information will be returned. If failed, false will be returned
Parameters:
$type indicates the type required to be returned: reset, malloc, maps, cachedump, slabs, items, sizes;
$slabid is used when the first parameter is set to "cachedump".
$limit is used when the first parameter is set to "cachedump".

/****compression****/
1.Memcache::setCompressThreshold – Compress data larger than a certain size
Syntax: bool Memcache::setCompressThreshold (int $threshold [, float $min_savings ] )
Returns TRUE if successful, returns FALSE if failed.
Parameters:
The setCompressThreshold method has two parameters. The first parameter indicates the critical point of processing data size, and the second parameter indicates the compression ratio. The default is 0.2.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

How to use instanceof

##The difference and installation between LAMP, LNMP and LNAMP

The above is the detailed content of Introduction to the extension functions of the memcache class. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1253
29
C# Tutorial
1227
24
Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

How do you handle exceptions effectively in PHP (try, catch, finally, throw)? How do you handle exceptions effectively in PHP (try, catch, finally, throw)? Apr 05, 2025 am 12:03 AM

In PHP, exception handling is achieved through the try, catch, finally, and throw keywords. 1) The try block surrounds the code that may throw exceptions; 2) The catch block handles exceptions; 3) Finally block ensures that the code is always executed; 4) throw is used to manually throw exceptions. These mechanisms help improve the robustness and maintainability of your code.

Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Apr 08, 2025 am 12:03 AM

There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

What is the difference between include, require, include_once, require_once? What is the difference between include, require, include_once, require_once? Apr 05, 2025 am 12:07 AM

In PHP, the difference between include, require, include_once, require_once is: 1) include generates a warning and continues to execute, 2) require generates a fatal error and stops execution, 3) include_once and require_once prevent repeated inclusions. The choice of these functions depends on the importance of the file and whether it is necessary to prevent duplicate inclusion. Rational use can improve the readability and maintainability of the code.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? Apr 09, 2025 am 12:09 AM

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

See all articles