How to store PHP Trie for all later uses?

痴心易碎 提交于 2020-01-07 03:48:20

问题


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

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

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

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.

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?

Any help would be much appreciated.


回答1:


You have a couple of standard options.

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

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.

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.

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.



来源:https://stackoverflow.com/questions/44739521/how-to-store-php-trie-for-all-later-uses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!