Home > Article > Backend Development > PHP shared cache Yac replaces APCU memcache!
This article brings you relevant knowledge about php yac. It mainly talks about how to replace APCU memcache with yac. Friends who are interested can take a look below. I hope it will be helpful to everyone.
yac cache
Yac is a shared and lock-free memory user data cache for PHP. It can be used to replace APC or local memcached. The products produced by Bird Brother must be high-quality products
Requirements
PHP 7
Install
$/path/to/phpize $./configure --with-php-config=/path/to/php-config $make && make install
Note
Yac is a lock-free cache, you should try to avoid or reduce the probability of multiple processes setting the same key
Yac With partial crc, you'd better rearrange the cache contents, putting the most significant (variable) bytes at the head or tail
Restrictions
The cache key cannot be larger than 48 (YAC_MAX_KEY_LEN) bytes
The cache content cannot be larger than 64M (YAC_MAX_VALUE_RAW_LEN) bytes
The compressed cache value cannot be larger than 1M 1M (YAC_MAX_VALUE_COMPRESSED_LEN) bytes
##ini configuration
yac.enable = 1 yac.keys_memory_size = 4M ; 4M can get 30K key slots, 32M can get 100K key slots yac.values_memory_size = 64M yac.compress_threshold = -1 yac.enable_cli = 0 ; 是否使用cli启用yac,默认为0 yac.serializer = php ; yac2.2.0以来,yac使用的特定seralizer json(-- enable-json) 、msgpack(-- enable-msgpack) 或igbinary(-- enable-igbinary)Constant
YAC_VERSION YAC_MAX_KEY_LEN = 48 ; if your key is longer than this, maybe you can use md5 result as the key YAC_MAX_VALUE_RAW_LEN = 64M YAC_MAX_VALUE_COMPRESSED_LEN = 1M YAC_SERIALIZER_PHP = 0 ; since yac-2.2.0 YAC_SERIALIZER_JSON = 1 ; since yac-2.2.0 YAC_SERIALIZER_MSGPACK = 2 ; since yac-2.2.0 YAC_SERIALIZER_IGBINARY = 3 ; since yac-2.2.0 YAC_SERIALIZER ; serializer according to yac.serializer, default is YAC_SERIALIZER_PHP
Pay attention to the problems that will occur under cli
If cli is used, the ini configuration must be enabled to enable cli-enable<?php use Doraemon\pockets\datebase\ShareCache; //实例化缓存封装类 $cache = new ShareCache('test'); //设置缓存 $cache->set([1,2,3,5,6]); //获取缓存 $a = $cache->get(); //备注 1.由于yac的缓存是共享的,所以在多个项目中使用时,需要注意key的唯一性,否则会出现缓存覆盖的情况 //备注 2.由于cli在执行后会自动退出,所以在cli中使用时,需要注意缓存的有效期,当再次执行时候换存是拿不到的 //例如 //例 //step 1 <?php use Doraemon\pockets\datebase\ShareCache; $cache = new ShareCache('test'); //设置缓存 $cache->set([1,2,3,5,6]); //php step1.php //执行后会自动退出,缓存失效 <?php use Doraemon\pockets\datebase\ShareCache; //step 2 $cache = new ShareCache('test'); //设置缓存 $arr = $cache->get(); var_dump($arr);// 空 //php step2.php //执行事后上一个进程已经退出,所以缓存失效
Method
Yac::__construct
Yac::__construct([string $prefix = ""])Constructor of Yac, you can specify a prefix that will be used to prepend to any keys when performing set/get/delete
<?php $yac = new Yac("myproduct_"); ?>
Yac::set
Yac::set($key, $value[, $ttl = 0]) Yac::set(array $kvs[, $ttl = 0])Stores a value into the Yac cache, the key is unique to the cache, so storing a second value with the same key will overwrite the original value. Return true on success, return false on error (if there is no memory, cas write right cannot be obtained)
<?php $yac = new Yac(); $yac->set("foo", "bar"); $yac->set( array( "dummy" => "foo", "dummy2" => "foo", ) ); ?>
Note:
Such as Yac 2.1 , which may fail if cas competition fails, you may need to do the following:while (!($yac->set("important", "value")));Yac::get
Yac::get(array|string $key[, &$cas = NULL])Get the storage variable from the cache. If an array is passed, each element is obtained and returned. Returns a value on success, false on error
<?php $yac = new Yac(); $yac->set("foo", "bar"); $yac->set( array( "dummy" => "foo", "dummy2" => "foo", ) ); $yac->get("dummy"); $yac->get(array("dummy", "dummy2")); ?>
Yac::delete
Yac::delete(array|string $keys[, $delay=0])Deletes the stored variable from the cache. If a delay is specified, the value will be deleted after $delay seconds.
Yac::flush
Yac::flush()Immediately invalidate all existing items. It doesn't actually release any resources, it just marks all items as invalid.
Yac::info
Yac::info(void)Get cache information
<?php .... var_dump($yac->info()); /* will return an array like: array(11) { ["memory_size"]=> int(541065216) ["slots_memory_size"]=> int(4194304) ["values_memory_size"]=> int(536870912) ["segment_size"]=> int(4194304) ["segment_num"]=> int(128) ["miss"]=> int(0) ["hits"]=> int(955) ["fails"]=> int(0) ["kicks"]=> int(0) ["slots_size"]=> int(32768) ["slots_used"]=> int(955) } */
<?php namespace Test\Cache use Yac; use RuntimeException; /** * 共享缓存类 * Date: 2023/2/22 * Time: 16:13 * docs: */ class ShareCache { public bool $isEnable = true; public string $key = ''; /** * * 共享内存块实例化。 */ public function __construct($key) { if (!extension_loaded("yac")) { $this->isEnable = false; throw new RuntimeException('yac 扩展不存在!'); } if (!$key) { throw new RuntimeException('key 不能为空!'); } $this->key = md5($key); } /** * * 获取共享内存块的值。 */ public function get() { if ($this->isEnable) { return (new Yac('db_'))->get($this->key); } throw new RuntimeException('yac is not enable ,skip getCache'); } /** * * 设置共享内存块的值。 */ public function set($var): bool { if ($this->isEnable) { return (new Yac('db_'))->set($this->key, $var, 3600); } throw new RuntimeException('yac is not enable ,skip setCache'); } /** * * 删除共享内存块的值。 */ public function del(): bool { if ($this->isEnable) { return (new Yac('db_'))->delete($this->key); } throw new RuntimeException('yac is not enable ,skip delCache'); } /** * * 获取共享内存块的信息。 */ public function info(): array { if ($this->isEnable) { return (new Yac('db_'))->info(); } throw new RuntimeException('yac is not enable ,skip info'); } /** * * 清空共享内存块的值。 */ public function flush(): bool { if ($this->isEnable) { return (new Yac)->flush(); } throw new RuntimeException('yac is not enable ,skip flush'); } }Recommended learning: "
PHP Video Tutorial"
The above is the detailed content of PHP shared cache Yac replaces APCU memcache!. For more information, please follow other related articles on the PHP Chinese website!