doudichu1358 2011-05-11 15:37
浏览 28
已采纳

如何在内存中保留数据结构以供多个用户使用,PHP中有多个页面请求

I'm new to PHP and I'm having no luck googling for a best practice that targets this scenario.

I have a data structure that's expensive to create, applies to all users of the site, is identical for all users of the site, and never changes once created. (OK -- it needs to change whenever I upload new versions of source code files.) It's not particularly large. Ideally, I'd like to create it one time -- the first time I need it -- and hold onto it thereafter, using the same instance for every user, every page request. Then it would be nice if it would get "nulled out" whenever I click "clear cached data" (I'm using Drupal).

I haven't found a tutorial for how to do this... I see how I can store information in the session, but that only applies to one user.

UPDATE

Inside the data structure there are anonymous functions (closures). I have read that these sometimes have issues serializing.

  • 写回答

3条回答 默认 最新

  • doushao5047 2011-05-11 15:41
    关注

    It sounds what you're looking for is some kind of cached object.

    There a number of different caching methods you could use in PHP:

    • In a file on the file system
    • Using APC which is an in memory local cache
    • memcached which is an in memory cache accessed over TCP/IP

    For each of these you'd likely want to serialize your data structure (with PHP's serialize()), save it to the cache store and then read it back and unserialize the data (with unserialize()).

    I would probably go with APC as by installing it you can easily benefit from it's opcode caching which is another function it can perform.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站