自定义 PHP 缓存
我有一个应用程序需要通过多个页面请求(针对同一用户/会话)缓存大量数据(有时甚至是 MB)。经过一些谷歌搜索等之后,我得出的结论是,最好通过将缓存文件写入磁盘来实现缓存机制(如果您认为有更好的选择,请纠正我)。
现在,我的想法是拥有一个根缓存文件夹,在其中为每个会话 ID 创建文件夹,以免覆盖单独会话中使用的任何缓存数据。然后,对于每个数据块,我将创建一个唯一的标识符,每当我想再次检索它时,它都可以链接到数据。然后,数据将被序列化为字符串格式(使用默认的 PHP“序列化”函数),然后将其写入适当的文件。
我不太确定如何实现的是缓存文件的清理。在某些时候,或者不再需要数据,例如当会话过期或许多其他原因时。由于在每个页面请求期间检查这一点可能会产生太多开销,因此我希望必须使用某种调度程序在外部执行此操作。但是,我不能保证我的应用程序将在 UNIX 环境上运行,因此我还必须考虑其他平台(Windows、Mac)。是否有一个任何人都可以想到的通用解决方案,可以跨平台而没有太多麻烦?
我还认为也许有一种方法可以智能地检查或标记某些要清理的文件,而不必单独检查所有现有文件。我正在考虑也许存储他们上次访问的时间戳或其他东西,但是除了时间之外可能还有其他标准可能使缓存的数据过时,例如应用程序中触发的异常(尽管我可以说,每当发生这种情况时,整个缓存都会被删除)会话将被清空或类似的事情)。
任何有关这些问题的建议将不胜感激!
I have an application that is in need of caching large amounts of data (sometimes even MBs) over multiple page request (for the same user/session). After doing some Googling etc. I've concluded that it is likely best to implement the caching mechanism by writing cache files to disk (please correct me if you think there are better alternatives).
Now, my idea was to have a root cache folder, within which I create folders for each session ID to not overwrite any cached data used in separate sessions. Then for each block of data I will create an unique identifier which can be linked to the data whenever I want to retrieve it again. The data will then be serialized to a string format (using the default PHP 'serialize' function) after which it is written to the appropriate file.
The thing I'm not so sure on how to implement is the clean up of the cached files. At some point either the data is not needed anymore, for example when the session has expired or a number of other reasons. Since it will likely be too much overhead to check for this during each page request, I expect to have to do this externally using some kind of scheduler. However, I cannot guarantee that my application will run on a UNIX environment, so I'd have to consider other platforms as well (Windows, Mac). Is there a general solution that anyone can think of that would be cross-platform without too much hassle?
I'm also thinking that there maybe is a way to intelligently check or mark certain files to be cleaned up, without have to check all the existing files separately. I was considering maybe storing their last accessed timestamp or something, but there may be other criteria besides time that could make the cached data obsolete, such as an exception being triggered in the application (though I could say that whenever that happens the entire cache for that sessions will be emptied or something like that).
Any suggestions on these issues would be very much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果安装了 MemCache,则可以使用它进行缓存。文件缓存速度更快,并且您可以给它一个过期时间,因此在给定的时间段后它将自动从缓存中删除。
If you got MemCache installed, you can use that for caching. It is faster that file cache, and you can give it an expiration time, so it will automatically be removed from the cache after a given period of time.
Windows 和 Unix 都有计划作业支持 - Unix/Linux 为 cron,Windows 为 'at'。编写一个 PHP 脚本来扫描缓存目录并将删除标准应用于所发现的内容是一件简单的事情。上次访问时间戳是微不足道的,它基于缓存的文件内容或其他触发器,稍微不那么重要。
Both Windows and Unix have scheduled job support - cron for Unix/Linux, and 'at' for Windows. It would be a simple matter to whip up a PHP script to scan your cache directory and apply your deletion criteria to what it finds. Last access timestamp is trivial, basing it on cached file contents or other triggers slightly less so.