In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail the introduction and usage of PHP's efficient cache extension Yac. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
Efficient caching extension of PHP: Yac what is Yac
In fact, from the name, we can see that this is the work of Brother Bird. After all, he is the core developer of PHP, and his work will not let us down every time. Brother Bird can be said to be the pride of our Chinese programmers, he has a pivotal position in the PHP world, you can search his blog, although the update frequency is not high, but every article is worth learning.
Yac is a lock-free shared cache system, and because there are no locks, it is very efficient. It is said that Apc is more than twice as efficient as Memcached, while Yac is faster than Apc. This is its greatest feature.
Yac is more lightweight than Memcached or Redis, and we don't need to install any other software on the server, just install this extension to use it. For small systems, especially for simple data caching systems, we do not need complex data types, only the expansion of this programming language can make our development more convenient and fast.
The way to install it is also very simple, just do the same extension installation after downloading the installation package from PECL.
Basic operation
For cache-related operations, nothing more than adding, modifying, and deleting caches. Unlike external caching systems, the cache of the PHP extension class can hold these data types directly when saving arrays or objects, without serializing them into strings or converting them to JSON strings, which is one of the advantages of Apc and Yac.
Add, get cache $yac = new Yac ()
$yac- > add ('asides,' value a')
$yac- > add ('baked, [1Jing 2jue 3jue 4])
$obj = new stdClass
$obj- > v = 'obj v'
$yac- > add ('obj', $obj)
Echo $yac- > get ('a'), PHP_EOL; / / value a
Echo $yac- > a, PHP_EOL; / / value a
Print_r ($yac- > get ('b'))
/ / Array
/ / (
/ / [0] = > 1
/ / [1] = > 2
/ / [2] = > 3
/ / [3] = > 4
/ /)
Var_dump ($yac- > get ('obj'))
/ / object (stdClass) # 3 (1) {
/ / ["v"] = >
/ / string (5) "obj v"
/ /}
In a very simple operation, we only need to instantiate a Yac class, and we can add and get the cache content through the add () method and the get () method.
The Yac extension also overrides the _ _ set () and _ _ get () magic methods, so we can manipulate the cache directly by manipulating variables.
Next, we can view the status information of the current cache through the info () function.
Print_r ($yac- > info ())
/ / Array
/ / (
/ / [memory_size] = > 71303168
/ / [slots_memory_size] = > 4194304
/ / [values_memory_size] = > 67108864
/ / [segment_size] = > 4194304
/ / [segment_num] = > 16
/ / [miss] = > 0
/ / [hits] = > 4
/ / [fails] = > 0
/ / [kicks] = > 0
/ / [recycles] = > 0
/ / [slots_size] = > 32768
/ / [slots_used] = > 3
/ /)
Set cache $yac- > set ('new value', 'cache')
Echo $yac- > a, PHP_EOL; / / new value a!
$yac- > a = 'best new value aegis'
Echo $yac- > a, PHP_EOL; / / best new value a!
The purpose of the set () function is to modify the contents of the current cache key if it exists, and create a cache if it does not exist.
Delete cache $yac- > delete ('a')
Echo $yac- > a, PHP_EOL; / /
$yac- > flush ()
Print_r ($yac- > info ())
/ / Array
/ / (
/ / [memory_size] = > 71303168
/ / [slots_memory_size] = > 4194304
/ / [values_memory_size] = > 67108864
/ / [segment_size] = > 4194304
/ / [segment_num] = > 16
/ / [miss] = > 1
/ / [hits] = > 6
/ / [fails] = > 0
/ / [kicks] = > 0
/ / [recycles] = > 0
/ / [slots_size] = > 32768
/ / [slots_used] = > 0
/ /)
For the deletion of a single cache, we can directly use the delete () function to delete the contents of the cache. If you want to empty the entire cache space, you can use flush () directly to empty the entire cache space.
Alias space
We mentioned the cache space above. This means that you can pass an alias configuration to the default Yac class constructor when instantiating Yac. In this way, different Yac instances are placed in different namespaces, and the caching of the same Key in different spaces will not affect each other.
$yacFirst = new Yac ()
$yacFirst- > a = 'first aegis'
$yacSecond = new Yac ()
$yacSecond- > a = 'second aegis'
Echo $yacFirst- > a, PHP_EOL; / / second a!
Echo $yacSecond- > a, PHP_EOL; / / second a!
We all use the default instantiation of Yac objects in this code. Although they are instantiated separately, they hold the same space, so the same a variable overrides each other.
$yacFirst = new Yac ('first')
$yacFirst- > a = 'first aegis'
$yacSecond = new Yac ('second')
$yacSecond- > a = 'second aegis'
Echo $yacFirst- > a, PHP_EOL; / / first a!
Echo $yacSecond- > a, PHP_EOL; / / second a!
When we use different instantiation parameters, the same a does not affect each other, and they are stored in different spaces. In other words, Yac automatically adds a prefix to these Key.
Cache aging
Finally, the cache system has a time limit for the cache content, and if you specify an expiration time, the cache content will expire after the specified time.
$yac- > add ('ttl',' 10slots, 10)
$yac- > set ('ttl2',' 20slots, 20)
Echo $yac- > get ('ttl'), PHP_EOL; / / 10s
Echo $yac- > ttl2, PHP_EOL; / / 20s
Sleep (10)
Echo $yac- > get ('ttl'), PHP_EOL; / /
Echo $yac- > ttl2, PHP_EOL; / / 20s
The ttl cache in the above code only sets an expiration time of 10 seconds, so after 10 seconds of sleep (), the output ttl has no content.
It is important to note that the time setting is valid for a long time if it is not set, and the expiration time cannot be set with the _ _ set () method, but only with the set () or add () function.
So, whether the Yac extension is as easy to use as our Apc, of course, the more important thing is its performance and applicable scenarios. For small systems, especially in the operating environment where the machine configuration is not so strong, this extended cache system can make our development faster and more convenient.
So much for the introduction and usage of PHP's efficient cache extension Yac. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.
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.