1

Assume I have a system in PHP that involves class discovery. The result of the class discovery is cached for performance.

Now I had the idea to attempt to clear or invalidate the cache, if the code changes. Either because a developer did something, or because a new version of a 3rd party library was downloaded.

One idea would be to compare file hashes for the class file. But doing this with e.g. md5(file_get_contents($file)) seems rather costly, and not something we want to do in every request / process.

Is there another, faster way to get hash for a PHP class, that produces a different result if the code changes?

I imagine that the answer is no, but one can always ask and hope.

donquixote
  • 4,877
  • 3
  • 31
  • 54
  • 1
    You can check out the speed of different hashes and use [`hash_file`](http://php.net/manual/en/function.hash-file.php). You'd possibly be able to compare modification dates and sizes, too. It's how most FTP programs do it. – h2ooooooo Jun 25 '17 at 18:05
  • I haven't looked in the context of your question, but I wonder if you couldn't figure out a way using the the ReflectionClass: http://php.net/manual/en/class.reflectionclass.php since it is meant for getting info on PHP classes – kjones Jun 25 '17 at 18:07
  • How about just invalidating the cache on deployment? – localheinz Jun 25 '17 at 18:11
  • @h2ooooooo Oh great, this is already one step better than `md5(file_get_contents())` :) – donquixote Jun 25 '17 at 18:21
  • @kjones Yes obviously a `ReflectionClass::getClassHash()` would do the job, but such a method does not currently exist. – donquixote Jun 25 '17 at 18:26
  • @localheinz Yes. This is what I am doing right now. I was just wondering if there is an alternative, and how expensive it would be. – donquixote Jun 25 '17 at 18:29
  • @localheinz On the project I work on, clearing caches is usually quite costly, and it would slow down development doing this a lot on the local instance of the project. Time is money :) – donquixote Jun 25 '17 at 18:31
  • How about invalidating and warming the cache on deployment, then? If a deployment is the only way the code can change, then it's probably the best solution to only invalidate and warm up the cache during deployment. – localheinz Jun 25 '17 at 18:32
  • @localheinz It is the only way that the code can change on production. But on a local development instance, code changes all the time. And not having to clear the cache locally can save precious time. – donquixote Jun 25 '17 at 18:35

1 Answers1

1

I think you can use filemtime function to get the last modified of the file instead of checking the content of the file. Sorry if it's not what you want to.

ThucVu
  • 95
  • 7
  • It clearly is or can be an improvement over `md5(file_get_contents())`. It is probably good enough for the majority of cases. – donquixote Jun 25 '17 at 18:24
  • Of course it still means one has to know which file a class was defined in. `ReflectionClass::getFileName()` can do this. – donquixote Jun 25 '17 at 18:31
  • 1
    See also https://stackoverflow.com/questions/14697805/how-to-check-if-a-file-has-changed – donquixote Jun 25 '17 at 18:33