FullCalendar 的 JSON feed 加载缓慢

发布于 2024-11-11 13:40:59 字数 2653 浏览 0 评论 0原文

我正在使用 FullCalendar 通过 JSON feed 从 WP 上的自定义帖子类型加载事件。它正在工作,但加载需要一些时间。请查看此处:http://cea3.iscte.pt/en/agenda-3/(六月或八月)。你们中有人知道是什么原因造成的吗?

这是 JSON feed 的完整代码:

<?php

// - standalone json feed -

header('Content-Type:application/json');

// - grab wp load, wherever it's hiding -
if(file_exists('../../../../wp-load.php')) :
    include '../../../../wp-load.php';
else:
    include '../../../../../wp-load.php';
endif;

global $wpdb;

// - grab date barrier -
$oneyear = strtotime('-1 year') + ( get_option( 'gmt_offset' ) * 3600 );

// - query -
global $wpdb;
$querystr = "
    SELECT *
    FROM $wpdb->posts wposts, $wpdb->postmeta metastart, $wpdb->postmeta metaend
    WHERE (wposts.ID = metastart.post_id AND wposts.ID = metaend.post_id)
    AND (metaend.meta_key = 'tf_events_enddate' AND metaend.meta_value > $oneyear )
    AND metastart.meta_key = 'tf_events_enddate'
    AND wposts.post_type = 'tf_events'
    AND wposts.post_status = 'publish'
    ORDER BY metastart.meta_value ASC LIMIT 500
 ";

$events = $wpdb->get_results($querystr, OBJECT);
$jsonevents = array();

// - loop -
if ($events):
global $post;
foreach ($events as $post):
setup_postdata($post);

// - custom variables -
$custom = get_post_custom(get_the_ID());
$sd = $custom["tf_events_startdate"][0];
$ed = $custom["tf_events_enddate"][0];

// - grab gmt for start -
$gmts = date('Y-m-d H:i:s', $sd);
$gmts = get_gmt_from_date($gmts); // this function requires Y-m-d H:i:s
$gmts = strtotime($gmts);

// - grab gmt for end -
$gmte = date('Y-m-d H:i:s', $ed);
$gmte = get_gmt_from_date($gmte); // this function requires Y-m-d H:i:s
$gmte = strtotime($gmte);

// - set to ISO 8601 date format -
$stime = date('c', $gmts);
$etime = date('c', $gmte);

$thetitle = $post->post_title;
$short_title = substr($thetitle,0,50);

$eventpostid = $post->ID;
$eventslug = wp_get_post_terms( $eventpostid, 'tf_eventcategory' );
$eventvenueslug = $eventslug[0]->slug;

$tf_events_link = get_post_meta($post->ID, 'tf_events_link', true);
$tf_events_permalink = get_permalink($post->ID);
if ($tf_events_link) { $url_event = $tf_events_link ; }
else { $url_event = $tf_events_permalink; };

// - json items -
$jsonevents[]= array(
    'title' => $short_title . '...',
    'allDay' => false, // <- true by default with FullCalendar
    'start' => $stime,
    'end' => $etime,
    'url' => $url_event,
    'className' => $eventvenueslug
    );

endforeach;
else :
endif;

// - fire away -
echo json_encode($jsonevents);

?> 

谢谢。

I'm using FullCalendar to load events from custom post types on WP, through a JSON feed. It's working, but it's taking some time to load. Please check here: http://cea3.iscte.pt/en/agenda-3/ (june or august). Do any of you have a clue what can be causing it?

This is the full code for the JSON feed:

<?php

// - standalone json feed -

header('Content-Type:application/json');

// - grab wp load, wherever it's hiding -
if(file_exists('../../../../wp-load.php')) :
    include '../../../../wp-load.php';
else:
    include '../../../../../wp-load.php';
endif;

global $wpdb;

// - grab date barrier -
$oneyear = strtotime('-1 year') + ( get_option( 'gmt_offset' ) * 3600 );

