php 共享缓存之yac 快来替换掉APCU memcache
生活随笔
收集整理的這篇文章主要介紹了
php 共享缓存之yac 快来替换掉APCU memcache
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
yac緩存
Yac是用于PHP的共享和無鎖內存用戶數據緩存。它可以用來替換APC或本地memcached。
要求
PHP 7 +
Install
$/path/to/phpize $./configure --with-php-config=/path/to/php-config $make && make installNote
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 <?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 //執行事后上一個進程已經退出,所以緩存失效方法
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 共享缓存之yac 快来替换掉APCU memcache的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第 11 章 基于小波技术进行图像融合-
- 下一篇: 20200903-03-Hadoop运行