We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8784
    • 32 Posts
    OpenGeek's solution works perfectly. Thanks all for the help!

    //Delete cache file for Resource 5:
    $resourceCache = $modx->cacheManager->getCacheProvider($modx->getOption('cache_resource_key', null, 'resource'));
    $resFive = $modx->getObject('modResource', 5);
    $resourceCache->delete($resFive->getCacheKey());
      • 37480
      • 9 Posts
      Hi all,

      I think I tried to do the same thing. But it's not working for me...

      First of all, if I save a resource in the manager I only want this resource to be deleted (or better refreshed) from the cache. So if I save one resource I don't want the whole cache to be cleared.
      Is there a simple way to do that?

      I tried to create a plugin for the OnDocFormSave event:

      $resourceCache = $modx->cacheManager->getCacheProvider($modx->getOption('cache_resource_key', null, 'resource'));
      $res = $modx->resource;
      $resourceCache->delete($res->getCacheKey());


      And when I save the resource I deactivate the clear cache for the resource. This is not the best idea because if I forget to deactivate the clear cache once, the whole cache will be cleared.

      But the bigger problem is, that my code in the plugin doesn't work. The delete method returns false. getCacheKey returns "mgr/resources/3" for the resource with id 3. Looks pretty good, but delete doesn't work.

      But isn't there an easier way to only update the cache for one resource???
      Sounds stupid to me to delete the whole cache if only one resource is changed!!

      Martin
        • 32316
        • 387 Posts
        Sounds stupid to me to delete the whole cache if only one resource is changed!!
        It is easy to see that there are changes you can make that could change other cached resources and thus require them to be updated as well - such as a change in a menu text. What is much harder is to figure out is how a change in one resource will effect others - much easier / safer / smarter(?) to just rebuild it all.
        • This is why the default behavior is to refresh at least the Context in which you add/update/remove a Resource. There are so many potential dependencies between cached items, it would be next to impossible to have them automatically tracked and cleared at such a granular level.
            • 8784
            • 32 Posts
            Martin,

            As Whistlemaker and OpenGeek have said, it makes perfect sense to delete all cache files for resources because the inevitable inter-dependancies.

            Martin, it sounds like you're attempting something similar to me perhaps? I'm building for a high-traffic environment. A few of my pages will ALWAYS need a cache file (other pages don't).

            My strategy:
            1. Flag my high-traffic pages with a TV ("Super page" or whatever).
            2. When the default cache refresh event clears all the cache files, immediately generate a new cache file for every resource flagged as a "Super page"--rather than waiting for the cache generation to be initiated by the next end-user browser request.

            Sounds simple, but I've hit some issues. I should probably start a new thread for this.


              • 37480
              • 9 Posts
              Hey,

              thank you for the replies! Well, you are right...stupid was the completely wrong word!

              If I think of a blog with 200 posts and I add a new one, you are right, it's probably the easiest and safest way to clear the whole cache, but there are 99% of the resources that are not effected. Now if I have a parse time of 3-4 seconds for a not cached page and about 0.2 seconds for a cached paged there is a huge difference. If most of the pages will be visited once between updates (clear caches) they will have a loading time of about 4 seconds. Otherwise it would be below 1 second.

              Do you think it would be the best solution to implement a script which builds the whole cache at one when I'm finished updating my resources?

              For example, I'm working with a site with more than thousand pages. If I correct a wrong spelled word in the content of a resource I don't want to rebuild the whole cache, or worse have it deleted!
              I see the problem with all the dependancies, but in this case I would be sure not to have any side effects.

              elvispresley2k, your solution doesn't sound too bad, but I want to have a high-speed site. Like I said my parse times are pretty high, so I need my sites in the cache. All of them, because some won't be called very often between updates and each updated kills the cache...


              • In my opinion the goal should always to get parse times under one second for any Resource when not yet cached.
                  • 37480
                  • 9 Posts
                  The goal yes, but I am afraid, that this is not always possible. I have a site with a couple thumbnails, comments, menus, a lot tvs and so on....they all need to be parsed. I'm sure, there might be a little room for some optimizations, but not that much!

                  So after all I think the only safe solution would be to write a script, that makes it possible to rebuild the whole cache if I need to!?!?
                  • If your site takes 4 seconds to parse, I'm pretty sure there's plenty that can be optimized o.O

                    You could indeed make something that re-creates the cache but that may just be solving the symptoms and not the cause..
                      Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                      Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.