今天遇到redis自动关闭的问题,有个读取 缓存 函数问题是这样的: 先去redis或memcache缓存读取,有就返回读取数组,没有就去读取文件缓存,有就将该文件数组就返回,然后写入redis或memcache缓存和文件缓存。 问题就出在redis关闭后,一直会绕过去读取file
今天遇到redis自动关闭的问题,有个读取缓存函数问题是这样的:
先去redis或memcache缓存读取,有就返回读取数组,没有就去读取文件缓存,有就将该文件数组就返回,然后写入redis或memcache缓存和文件缓存。
问题就出在redis关闭后,一直会绕过去读取file文件缓存,读取后把新的数据写入到文件去。
$strlen = file_put_contents($cachefile, $arr);//写入缓存文件
如果这里没有使用LOCK_EX,当高并发使用这个函数的时候,就有可能出现,一个php还没有把文件写完整,另外一个php就来读取,结果发现返回内容不是数组,又接着写入一个新的内容,循环不停的写入,读取,错误,写入。
这时候要看情况而定了,假如缓存文件是对应不同名称,同时被并发操作可能不大的情况。可以不LOCK_EX,毕竟会损耗性能,当有些时候不LOCK_EX损耗更大,是个死循环。
原文地址:给你的PHP写入函数file_put_contents加个锁, 感谢原作者分享。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools