A solution to increase PHP Performance for MVC Frameworks (ex: CodeIgniter, Symfony, Laravel, etc.) is to save the Cache to local disk. Since the cache is saved locally to the VM, there will be an improved performance boost.
However, this raises a couple of questions:
- How can cache be cleared from the local disk?
- And if there are multiple instance, how can cache be cleared in each instance?
The local disk can be written to programmatically by the application, but Azure Web Apps does not provide a way to read this easily. It may seem like it does on the SCM site, but the SCM site is in a separate application pool and the local disks are not shared between the pools. Meaning when you access the SCM’s local disk, you are not accessing the Web App’s local disk. So in order to access the web app’s local disk, we have to access it the same way the MVC Frameworks do – programmatically.
Now that we have the answer to question #1, how do we go about answering #2?
Well, the answer is in the title of the blog post. J
IIS has the ability to execute a Script on a W3WP process recycle. This includes: restarts, application settings updates, scaling out to new instances, etc. By no means is this a full list. See discussions online to find out more about recycles.
The concept here is to use a script to clear the cache on demand. However, if we link the script to the AppInit module, it will run every time there is a recycle. So to circumvent this running unnecessarily, we can add a Boolean variable which controls whether the cache should be cleared. Makes sense? Good! J
And to take this a step further, let’s keep a log of when the AppInit was triggered and if the cache was cleared.
Alright, let’s get down to the nitty gritty, code level.
This solution has 3 parts:
- appInit script
- Application Settings
Place this in wwwroot directory. This tells IIS to run the script defined in “initializationPage” on recycles. Hostname is the Web App name.
Here is sample code for the appInit script. All you have to do is copy it and insert the script to clear the cache for your particular framework.
$message = "================================n" . 'Time Stamp: '. date('m/d/Y H:i:s') ."n" . "--------------------------------n" . "*** CLEARING CACHE***n" . 'Computer Name: ' .$_SERVER['COMPUTERNAME'] ."n" . 'Instance ID: ' . $_SERVER['WEBSITE_INSTANCE_ID'] . "nn"; } // Write the contents back to the file file_put_contents('cache.log', $message, FILE_APPEND);
Add the following application setting and set the value to true.
Now that all components are in place, trigger a recycle by restarting or updating an Application Setting. If everything went well, we will have a message within cache.log. It should look something like this:
This post first appeared on MSDN Blogs | Get The Latest Information, Insights, Announcements, And News From Microsoft Experts And Developers In The MSDN Blogs., please read the originial post: here