Add a lock to your PHP write function file_put_contents.

Source: Internet
Author: User
Tags php write
Today, when redis is automatically disabled, there is a problem of reading the cache function: first go to redis or memcache to read the cache, and then return to read the array. If not, read the file cache, if yes, the file array is returned, and then written to redis or memcache cache and file cache. The problem is that after redis is closed, it will always read the file

Today, when redis is automatically disabled, there is a problem of reading the cache function: first go to redis or memcache to read the cache, and then return to read the array. If not, read the file cache, if yes, the file array is returned, and then written to redis or memcache cache and file cache. The problem is that after redis is closed, it will always read the file

Today, when redis is automatically disabled, there is a problem with reading the cache function:

Read from the redis or memcache cache first. If yes, the system returns the read array. If no, it reads the File Cache. If yes, the system returns the array of the file and writes it to the redis or memcache cache and the File Cache.

The problem is that after redis is closed, it will always read the file Cache and write new data to the file.

$ Strlen = file_put_contents ($ cachefile, $ arr); // write the cached File

If LOCK_EX is not used here, when using this function in high concurrency, it may appear that one php has not completely written the file, and the other php will read it, the result shows that the returned content is not an array, and a new content is written, which is repeatedly written, read, error, and written.

At this time, it depends on the situation. If the cached file corresponds to a different name, concurrent operations may not happen. LOCK_EX can be avoided. After all, it will consume performance. When LOCK_EX does not consume more, it is an endless loop.

Original article address: Add a lock to your PHP write function file_put_contents. Thank you for sharing it with me.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.