What is Yac
It can be seen from the name that this is another work of the master Niao Ge. After all, he is the core developer of PHP and his work never disappoints us every time. Brother Niao can be said to be the pride of our Chinese programmers. He plays a decisive role in the PHP world. You can search his blog yourself. Although the update frequency is not high, every article is worth learning.
Yac is a lock-free shared cache system. Because it is lock-free, it is very efficient. Apc is said to be more than twice as efficient as Memcached, while Yac is faster than Apc. This is its biggest feature.
Compared with Memcached or Redis, Yac is more lightweight. We don’t need to install any other software in the server. We only need to install this extension to use it. For small systems, especially systems that simply cache data, we do not need complex data types. Just using this extension of the programming language can make our development more convenient and faster.
The installation method is also very simple. Just download the installation package from PECL and then install the extension.
Basic operations
For cache-related operations, they are nothing more than adding, modifying, and deleting cache. Unlike external caching systems, when saving arrays or objects, the cache of PHP extension classes can directly save these data types without serializing them into strings or converting them into JSON strings. This is one of the advantages of Apc and Yac.
Add and get cache
$yac = new Yac(); $yac->add('a', 'value a'); $yac->add('b', [1,2,3,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" // }
Very simple operation, we only need to instantiate a Yac class, and then we can add and get cache content through the add() method and get() method.
Yac extension also overrides the __set() and __get() magic methods, so we can directly operate the cache by operating variables.
Next, we can view the current cached status information 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('a', 'new value a!'); echo $yac->a, PHP_EOL; // new value a! $yac->a = 'best new value a!'; echo $yac->a, PHP_EOL; // best new value a!
The function of the set() function is to modify the content of the cache if the current cache key exists. If it does not exist, create a cache.
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 deletion of a single cache, we can directly use the delete() function to delete the contents of this cache. If you want to clear the entire cache space, you can directly use flush() to clear the entire cache space.
Alias space
We mentioned the cache space above. In fact, when instantiating Yac, you can pass an alias configuration to the default Yac class constructor. In this way, different Yac instances are equivalent to being placed in different namespaces, and caches of the same Key in different spaces will not affect each other.
$yacFirst = new Yac(); $yacFirst->a = 'first a!';; $yacSecond = new Yac(); $yacSecond->a = 'second a!'; echo $yacFirst->a, PHP_EOL; // second a! echo $yacSecond->a, PHP_EOL; // second a!
We all use the default instantiated Yac object in this code. Although they are instantiated separately, the spaces they save are the same, so the same a variables will overwrite each other.
$yacFirst = new Yac('first'); $yacFirst->a = 'first a!';; $yacSecond = new Yac('second'); $yacSecond->a = 'second a!'; echo $yacFirst->a, PHP_EOL; // first a! echo $yacSecond->a, PHP_EOL; // second a!
When we use different instantiation parameters, the same a will not affect each other, they are stored in different spaces. In other words, Yac will automatically add a prefix to these Keys.
Cache aging
Finally, the caching system will have aging restrictions on cached content. If an expiration time is specified, the cached content will expire after the specified time.
$yac->add('ttl', '10s', 10); $yac->set('ttl2', '20s', 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 will have no content.
It should be noted that if the time setting is not set, it will be effective for a long time, and the expiration time cannot be set using the __set() method. You can only use the set() or add() function to set the expiration time. time.
Summary
How about the Yac extension? Is it as convenient and easy to use as our Apc? Of course, the more important thing is its performance and applicable scenarios. For small systems, especially in operating environments where the machine configuration is not so strong, this extended cache system can make our development faster and more convenient. Regarding the concept of lock-free sharing, we can refer to the second link in the reference document below, which is detailed in Brother Niao's article.
Test code:
https://github.com/zhangyue0503/dev-blog/blob/master/php/202006/source/PHP%E7%9A%84%E5%8F%A6%E4%B8%80%E4%B8%AA%E9%AB%98%E6%95%88%E7%BC%93%E5%AD%98%E6%89%A9%E5%B1%95%EF%BC%9AYac.php
Recommended learning: php video tutorial