Skip to content

Instantly share code, notes, and snippets.

@noogen
Created February 8, 2017 16:25
Show Gist options
  • Save noogen/15c8ca088a02b57b5fa005b5c0e94bd4 to your computer and use it in GitHub Desktop.
Save noogen/15c8ca088a02b57b5fa005b5c0e94bd4 to your computer and use it in GitHub Desktop.
minute counter
<?
$counterBaseDir = './counter/';
$files = glob($counterBaseDir . '*.txt')
$now = date("YmdHi");
foreach ($files as $file) {
$fileParts = explode($file, "_");
$fileDate = $fileParts[0];
$tenant = $fileParts[1];
$key = $fileParts[2];
if ($fileDate < $now) {
$counter = strlen(file_get_contents($file));
// store counter value somewhere then delete file
unlink($file);
}
}
<?
/**
* The task fo this script is to store counter for each minute in the day.
*
* A cron script will come around, read all files for each minute
* And use file string length to determine the count for each tenant + key.
**/
$counterBaseDir = './counter/';
$tenant = $_GET["tenant"];
$key = $_GET["key"];
$tenant = preg_replace('/[^A-Za-z0-9]+/', '-', $tenant);
$key = preg_replace('/[^A-Za-z0-9]+/', '-', $key);
$today = date("YmdHi");
$file = $today . '_' . $tenant . '_' . $key;
// get counter dir
$counterDir = $counterBaseDir;
if(!is_dir($counterDir)) {
if (!mkdir($counterDir, 0755, true)) {
die('Failed to create counter folders...');
}
}
$counterFile = $counterDir . '/' . $file;
$fp = file_put_contents($counterFile, '1' , FILE_APPEND | LOCK_EX);
// return nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment