First, thanks for the information in this thread, helped me to get started with the modx cache.
But I have a problem I don't understand...
What I'm trying is to cache the Twitter API result because there is a rate limit of 150/per hour to the twitter servers. I'm doing it like this (only part of the full code, but there's no class involved, just some more or less simple lines of php):
$url = 'h t t p ://api.twitter.com/' . $api . '/statuses/user_timeline.json?' . $retweets . $id . $name . $count; // generate request URL
$twitterresponse = curl($url); // send the request to the url specified above
$twitterdata = json_decode($twitterresponse); // make the response usable > this shoudl be cached
// set up modx caching to omit problems with request limit
$cachetime = isset($settings['cachetime']) ? $settings['cachetime'] : 0;
$cacheoptions = array(
xPDO::OPT_CACHE_KEY => 'twitter'
);
$modx->cacheManager->set('tweets', $twitterdata, $cachetime, $cacheoptions);
$twitterdata_cached = $modx->cacheManager->get('tweets');
var_dump($twitterdata_cached);
I get the response from the twitter api, everything fine. I cache this response with $modx->cacheManager->set() and specify the folder to save the cache file in $cacheoptions, here /twitter/ which results (with the cache-key of 'twitter') in a file saved at core/cache/twitter/twitter.cache.php which contains the twitter response and is saved successfully where it should, so everything ok til here!
Then I need to get the cached data back for further use in the script, but that's where I'm stuck. I thought I could just get it back by calling $modx->cacheManager->get('twitter'), but this returns just NULL, I tried several things I found in forums/docs and just by myself, but nothing helped. First I had a look at the cache-file which says (if an expiration time is set, like 600 seconds) return NULL if the current time is smaller then the calculated time, which is when the cachefile was saved plus the expiration time, I guess =). So i reduced expiration time to 0 which results in having no "return NULL" condition, so there is just a return 'cached Object or whatever', but there is nothing / NULL returned anyways...
also tried stuff like
$cacheManager = $modx->getCacheManager();
$twitterdata_cached = $cacheManager->get('twitter');
// instead of jsut
$modx->cacheManager->get('twitter');
did also not work (return NULL)
or
$twitterdata_cached = $modx->cacheManager->get('tweets', $cacheoptions);
// instead of just
$twitterdata_cached = $modx->cacheManager->get('tweets');
but this just causes a totally blank white page...so doesn't work too...
I'm really stuck. Possibly I'm mixing up some stuff or I didn't get the concept of it =)...would really appreciate help, I have the feeling I'm sooo close, but there's the last "click" missing...^^
thanks in advance!
EDIT: OK, i got it working now! The problem seems to be how i saved the data btw. what kind of data into the cache...
the key was to set the true flag on json_decode() like
$twitterdata_uncached = json_decode($twitterresponse, true);
so it makes an associative array out of it, which can be saved in cache and also successfully queried by $modx->cacheManager->get(), I had to rewrite the whole parsing thing bc before it was like a foreach($twitterdata as $tweet) and now it's a for ($i;$count<$i;$i++) loop... I still think the foreach one would be more elegant but as I got it working that seems only to be cosmetics...
it would be interesting anyway, what went wrong when getting back a saved object from the cacheManager...or trying to =)
[ed. note: exside last edited this post 13 years, 11 months ago.]