本篇文章给大家带来了关于php+yac的相关知识,其中主要跟大家聊一聊怎么用yac替换apcu memcache,感兴趣的朋友下面一起来看一下吧,希望对大家有帮助。
yac 缓存
yac 是用于 php 的共享和无锁内存用户数据缓存。它可以用来替换 apc 或本地 memcached。鸟哥出品必属精品
要求
php 7 +
install
$/path/to/phpize$./configure --with-php-config=/path/to/php-config$make && make install
note
yac 是无锁缓存,您应该尽量避免或减少多个进程设置一个相同键的概率
yac 使用部分 crc,您最好重新排列缓存内容,将最重要 (可变) 的字节放在头部或尾部
restrictions
缓存 key 不能大于 48 (yac_max_key_len) bytes
缓存内容不能大于 64m (yac_max_value_raw_len) bytes
压缩后的缓存值不能大于 1m 1m (yac_max_value_compressed_len) bytes
ini 配置
yac.enable = 1yac.keys_memory_size = 4m ; 4m can get 30k key slots, 32m can get 100k key slotsyac.values_memory_size = 64myac.compress_threshold = -1yac.enable_cli = 0 ; 是否使用cli启用yac,默认为0yac.serializer = php ; yac2.2.0以来,yac使用的特定seralizer json(-- enable-json) 、msgpack(-- enable-msgpack) 或igbinary(-- enable-igbinary)
常量
yac_versionyac_max_key_len = 48 ; if your key is longer than this, maybe you can use md5 result as the keyyac_max_value_raw_len = 64myac_max_value_compressed_len = 1myac_serializer_php = 0 ; since yac-2.2.0yac_serializer_json = 1 ; since yac-2.2.0yac_serializer_msgpack = 2 ; since yac-2.2.0yac_serializer_igbinary = 3 ; since yac-2.2.0yac_serializer ; serializer according to yac.serializer, default is yac_serializer_php
注意 cli 下会出现的问题
如果 cli情况下 一定ini配置开启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<?phpuse doraemon\pockets\datebase\sharecache;$cache = new sharecache('test');//设置缓存$cache->set([1,2,3,5,6]);//php step1.php //执行后会自动退出,缓存失效<?phpuse doraemon\pockets\datebase\sharecache; //step 2$cache = new sharecache('test');//设置缓存$arr = $cache->get();var_dump($arr);// 空//php step2.php //执行事后上一个进程已经退出,所以缓存失效
方法
yac::__construct
yac::__construct([string $prefix = ""])
yac 的构造函数,您可以指定一个前缀,该前缀将用于在执行设置 / 获取 / 删除时预先添加到任何键
<?php $yac = new yac("myproduct_");?>
yac::set
yac::set($key, $value[, $ttl = 0]) yac::set(array $kvs[, $ttl = 0])
将一个值存储到 yac 缓存中,键是缓存唯一的,因此使用相同的键存储第二个值将覆盖原始值。
成功时返回 true,错误时返回 false (如无内存,无法获得 cas write right)
<?php$yac = new yac();$yac->set("foo", "bar");$yac->set( array( "dummy" => "foo", "dummy2" => "foo", ) );?>
note:
如 yac 2.1,如果 cas 竞争失败,可能会失败,您可能需要执行以下操作:
while (!($yac->set("important", "value")));
yac::get
yac::get(array|string $key[, &$cas = null])
从缓存中获取存储变量。如果一个数组被传递,那么每个元素都被获取并返回。成功时返回值,错误时返回 false
<?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])
从缓存中删除存储的变量。如果指定了延迟,则该值将在 $delay 秒后删除。
yac::flush
yac::flush()
立即使所有现有项目无效。它实际上并没有释放任何资源,它只将所有项目标记为无效。
yac::info
yac::info(void)
获取缓存信息
<?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) } */
<?phpnamespace test\cacheuse 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'); }}
推荐学习:《php视频教程》
以上就是php共享缓存之yac替换apcu memcache!的详细内容。
