PHP filemtime vs MySQL最近更新的时间戳,用于CMS中的图像缓存 [英] PHP filemtime vs MySQL last updated timestamp for image caching in CMS

查看:82
本文介绍了PHP filemtime vs MySQL最近更新的时间戳,用于CMS中的图像缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定将图像的最后修改日期存储在MySQL数据库中还是使用PHP函数

I'm trying to determine if it's better to store an image's last modified date in a MySQL database or use the PHP function filemtime.

就我而言,所有网站信息都存储在数据库(cms)中,因此总会有一个查询来拉取图像路径等.问题是出于缓存目的,我需要将HTML输出如下<img src="/img/photo.jpg?v20190613" />.根据我在php.net网站上阅读的内容,此功能已被缓存.那么,它会使用更少的资源向存储最新时间戳的数据库表中添加字段还是每次都使用此功能吗?两种方式都有优势吗?我正在寻找可以提供最佳性能的东西.

In my case, all of the website info is stored in a database (cms), so there is always a query to pull the image path, etc. The question is for caching purposes I need to have my HTML output something like this <img src="/img/photo.jpg?v20190613" />. From what I read on the php.net website this function is cached. So would it use fewer resources to add a field to the database table that stores the last updated timestamp or use this function each time? Are there any advantages either way? I'm looking for whatever is going to give the best performance.

推荐答案

我不会使用

I wouldn't use filemtime(), for one specific reason: it only works for checking local files on the same host where the PHP code is running.

在现代应用程序部署中,通常会将PHP代码部署到与提供静态资源的主机不同的主机上.实际上,通常将PHP代码部署到负载均衡器后面的多个应用服务器上,因此您可以进行滚动部署而不会造成任何停机.

It's common in modern app deployments that you deploy PHP code to a different host than the one that serves static resources. In fact, it's typical to deploy PHP code to multiple app servers behind a load-balancer, so you can do rolling deployments without suffering any downtime.

您今天可能没有这种体系结构.您可以将PHP代码部署到静态文件所在的同一主机上,也可以将数据库部署到同一主机上.但是,由于您的应用程序超出了一个主机的大小,或者在部署期间需要不间断的操作,因此最终将需要扩展到多个主机.最好尽早进行计划,并且不要实施阻止您横向扩展的代码.

You might not have this architecture today. You might deploy PHP code to the same host where your static files live, and your database too. But as your app outgrows a single host, or needs to have uninterrupted operation during deployments, you'll eventually need to scale out to multiple hosts. It would be better to plan for that early, and don't implement code that prevents you from scaling out if you can avoid it.

所有PHP应用程序服务器都不能直接访问保存了静态文件的文件系统.如果他们这样做,则必须执行以下操作之一:

None of the PHP app servers would have direct access to the filesystem where the static files are kept. If they did, you'd have to do one of the following:

  • 使用更多的存储空间在每个PHP应用服务器上存储静态文件的重复副本.然后担心保持它们同步,使用一些后端脚本来连续检查所有主机是否具有相同的文件集,等等.
  • 通过NFS或类似协议为远程安装到所有PHP应用程序主机的静态文件创建文件系统.然后filemtime()检查会变慢一些,因为它们要遍历NFS.而且您还必须担心NFS挂载会消失,安全性强制措施,在添加新的应用程序主机时配置NFS等等.
  • Store duplicate copies of the static files on each PHP app server, using lots more storage space. Then worry about keeping them in sync, have some back-end scripts to continually check that all hosts have the same set of files, etc.
  • Make the filesystem for static files remotely mounted to all the PHP app hosts, via NFS or similar protocol. Then the filemtime() checks would become somewhat slower, because they're going over NFS. And you'd have to worry about NFS mounts going away, security enforcement, configuring NFS when you add a new app host, and so on.

由于这些原因,我选择将时间戳记存储在数据库中,因为无论如何您已经在其中存储了文件元数据(路径名).

For these reasons I'd choose to put the timestamp in the database, since you already have file metadata (the pathname) stored there anyway.

这篇关于PHP filemtime vs MySQL最近更新的时间戳,用于CMS中的图像缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