Read from JSON/Cached JSON

majafy

Member
Hi,

I want use some data from RESTFul API (JSON) and show in sidebar, so I created a template and include in PAGE_CONTAINER,
and in new template I used <xen:callback> to include and run my php code, in php code I use file_get_contents to read from json.
everything seems to be ok, but I have 2 questions:

1- I want to know my way is true or xenForo has some methods for processing JSON APIs?
2- how can I cache JSON data? because every time the page load, server should connect to JSON url !


Regards
 
XenForo has a HTTP client which you could use. It doesn't really specifically add anything to handle JSON data, but it's obviously a more robust way of downloading data from a HTTP source.

You could use the XenForo simple cache:
PHP:
// Set simple cache data:
XenForo_Application::setSimpleCacheData('yourUniqueCacheKey', $yourDataToCache);

// Get simple cache data:
$dataFromCache = XenForo_Application::getSimpleCacheData('yourUniqueCacheKey');

// Permanently delete simple cache data:
XenForo_Application::setSimpleCacheData('yourUniqueCacheKey', false);
 
XenForo has a HTTP client which you could use. It doesn't really specifically add anything to handle JSON data, but it's obviously a more robust way of downloading data from a HTTP source.

You could use the XenForo simple cache:
PHP:
// Set simple cache data:
XenForo_Application::setSimpleCacheData('yourUniqueCacheKey', $yourDataToCache);

// Get simple cache data:
$dataFromCache = XenForo_Application::getSimpleCacheData('yourUniqueCacheKey');

// Permanently delete simple cache data:
XenForo_Application::setSimpleCacheData('yourUniqueCacheKey', false);

Thanks Chris,

Would you please guide me about XenForo HTTP methods too?


Regards
 
Sorry, that would help ;)

Look at the XenForo_Helper_Http class and how it is used in various places throughout the XF code.
 
XenForo has a HTTP client which you could use. It doesn't really specifically add anything to handle JSON data, but it's obviously a more robust way of downloading data from a HTTP source.

You could use the XenForo simple cache:
PHP:
// Set simple cache data:
XenForo_Application::setSimpleCacheData('yourUniqueCacheKey', $yourDataToCache);

// Get simple cache data:
$dataFromCache = XenForo_Application::getSimpleCacheData('yourUniqueCacheKey');

// Permanently delete simple cache data:
XenForo_Application::setSimpleCacheData('yourUniqueCacheKey', false);

Is there any way to set expire time ?


Regards
 
Oh, but I need refresh the cache each every 2~3 hours, because my json will update.
what should I do now ?
 
You just need to handle refreshing that cache however you want. So maybe storing the last update time, and doing a refresh once that time plus 2-3 hours has past.
 
Keep in mind that data in Simple Cache is loaded on every request. This includes ajax calls, css, deferred task etc.

You want to keep the contents small, or store it in a custom Data Registry key which you explicitly load when you need it.
 
Back
Top Bottom