<?php
/**
* Indexes content of time values for short-lived tasks with hard-coded limits.
*/
class TaskIndex {
private $taskData = []; // Array to store task data (time value => content)
private $maxTasks = 10; // Maximum number of tasks to store
private $timeWindowSeconds = 60; // Time window in seconds to consider
public function addTask($timestamp, $content) {
// Calculate the time difference from the current time
$timeDiff = time() - $timestamp;
// Check if the task is within the time window
if ($timeDiff <= $this->timeWindowSeconds) {
$this->taskData[$timestamp] = $content; // Store the task data
// Limit the number of tasks stored
if (count($this->taskData) > $this->maxTasks) {
// Remove the oldest task
reset($this->taskData); // Reset the internal pointer to the beginning
if (!empty($this->taskData)) {
unset($this->taskData[key($this->taskData)]); // Remove the oldest entry
}
}
}
}
public function getTaskContent($timestamp) {
// Retrieve the content associated with the given timestamp
return $this->taskData[$timestamp] ?? null; // Return null if not found
}
public function getIndexedTasks() {
// Return the current indexed tasks (for debugging/inspection)
return $this->taskData;
}
}
// Example usage:
$indexer = new TaskIndex();
// Simulate adding tasks at different times
$indexer->addTask(time() - 30, "Task 1");
$indexer->addTask(time() - 10, "Task 2");
$indexer->addTask(time() - 5, "Task 3");
$indexer->addTask(time() - 15, "Task 4");
$indexer->addTask(time() - 20, "Task 5");
$indexer->addTask(time() - 25, "Task 6");
$indexer->addTask(time() - 35, "Task 7");
$indexer->addTask(time() - 40, "Task 8");
$indexer->addTask(time() - 45, "Task 9");
$indexer->addTask(time() - 50, "Task 10");
$indexer->addTask(time() - 55, "Task 11"); // This will replace Task 1
// Retrieve the content of a specific task
$content = $indexer->getTaskContent(time() - 10);
echo "Content of task at time " . (time() - 10) . ": " . ($content ?? "Not found") . "\n";
// Print the indexed tasks
print_r($indexer->getIndexedTasks());
?>
Add your comment