这是一个合理的缓存方法吗?
我使用 PHP 将 LaTeX 渲染为 png,作为 CMS 的一部分,我需要某种简单的缓存机制,因为服务器渲染 png 需要大约 2 秒的时间。 我获取原始 TeX 并通过 md5 函数运行它,然后将哈希值与当前时间戳一起插入数据库,并使用
I'm using PHP to render LaTeX to pngs as part of a CMS, I need some kind of simple caching mechanism because it takes the server about 2 seconds to render the png. I take the raw TeX and run it through the md5 function, then I insert the hash into the database along with the current timestamp, and use the <hash>.png as the filename. Every time the image is accessed the timestamo is updated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于这种类型的场景,我更喜欢使用磁盘上的临时目录。
根据要求:
For this type of scenario, I prefer to use a temporary directory on disk.
On request:
这听起来不错。 如果您可以更改 .png 的名称、数据库 ID 甚至哈希本身,以便图像也可以缓存在客户端。
This sounds fine. If you could change the name of the .png to say, the database ID or even the hash itself so that the image can be cached on the client side as well.
嗯,我认为这是一个很好的方法,但是在散列之前您可能需要对 LaTeX 进行一些额外的处理。 否则,空格的更改(以及可能的注释和其他一些内容,具体取决于您的输入方法)将导致不同的哈希值。
另外,更新时间戳的目的是什么? 作为删除长时间未访问的 PNG 的批处理方法的一部分?
Hmm, I think that it's a good approach, but you'll probably want to do some additional processing on the LaTeX before you hash it. Otherwise, changes in whitespace (and potentially comments and some other things, depending on your input method) would result in different hashes.
Also, what's the purpose of updating the timestamp? As part of a batch method that removes PNGs that haven't been accessed in a long time?
如果 .png 在渲染后可能会发生变化,我想您还应该在 .png 更新时使用某种观察来解释这一点(检查线程、设置方法来更新最后修改的密钥或其他内容: ) )
If the .png's could change after they've been rendered, I guess you also should account for that using some sort of observing whenever the .png is updated (checking thread, set methods to update a last-modified key, or something else :) )