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)
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-enableset([1,2,3,5,6]); //获取缓存 $a = $cache->get(); //备注 1.由于yac的缓存是共享的,所以在多个项目中使用时,需要注意key的唯一性,否则会出现缓存覆盖的情况 //备注 2.由于cli在执行后会自动退出,所以在cli中使用时,需要注意缓存的有效期,当再次执行时候换存是拿不到的 //例如 //例 //step 1 set([1,2,3,5,6]); //php step1.php //执行后会自动退出,缓存失效 get(); var_dump($arr);// 空 //php step2.php //执行事后上一个进程已经退出,所以缓存失效
Method
Yac::__construct
Yac::__construct([string $prefix = ""])
Yac::set
Yac::set($key, $value[, $ttl = 0]) Yac::set(array $kvs[, $ttl = 0])
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(array|string $key[, &$cas = NULL])
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])
Yac::flush
Yac::flush()
Yac::info
Yac::info(void)
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) } */
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'); } }
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!