English 中文(简体)
缓存 PHP 对象至文件的方法?
原标题:
  • 时间:2008-09-24 12:48:37
  •  标签:

在ASPNET中,我逐渐爱上了Application和Cache存储。它们真的很棒。对于未经培训的人来说,您可以将数据逻辑对象投放到其中,然后,您只需要一次查询数据库就可以获得一些数据。

By far one of the best ASPNET features, IMO.

I ve since ditched Windows for Linux, and therefore PHP, Python and Ruby for webdev. I use PHP most because I dev several open source projects, all using PHP.

不用说,我已经探索了PHP在缓存数据对象方面所提供的功能。到目前为止,我已经尝试了:

  1. Serializing to file (a pretty slow/expensive process)
  2. Writing the data to file as JSON/XML/plaintext/etc (even slower for read ops)
  3. Writing the data to file as pure PHP (the fastest read, but quite a convoluted write op)

I should stress now that I m looking for a solution that doesn t rely on a third party app (eg memcached) as the apps are installed in all sorts of scenarios, most of which don t have install rights (eg: a cheap shared hosting account).

所以回到我现在正在做的事情,将持久化到文件是否安全?生产服务器安全的规则1一直是禁用文件写入,但我真的看不到PHP在没有写入时如何缓存。有没有任何提示和/或技巧来提高安全性?

Is there another persist-to-file method that I m forgetting?

在“有限”的环境中是否有更好的缓存方法?

最佳回答

序列化非常安全且常用。然而,有另一种选择,那就是缓存到内存中。请查看memcachedAPC,它们都是免费且性能非常高的。 这篇文章讨论了PHP中不同的缓存技术,可能也会引起您的兴趣。

问题回答

Re: Is there another persist-to-file method that I m forgetting?

It s of limited utility but if you have a particularly beefy database query you could write the serialized object back out to an indexed database table. You d still have the overhead of a database query, but it would be a simple select as opposed to the beefy query.

Re: Is persisting to file secure? and cheap shared hosting account)

The sad fact is cheap shared hosting isn t secure. How much do you trust the 100,500, or 1000 other people who have access to your server? For historic and (ironically) security reasons, shared hosting environments have PHP/Apache running as a unprivileged user (with PHP running as an Apache module). The security rational here is if the world facing apache process gets compromised, the exploiters only have access to an unprivileged account that can t screw with important system files.

坏的一面是,这意味着无论何时你使用PHP写入文件,该文件的所有者都是相同的不受特权保护的Apache用户。这对系统上的每个用户都是正确的,这意味着任何人都可以读写文件。上述情况中的理论黑客也将可以访问文件。

PHP中还存在一种持久的不良做法,即将目录的权限设置为777以使非特权的Apache用户可以将文件写出,然后保留目录或文件在该状态下。 这会给系统上的任何人提供读/写访问权限。

Finally, you may think obscurity saves you. "There s no way they can know where my secret cache files are", but you d be wrong. Shared hosting sets up users in the same group, and most default file masks will give your group users read permission on files you create. SSH into your shared hosting account sometime, navigate up a directory, and you can usually start browsing through other users files on the system. This can be used to sniff out writable files.

解决方案不太好看。一些主机提供CGI Wrapper,让您将PHP作为CGI运行。这里的好处是PHP将作为脚本的所有者运行,这意味着它将作为您而不是非特权用户运行。问题解决!新问题!传统的CGI在二月份像糖蜜一样缓慢。

有FastCGI,但FastCGI很挑剔,需要不断调整。许多共享主机都没有提供。如果您找到一个可以提供的共享主机,很可能他们会启用APC,并甚至能够提供memcached机制。

I had a similar problem, and thus wrote a solution, a memory cache written in PHP. It only requires the PHP build to support sockets. Other then that, it is a pure php solution and should run just fine on Shared hosting.

http://code.google.com/p/php-object-cache/

What I always do if I have to be able to write is to ensure I m not writing anywhere I have PHP code. Typically my directory structure looks something like this (it s varied between projects, but this is the general idea):

project/
  app/
  html/
    index.php
    data/
  cache/

app 不可被网络服务器写入(最好不要写入index.php)。cache 可被写入且用于缓存被解析的模板和对象等数据。 data 可能需要被写入,取决于需要。即,如果用户上传数据,则数据会被存入data文件夹。

网络服务器被指向project/html,并使用方便的方法来设置index.php作为项目中每个页面要运行的脚本。您可以在Apache中使用mod_rewrite,也可以使用内容协商(我更喜欢,但通常不可能),或者您喜欢的其他方法。

你所有的实际代码都存储在 app 中,它不能直接被 Web 服务器访问,但应添加到 PHP 路径中。

这对我来说在多个项目中都运作得相当不错。例如,我甚至能够让维基媒体使用修改过的这种结构。

哦...我会使用serialize()/unserialize()来进行缓存,尽管生成PHP代码具有一定的吸引力。我所知道的所有模板引擎都会生成用于执行的PHP代码,使得后期解析非常快。

如果您可以访问数据库查询缓存(即MySQL),则可以使用序列化对象并将它们存储在数据库中。数据库会负责将查询结果存储在内存中,因此速度应该相当快。

你没有明确说明为什么要缓存对象。你是想加快缓慢的数据库查询速度吗?是为了避免昂贵的对象实例化,避免重复生成复杂的页面,维护应用程序状态,还是仅仅出于你对对象进行着慢慢积累的习惯?

鉴于低成本共享托管的可怕限制,最好的解决方案将取决于你想要实现的目标。选择最便宜的共享托管意味着你必须接受你不能使用最好的工具的事实。这些数字很难量化,但在托管成本、网站性能和开发人员时间(即-快速、廉价或容易)之间存在权衡。

It s in theory possible to store objects in sessions. That might get you past the file writing disabled problem. Additionally you could store the session in a mysql memory backed table to speed up the query.

一些托管位置可能已经编译了APC。这将允许您将对象存储在内存中。





相关问题
热门标签