1

I`m developing a simple PHP application that is launched on an Apache2. I use APCu to speed up some parts of the application and use a special key for each part and package. The application is based on Composer and some time I need to update application scripts while the site is live.

I`m looking for a way to clear the cache if the version of the application is changed dynamically.

aynber
  • 22,380
  • 8
  • 50
  • 63
Mostafa Barmshory
  • 1,849
  • 24
  • 39
  • 1
    You could write a script that runs `apcu_clear_cache()` and add it to your deployment pipeline in Jenkins. – Sherif Apr 09 '20 at 13:15
  • But the product server is not reachable with my CI services, and I do not know when the server script will be updated. – Mostafa Barmshory Apr 09 '20 at 13:17
  • How do you deploy code to production then? As in how is the new code pushed to the production servers? – Sherif Apr 09 '20 at 13:18
  • The code is pushed into a GitHub repository (which is registered on packagist) and then I login to the server by ssh and run the composer update command.... – Mostafa Barmshory Apr 09 '20 at 13:23
  • One possible solution here is to have the CI server SSH into the production servers to run the `apcu_clear_cache()` script. The other is to make available some API hook that can trigger this in production from the CI server. Which one will work best depends on your setup and platform. – Sherif Apr 09 '20 at 13:23
  • This manual process of ssh and run composer should actually be a part of your CI pipeline. You should be having Jenkins do this for you. That way just adding the clear cache script is just one more step included before the composer update. – Sherif Apr 09 '20 at 13:24
  • Is it safe to add an API hook? – Mostafa Barmshory Apr 09 '20 at 13:25
  • You can have it only listen on LAN, assuming your CI server and prod server share the same local area network. If not you would have to find another way to secure its communication such as using an API key/password for remote access. – Sherif Apr 09 '20 at 13:28
  • Dear @Sherif, I`ll check if it is possible to combine your solution with the composer itself. – Mostafa Barmshory Apr 13 '20 at 14:56

1 Answers1

1

Finally, I found a very simple and effective solution. The best way to clean the cache on a project update is to put the script in composer. Here is an example:

{
    "scripts": {
        "post-update-cmd": "Util\\CacheController::clearCache()"
    }
}

Where the clearCache function will remove all cached data. So whenever anyone tries to update the project, then the cache will be removed.

Mostafa Barmshory
  • 1,849
  • 24
  • 39