我有一个使用 wp_cache_set/wp_cache_get 来存储结果的 JSON API 端点。这个端点一天被命中数十万次。
然而,这经常会导致我的服务器宕机,因为缓存似乎仍在访问 MySQL 和/或加载 Wordpress。
这是真的吗?如果是的话,什么是更好的缓存解决方案来使它尽可能轻? (例如内存缓存)
--
以下是有用的代码:
define('WP_USE_THEMES', false);
require_once('../../../wp-blog-header.php');
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
if(!$image_url) $image_url = $_GET['url'];
if(!$image_url) return false;
$cacheTitle = md5($image_url) . '1';
$result = wp_cache_get( $cacheTitle );
$notCached = $result ? false : true;
if ($notCached){
/** Insert code here to get the data I need and store it in $result **/
wp_cache_set( $cacheTitle, $result );
}
return json_encode($result);
最佳答案
我最终使用 Cloudflare Page Rules 来缓存该特定 URL。干净简单 :)
关于mysql - wp_cache 是否仍然加载 Wordpress 并使用 MySQL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37331715/