簡體   English   中英

如何從緩存中獲取以某些鍵開頭的所有值

[英]How to get all values from cache which start with certain key

作為標題,我想從緩存中獲取所有從某些值開始的值。

例如,我要在項目中計算帖子視圖,然后將它們存儲在緩存中。 然后,我想訪問緩存上所有已存儲的查看值。

使用會話幫助程序,它可以將點之間的鍵分開並創建多維數組。

這樣我們就可以獲取存儲的項目,例如Session::get("views")它為所有存儲的項目提供“視圖”。 鍵。

我們如何使用Cache做同樣的事情?

function get_all_cache(){
    $filesystem = \Cache::getStore()->getFilesystem();
    $cache_dir = (\Cache::getDirectory());
    $keys = [];
    foreach ($filesystem->allFiles($cache_dir) as $file1) {
        if (is_dir($file1->getPath())) {
            foreach ($filesystem->allFiles($file1->getPath()) as $file2) {
                $keys = array_merge($keys, [$file2->getRealpath() => unserialize(substr(\File::get($file2->getRealpath()), 10))]);
            }
        }
    }
    return $keys;
}

不正確的答案,但我給你我最好的答案

try like this 


function get_all_cache(){
    if(\Cache::has('viewsValue')) {
          $keys =  \Cache::get('viewsValue');
    }else {
    $filesystem = \Cache::getStore()->getFilesystem();
    $cache_dir = (\Cache::getDirectory());
    $keys = [];
    foreach ($filesystem->allFiles($cache_dir) as $file1) {
        if (is_dir($file1->getPath())) {
            foreach ($filesystem->allFiles($file1->getPath()) as $file2) {
                $keys = array_merge($keys, [$file2->getRealpath() => unserialize(substr(\File::get($file2->getRealpath()), 10))]);
            }
        }
    }
    \Cache::put('viewsValue',$keys,60);   ///add key array into cache 
    }

  return $keys;
 }

注意第一次,如果鍵不存在,則下次將所有鍵上的緩存值存儲在鍵上時,始終保持高速緩存,因此性能更好

only work if not updated your keys array every time

有關更多緩存的了解, 請參見

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM