惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Privacy & Cybersecurity Law Blog
V
V2EX
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Register - Security
The Register - Security
MongoDB | Blog
MongoDB | Blog
P
Privacy International News Feed
The Last Watchdog
The Last Watchdog
Security Archives - TechRepublic
Security Archives - TechRepublic
美团技术团队
Stack Overflow Blog
Stack Overflow Blog
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
V
Visual Studio Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
K
Kaspersky official blog
S
Secure Thoughts
T
Tenable Blog
Security Latest
Security Latest
The Cloudflare Blog
S
Security @ Cisco Blogs
H
Heimdal Security Blog
aimingoo的专栏
aimingoo的专栏
TaoSecurity Blog
TaoSecurity Blog
Blog — PlanetScale
Blog — PlanetScale
Microsoft Security Blog
Microsoft Security Blog
Schneier on Security
Schneier on Security
Webroot Blog
Webroot Blog
G
Google Developers Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Scott Helme
Scott Helme
IT之家
IT之家
Latest news
Latest news
The Hacker News
The Hacker News
C
Check Point Blog
T
The Exploit Database - CXSecurity.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
腾讯CDC
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
N
News | PayPal Newsroom
Forbes - Security
Forbes - Security
P
Palo Alto Networks Blog
S
Security Affairs
S
Securelist
Google Online Security Blog
Google Online Security Blog
WordPress大学
WordPress大学
Last Week in AI
Last Week in AI
C
Cybersecurity and Infrastructure Security Agency CISA
A
About on SuperTechFans

N25H

Maye Nano:拒绝臃肿,回归纯粹的快速启动工具 – N25H Maye Nano Script Help – N25H 国行三星 Galaxy Watch7 其他品牌手机解锁心电图、血压等功能 – N25H 推翻重做,彻底重写WordPress主题 – N25H WordPress 通过Hook修改评论Flood判定时间 – N25H 相册测试1 – N25H 相册测试2 – N25H 从 Typecho 迁移到 WordPress 的后续 – N25H 在Linux Ubuntu上挂载新硬盘 (临时、永久挂载) – N25H
WordPress 模板目前采用的缓存获取方案 – N25H
作者: N25H · 2023-12-13 · via N25H

MZ_CACHE_TYPE切换缓存类型,$valCallback$expire设置默认的值与有效期。

刚刚写好的,由于对php不太熟悉也没有严格测试这段代码的性能,其中mz_get_cachemz_set_cache函数的代码与之类似。


define('MZ_CACHE_TYPE', 1); // 缓存类型,0=transient; 1=object-cache; ,相关函数mz_set_cache(key,val,expiration) 与 mz_get_cache(key)


/**
 * 获取缓存数据Ex
 *
 * 与 mz_get_cache 函数类似,在未获取到数据的情况下,可以自动根据回调获取并设置缓存
 *
 * @param string $key 键
 * @param mixed $valCallback 如果没获取到数据,则根据此回调获取数据并设置缓存,注 回调必须返回一个有效的值
 * @param int $expire 过期时间,单位/秒,0为无过期时间
 * @return mixed|false 成功返回值数据,失败返回false
 */
function mz_cache_getEx($key, $valCallback = false, $expire = 0) {

    $result = false;

    switch (MZ_CACHE_TYPE) {
        case 0:
            $result = get_transient($key);
            break;
        case 1:
            $result = wp_cache_get($key, 'mzc');
            break;
    }

    if (!$result && is_callable($valCallback)) {
        $result = $valCallback();
        mz_set_cache($key, $result, $expire);
    }

    return $result;
}
// 调用方式
echo mz_cache_getEx('kkeeyy', fn()=>'666', 3 * MINUTE_IN_SECONDS);

写这个东西注意是为了在某些地方可以偷个懒😂

赞赏作者

如果觉得我的文章对你有用,请随意赞赏