dscw1223 2013-06-20 07:41 采纳率: 50%
浏览 8
已采纳

我可以以某种方式全局并临时保存函数的返回数据吗?

If we have a PHP function which makes difficult calculations based on a certain input, for example:

function count_score($var1, $var2, $var3){
   // based on $var1, $var2 and $var3 we run
   // a 5000-lines long calculation,
   // resulting in one $output
   return $output;
}

Now, what happens here:

  • the function takes about 1 second to run
  • there aren't many combinations of $var1, $var2 and $var3 that might be inputted (roughly around 3000)

What I can do:

I can store the result of the function based on every combination and check whether it has been calculated before. I can store this to $_SESSION, per user, so every time the function is called, the results are looked up in the $_SESSION.

However, what if I want to store these results for all users? I can't use $_SESSION then, as it's not shared globally across the site for all users. How can I store it then?

The only time when the results of the calculations are different is when I change the function, which happens about once in 48 hours. Then I can probably manually specify the function revision and check for results of that revision. But how/where do I store all this?

EDIT Optionally, the method should work in CRON (without the http layer), but it's not necessary.

  • 写回答

4条回答 默认 最新

  • dsa45132 2013-06-20 07:52
    关注

    I suggest three simple solutions:

    1. APC - http://php.net/manual/en/book.apc.php
    2. Memcache - http://memcached.org/
    3. Memory (MySQL Storage Engine) - http://dev.mysql.com/doc/refman/5.5/en/memory-storage-engine.html

    Memcache will automatically handle the memory available, so if you are going to store 1 byte more above the limit, memcache will delete the oldest stored value in order to "make space". APC too.

    The difference between APC and Memcache is that APC doesn't need a separate installation and is not distributed, so you can't store the data on APC and get this data from a pool of servers. It be the best solution if your application runs on a single server.

    With Memory, you can create a classic MySQL table which is easier to maintain and setup. Memory is about as fast as APC and Memcache, because with this Storage Engine, all the data is persisted in RAM, so if you reboot your server you are going to loose all the data stored in the table, but it is very fast and fits your needs.

    In my opinion APC is the best solutions for you.

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

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害