We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22303 MODX Staff
    • 10,725 Posts
    Do you have cache_db set on by chance BobRay?
      • 3749
      • 24,544 Posts
      No, it's off. I just double checked. cache_db_session is also off. [ed. note: BobRay last edited this post 14 years, 7 months ago.]
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 22303 MODX Staff
        • 10,725 Posts
        Then I'm not sure why it would fail to work. Perhaps we need an isolated test case as a separate package so I can understand what problem you are having.
          • 3749
          • 24,544 Posts
          I've created a minimal transport package to demonstrate.

          http://tracker.modx.com/issues/6685


          BTW, this issue has been there for at least a year. Most packages don't create resources and I think I'm probably the only who has tried to get resource and system setting objects in a script resolver.

          With a couple of extras, I resorted to doing the work the first time the snippet or plugin runs in MODX. I've also had to save resource IDs to a file so I can remove the resources on uninstall. wink





          ---------------------------------------------------------------------------------------------------------------
          PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
          MODx info for everyone: http://bobsguides.com/modx.html [ed. note: BobRay last edited this post 14 years, 7 months ago.]
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 40045
            • 534 Posts
            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.]