如何使用PHP只保存最后一小时的数据


How to save only last hour of data, with PHP

什么是只保存最后一小时信息的优雅方法?在我的情况下,我正在记录信息,例如:

Tue Nov 10 05:17:12 UTC 2015   't   apple
Tue Nov 10 05:42:27 UTC 2015   't   banana
Tue Nov 10 05:51:41 UTC 2015   't   carrot

我只需要最后一小时的信息。我想以一种简单且防弹的方式保存到一个或多个文本文件中。即不遍历目录以基于广泛的模式匹配来删除旧文件。如果我有2小时的数据,这不是问题,但我不希望少于1小时。

我有一个想法,交替使用2到3个文件,有时会登录到>1个文件,并在特定时间删除一个或其他特定文件,但我无法理解其中的逻辑。

假设PHP。因此,这是web请求的随需应变,而不是crontab。

我不久前处理过一个类似的问题。我的解决方案是使用两个文件,最多可以节省最后两个小时。在伪代码中:

 # two files
 file0 = a file
 file1 = another file
 # delete old files
 if file0 older than 2 hours, delete file0
 if file1 older than 2 hours, delete file1
 # select newest file, and delete on alternating hours
 if current hour mod 2 = 0 then
     newest file = file0; 
     delete file1;
 else
     newest file = file1;
     delete file0;
 # save data to end of file
 print data to file0; 
 print data to file1;
 # the file to use is: newest file

这与你想要的有点不同,因为它长达2个小时,但如果主要想法是你总是想要最后一个小时,这种方法会奏效。

以下是明细。F0=文件0;F1=文件1;a、 b,c=数据

 time    mod   data   clear   F0      F1      fileToUse
 23:45   1     a      F0      a       a       F1
 00:15   0     b      F1      ab      b       F0
 00:45   0     c      F1      abc     c       F0
 10:05   0     d      F0,F1   d       d       F0
 11:30   1     e      F0      e       de      F1

在@michaelCodes上重复答案,但使用了三个文件。这保证了data_file中有整整一个小时的数据。它一次只记录两个文件。

 # three files
 file0 = a file log starting at hour mod 0, deleted at hour mod 2
 file1 = a file log starting at hour mod 1, deleted at hour mod 0
 file2 = a file log starting at hour mod 2, deleted at hour mod 1
 # data_file is the last file written to (last hour + this hour)
 # current_file is just from the top of this hour (could be 1 record)
 # select newest file, and delete file from 2 hours ago
 switch (current hour mod 3)
   case 0:
     current_file = file0;
     data_file = file2;
     delete file1;
   case 1:
     current_file = file1;
     data_file = file0;
     delete file2;
   case 2:
     current_file = file2;
     data_file = file1;
     delete file0;
 # save data to all files that weren't just deleted
 print data to data_file; 
 print data to current_file;

换句话说,日志中的任何记录仅在创建后的60分钟内有效。您可以使用内存中的存储系统,如memcached/redis。这样的系统具有垃圾收集功能,您可以将它们设置为每小时对磁盘进行一次快照,也可以查询有效的密钥。