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条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?