我将其作为临时名称添加到变量中,以便于处理。
接下来,我检查get_transient
为false,并更新变量名和瞬态。你可以试试这个:
$transient_name = \'cached_json_data\';
if ( false === ( $json_data = get_transient( $transient_name ) ) ) {
$json_data = json_decode(file_get_contents(\'json_file\'));
set_transient( $transient_name, $json_data, WEEK_IN_SECONDS );
}
//normal code
set_transient
您不需要序列化值。如果需要序列化该值,则将在设置该值之前对其进行序列化。
常数WP 3.5+
MINUTE_IN_SECONDS = 60 (seconds)
HOUR_IN_SECONDS = 60 * MINUTE_IN_SECONDS
DAY_IN_SECONDS = 24 * HOUR_IN_SECONDS
WEEK_IN_SECONDS = 7 * DAY_IN_SECONDS
MONTH_IN_SECONDS = 30 * DAY_IN_SECONDS
YEAR_IN_SECONDS = 365 * DAY_IN_SECONDS