Do you have to purge the cache every time you want to view the URL you are trying to add as a result of scripting (script inserts/updates rows in tables in the Moodle database)?

Do you have to purge the cache every time you want to view the URL you are trying to add as a result of scripting (script inserts/updates rows in tables in the Moodle database)?

by Brian Lee -
Number of replies: 4

I am building an app that inserts/updates rows in some of the tables in the Moodle database. The tables are mdl_course_sections, mdl_course_modules, mdl_context and mdl_url. Yes, as you may have guessed, I am trying to script the action of adding a URL to a section minus writing to the logs. After inserting/updating rows to add a URL, correctly (I believe), I could not see the URL, immediately, after doing a browser refresh of the course page. I realized after doing a purge of the whole Moodle website ("Site administration" -> "Development" -> "Purge all caches"), I could see the URL that I intended to add.

Do you have to purge the cache every time you want to view the URL that you intended to add by scripting? (You do not have to purge the cache after manually adding the URL to see the URL.)

Thank you.

Average of ratings: -
In reply to Brian Lee

Re: Do you have to purge the cache every time you want to view the URL you are trying to add as a result of scripting (script inserts/updates rows in tables in the Moodle database)?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

No, you should not have to purge all the caches manually.

Of course, your script cannot just shove data into the database and expect the Moodle caches to magically know which ones need to be cleared. Your script needs to clear the relevant cache. That probably means calling 

rebuild_course_cache($courseid, true);

In reply to Tim Hunt

Re: Do you have to purge the cache every time you want to view the URL you are trying to add as a result of scripting (script inserts/updates rows in tables in the Moodle database)?

by Brian Lee -

Thanks, Tim. Exactly the answer that I was looking for. You have been ever so helpful.

In reply to Brian Lee

Re: Do you have to purge the cache every time you want to view the URL you are trying to add as a result of scripting (script inserts/updates rows in tables in the Moodle database)?

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

If you want to purge caches during development you can do it slightly more quickly than via the web page by running purge_caches.php from moodle\admin\cli

In reply to Marcus Green

Re: Do you have to purge the cache every time you want to view the URL you are trying to add as a result of scripting (script inserts/updates rows in tables in the Moodle database)?

by Brian Lee -

Haven't really tried your solution, yet. I assume that this script purges the cache for every course page in the whole Moodle site. Thanks, and if I ever feel the need to try your solution, I will definitely consider.