In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how PHP's APCu extension is carried out. Many people may not know much about it. In order to let everyone know more, Xiaobian summarizes the following contents for everyone. I hope everyone can gain something according to this article.
PHP APCu extension
Presumably everyone has used memcached or redis such a cache system to do daily caching, or to resist traffic, or to save some commonly used hot data, in fact, in small projects, PHP has also prepared a simple cache system for us, fully capable of coping with our daily ordinary scale site development. This extension is called the APCu extension.
APCu Extension
The APCu extension is an upgrade of the APC extension, which is no longer maintained. Both extensions are based on opcode caching. PHP's own opcode to achieve caching capabilities.
The installation of APCu is as simple as a normal PHP extension, and the main thing is that the extension is very small. Both download and install can be done in seconds. Therefore, it can be easily applied to small-scale projects, and it is natively supported by PHP, without the need for additional port configuration.
method statements
Cache system generally have add, delete, query, self-increment and other functions in the APCu extension has the corresponding implementation.
apcu_add -Create a new cache apcu_cache_info -View all cache information of APCu apcu_cas -Update a cache value to a new value apcu_clear_cache -Clear all caches apcu_dec -Self-decrement cache value apcu_delete -Delete the contents of a cache apcu_enabled -Whether APCu cache is enabled in the current environment apcu_entry -Atomically generate a cache entity apcu_exists -Check if cache exists apcu_fetch -query cache apcu_inc -auto-increment cache value apcu_sma_info -query cache shared memory information apcu_store -save a cache use demo apcu_add("int", 1);
apcu_add("string", "I'm String");
apcu_add("arr", [1,2,3]);
class A{
private $apc = 1;
function test(){
echo "s";
}
}
apcu_add("obj", new A);
var_dump(apcu_fetch("int"));
var_dump(apcu_fetch("string"));
var_dump(apcu_fetch("arr"));
var_dump(apcu_fetch("obj"));
Normal use is relatively simple, we add various types of data can be stored in the cache normally. However, it should be noted that we can save the object directly into the APCu cache without serializing it or JSON into a string. The system will automatically serialize it for us.
apcu_add(string $key , mixed $var [, int $ttl = 0 ]) method is ordinary to add a cache,$ttl can set the expiration time, also in seconds, if not set is long-term effective. Note that the cache time limit of APCu is valid in one CLI, and the cache content set in the previous CLI cannot be retrieved by calling CLI again. In PHP-FPM, cache invalidation occurs after restarting PHP-FPM or FastCGI.
Let's focus on testing a few less common methods.
apcu_cas("int", 1, 2);
var_dump(apcu_fetch("int"));
// Warning apcu_cas() expects parameter 2 to be int
apcu_cas("string", "I'm String", "I'm New String");
apcu_cas(string $key , int $old , int $new) is to modify a $old value to a $new value, it can only modify the content of numeric types, if it is a string modification will report an error. What are the advantages of this function? Its greatest advantage is that it is atomic, i.e., immune to high concurrency. Similar to this is the apcu_store(string $key , mixed $var [, int $ttl = 0 ]) method, but this method simply modifies the contents of a cache, if the cache key does not exist, then create a new one, it is not restricted by type, and certainly not atomic.
apcu_entry("entry", function($key){
return "This is " . $key;
});
var_dump(apcu_fetch("entry"));
apcu_entry(string $key , callable $generator [, int $ttl = 0 ]) This function performs the anonymous $generator function if the $key cache does not exist, passing $key as the key value, and then generating a return as the cache value.
var_dump(apcu_cache_info());
Finally, if we want to see all the APCu cache information in the current system, we can use this apcu_cache_info() function directly.
When the cache is very large, it also provides an APCUIterator iterator to facilitate our circular query of cache information and related statistics. In short, this system is a very convenient set of small-scale cache system, in daily development can try to use some small functions.
After reading the above, do you know more about how PHP's APCu extension works? If you still want to know more knowledge or related content, please pay attention to the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.