Scenario of high Performance PHP log
High Performance PHP log scheme
The log is completed in two steps:
- Log information is written to the memory cache in real time to minimize performance overhead
- Crontab periodically collects writes from the memory cache to the database or file
Specific algorithm: (Laravel framework)
- Set a constant: imax=10000000, two variables iwrite=1, iread=1. ( iwrite Log write pointer, iread log read pointer, Iread <=iwrite, two variable values saved in memory cache)
- For each log generated, use "Cache::increment (iwrite)" To get the increment value of Iwrite, if the value is greater than IMAX, reset to 1, cache the log, key: ' Log_ ' +iwrite, Value: Log content
- crontab calls the command line every 30 seconds, collects the log, uses "Cache::increment (Iread)" To get the increment value of iread, if it equals iwrite, stops if the value is greater than IMAX, resets to 1, from The cache reads the logs and batches them into the library. Finally , the log is removed from the cache.
The memory cache can be used with APC, XCache, etc.
Cache::increment, which belongs to the Laravel framework built-in method, can be encapsulated if it is not a laravel frame.
Temporarily no time, first record ideas, later have time, practice, and then send out the code.
The above describes the high-performance PHP log scenarios, including aspects of the content, I hope that the PHP tutorial interested in a friend to help.