2018-05-04 09:11
浏览 23


Let's say I want to have a variable which should keep it's value.

Simple example:

$files = array('file1.txt','file2.txt','file3.txt'); // list of the files to process
$file = file_get_contents($files[$i]);

So I'd like to process only one file per script run. And in my case the index of the currently processed file is $i.

From my pont of view there are two methods available:

  1. Store variable in a file. I can serialise (and unserialize upon initialization) any data and store in a text file. However, I should keep an eye on the script termination to be sure that the needed values will be stored before the script terminated.
  2. Use database. It looks bulky and as previously described - I should keep an eye on storing.

So both of the methods looks too comlicated. Is there any better solution?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • doushou3814 2018-05-04 12:57

    The solution depends on the task:

    1. Use a cache system (memcached, redis, etc.) - when you need a runtime storage (i.e. values may be deleted and will not break an application logic).

    2. Use DB(MySQL, Mongo) - when you need a permanent storage for values (i.e. values may not be deleted - this will break an application logic).

    3. Use Files for both variants.

    Sessions is good enough when you need to store data for one user between requests.

    The best solution is the solution that the most suitable for your task. I would recommend to use either 1 or 2 variant: in projects where I am working we use memcached for a runtime storage and mongodb for a permanent storage.

    解决 无用
    打赏 举报

相关推荐 更多相似问题