记住请求之间 PHP 中的对象状态

发布于 2024-08-11 04:46:48 字数 624 浏览 7 评论 0原文

我正在编写一个应用程序,并且我将拥有许多不同语言的一些字典值。我知道我可以使用 GetText,但是 AFAIR 文件必须在编辑后进行编译,并且我想让用户编辑字典,并且我无法在服务器上重新编译 .mo 文件。我不知道会使用多少种语言,所以解决方案必须是有弹性的。

我设计了数据库,所以它工作得很好,模式看起来也很好,但对于每个字典值都有几个连接,所以解决方案不是太快。

因此,我正在考虑存储字典值一次,并仅在编辑其中的值后刷新它。不幸的是,PHP 静态变量无法使用,因为它们在请求结束时消失。我不想使用会话,因为我必须为每个用户进行数据库调用。理想情况下,我想像在 Java 中一样使用静态变量 - 只要应用程序存在于 JVM 中,它们就存在。

为所有用户(不是每个用户)长时间(不是每个请求)存储一些变量(在我的示例中 - 字典、字典条目等)的最佳解决方案是什么?

我正在考虑制作类似 DictionaryValues 类的东西,将其序列化一次,然后在每个请求时反序列化它。每次有人编辑字典时,对象都会再次序列化并替换旧的序列化对象。当然,与读取字典值的频率相比,编辑很少发生。

这是一个好的解决方案吗?我应该序列化对象并将其存储在磁盘上还是写入数据库?哪一个更快?

也许您找到了更好的解决方案来解决这个问题?

I am writing an application and I will have some dictionary values in many different languages. I know I could use GetText, but AFAIR files have to be compiled after editing, and I want to let user edit dictionary, and I can't recompile .mo files on the server. I don't know how many languages will be used, so solution must be elastic.

I designed database so it works good, and schema looks fine, but for every dictionary value there are a couple of joins, so the solution is not too fast.

For that reason I am thinking of storing dictionary values once, and refresh it only after editing a value in it. Unfortunately, PHP static variables couldn't be used, because they die at the end of the request. I don't want to use session, because I will have to make DB calls per user. Ideally I would like to use static variables just as in Java - they are living as long as application live in JVM.

What is the best solution to store some variables (in my example - dictionaries, dictionary entries, etc.) for a long time (not per request) for all users (not per user)?

I am thinking of making something like DictionaryValues class, serialize it once, and then deserialize it every request. Every time somebody will edit dictionary, object will be serialized again and will replace old serialized object. Of course editing will occur rarely compared to how often dictionary values will be read.

Is this a good solution? Should I serialize the object and store it on the disk or write it to the database? Which one is faster?

Maybe you find better solution for that problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

断舍离 2024-08-18 04:46:48

您可以尝试 PHP APC。它快速且易于使用。您可以序列化/反序列化对象并轻松存储/获取它们。

在分布式环境中,您可以使用 memcache 执行相同的功能。

You could try PHP APC. It is fast and easy to use. You can serialize/deserialize objects and store/get them easily.

In a distributed environment, you could perform the same functions using memcache.

护你周全 2024-08-18 04:46:48

如果 memcache / APC 太多或不可能,有几个选项:

您可以经常将数据库值“编译”到 PHP 文件中并将其包含在内。这将是最快的解析和处理。

如果您不需要每个页面中的每个字典值(很可能,不是吗?),您可能需要使用无需 JOIN 即可工作的缓存表。从足迹的角度来看,这将是最好的解决方案。

A few options if memcache / APC is too much or not possible:

You could "compile" your database values frequently into a PHP file and include that. That would be the fastest parsing- and processing-wise.

If you don't need every dictionary value in each page (very likely, isn't it?) you might want to use a caching table that can work without the JOINs. That would be the best solution from a footprint perspective.

烟花易冷人易散 2024-08-18 04:46:48

有几个选项:

  1. 您可以使用共享内存(shmopmemcache)。
  2. 您可以缓存到文件或数据库 - 尝试 PEAR::Cache_Lite (或 APC 正如 @jldupont 建议的那样)

There are a couple of options:

  1. You could use shared memory (shmop or memcache).
  2. You could cache to a file or database - try PEAR::Cache_Lite (or APC as @jldupont suggests)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文