如何存储PHP Trie以供以后使用? [英] How to store PHP Trie for all later uses?

查看:110
本文介绍了如何存储PHP Trie以供以后使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用PHP设计一个涉及Trie数据结构的应用程序。

I'm designing an application in PHP which involves Trie data structure.

为了节省时间,我们使用Trie。

For time efficient prefix search, I'm using Trie.

我正在使用数据库中的记录来构造Trie。

I'm constructing the Trie using records from the database.

现在,数据库有数百万条记录。因此,每次创建新的Trie并针对每个新的用户请求进行搜索都是不可行的。

Now, the database has millions of records. So it is not feasible to everytime create the Trie and then search in it, for every new user request.

相反,我只能创建一次Trie并以某种方式存储此信息,这样就不必为每个新的用户请求都重新创建它,然后可以立即进行搜索。是否可以使用PHP以某种方式缓存创建的Trie(不仅针对一个用户会话,还针对所有用户请求)?

Instead can I create the Trie only once and somehow store this information, such that it does not have to be re-created for every new user request, and then searching can be immediately done. Is there somehow I can cache the created Trie (not just for one user session, but for all user requests) using PHP?

任何帮助将不胜感激。

推荐答案

您有一个几个标准选项。

You have a couple of standard options.

使用简单的缓存(例如 memcached )将数据库结果缓存在内存中

Cache the database result in memory, using a simple cache like memcached

使用 Redis 进行缓存,也许可以利用某些缓存其额外功能。这可能涉及到以下过程:将数据加载到REDIS中的结构中,并使Trie搜索代码直接针对Redis,而不是针对数据库结果集。

Cache using Redis, perhaps taking advantage of some of its extra features. This might involve a process where you load the data into a structure in REDIS and have your trie search code work against Redis directly rather than the database result set.

,您将在可接受的一段时间内缓存结果,并且由于数据库结果将以某种形式存储在内存中,因此RDBMS上没有负载。

In either case, you are going to cache the result for some period of time that is acceptable, and since the database result will be in memory in some form, there is no load placed on the RDBMS.

在您的相关问题中,您指出变量的原始序列化形式的大小约为200mb。这在Redis的最大对象大小(512mb)之内,但是对于memcached可能会出现问题。这些天,我个人将Redis用于大多数应用程序服务器缓存。

In your related question, you indicated that he raw serialized form of the variable would be about 200mb in size. That is well within the max object size (512mb) for Redis, but could be problematic for memcached. I personally use Redis for most app server caching these days.

这篇关于如何存储PHP Trie以供以后使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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