// - query -
global $wpdb;
$querystr = "
    SELECT *
    FROM $wpdb->posts wposts, $wpdb->postmeta metastart, $wpdb->postmeta metaend
    WHERE (wposts.ID = metastart.post_id AND wposts.ID = metaend.post_id)
    AND (metaend.meta_key = 'tf_events_enddate' AND metaend.meta_value > $oneyear )
    AND metastart.meta_key = 'tf_events_enddate'
    AND wposts.post_type = 'tf_events'
    AND wposts.post_status = 'publish'
    ORDER BY metastart.meta_value ASC LIMIT 500
 ";

$events = $wpdb->get_results($querystr, OBJECT);
$jsonevents = array();

// - loop -
if ($events):
global $post;
foreach ($events as $post):
setup_postdata($post);

// - custom variables -
$custom = get_post_custom(get_the_ID());
$sd = $custom["tf_events_startdate"][0];
$ed = $custom["tf_events_enddate"][0];

// - grab gmt for start -
$gmts = date('Y-m-d H:i:s', $sd);
$gmts = get_gmt_from_date($gmts); // this function requires Y-m-d H:i:s
$gmts = strtotime($gmts);

// - grab gmt for end -
$gmte = date('Y-m-d H:i:s', $ed);
$gmte = get_gmt_from_date($gmte); // this function requires Y-m-d H:i:s
$gmte = strtotime($gmte);

// - set to ISO 8601 date format -
$stime = date('c', $gmts);
$etime = date('c', $gmte);

$thetitle = $post->post_title;
$short_title = substr($thetitle,0,50);

$eventpostid = $post->ID;
$eventslug = wp_get_post_terms( $eventpostid, 'tf_eventcategory' );
$eventvenueslug = $eventslug[0]->slug;

$tf_events_link = get_post_meta($post->ID, 'tf_events_link', true);
$tf_events_permalink = get_permalink($post->ID);
if ($tf_events_link) { $url_event = $tf_events_link ; }
else { $url_event = $tf_events_permalink; };

// - json items -
$jsonevents[]= array(
    'title' => $short_title . '...',
    'allDay' => false, // <- true by default with FullCalendar
    'start' => $stime,
    'end' => $etime,
    'url' => $url_event,
    'className' => $eventvenueslug
    );

endforeach;
else :
endif;

// - fire away -
echo json_encode($jsonevents);

?> 

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

念三年u 2024-11-18 13:40:59

加载似乎需要几秒钟。

我使用这个日历,加载大约需要 2-6 秒 - 最长的是 6 秒,但我有大约 3 个源和大约 40 个事件。

现在我不确定这对你来说是否很长一段时间,因为你没有具体说明它实际上需要多长时间。
看起来调用是在同一台服务器上,因此唯一的问题可能是 SQL 需要很长时间才能回复。是专用服务器还是共享服务器?

您的 PHP 看起来不错并且应该可以快速执行。我使用 .NET 和 SQL Server 有类似的逻辑。

这是从我的世界加载您的日历所需的时间。

在此处输入图像描述

加快速度的唯一其他方法是使用缓存

http://arshaw.com/fullcalendar/docs/event_data/events_json_feed/#options

并通过巧妙检查日期来管理它以某种方式重新加载源?或其他什么。
我确信一旦之前加载了 feed,时间就会减少到毫秒。

您必须做一些聪明的事情,例如快速加载当前月份 - 并在后台加载 1 年或 2 年并缓存它。然后当你改变月份时,它就会立即发生,因为它在记忆中。

It does seem like it takes a few seconds to load.

I use this calendar and it takes about 2-6 seconds to load- The longest was 6 seconds but i had about 3 sources and ~40 events.

Now I am not sure if that for you is a long time because you did not specify how long it actaully takes.
It looks like the call is on the same server so the only problem can be the SQL that takes long to reply. Is it dedicated or shared server?

Your PHP looks fine and should execute quickly. I have similar logic using .NET and SQL Server.

This is the time it takes to load your calendar from my side of the world.

enter image description here

The only other way to speed it up is to use Caching

http://arshaw.com/fullcalendar/docs/event_data/events_json_feed/#options

and manage it to reload the source somehow with a clever check of the date? or something.
I am sure the times would be reduced to ms once the feed has been loaded before.

You would have to do something clever like load the current month quickly - and in the background load 1 year or 2 years and cache it. Then when you change months it will be instant because its in memory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文