Skip to content

Commit

Permalink
Optimize memory management to prevent leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
javier-villatoro committed Aug 8, 2024
1 parent 2cc4414 commit d69412a
Showing 1 changed file with 54 additions and 16 deletions.
70 changes: 54 additions & 16 deletions src/Http/Middleware/LeverRateStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,73 @@ public function __construct()

public function get(): array
{
$data = Cache::get($this->cacheKey, []);
$data = [];
$part = 1;

// Check the size of the data in the cache
if ($this->getCacheSize($data) > $this->maxCacheSize) {
// If the data is too large, clear the cache
Cache::forget($this->cacheKey);

return [];
// Retrieve and merge all parts
while (Cache::has($this->getCacheKey($part))) { // Check if the part exists
$partData = Cache::get($this->getCacheKey($part), []); // Retrieve the part data
$data = array_merge($data, $partData); // Merge the part data with the main data
$part++; // Move to the next part
}

return $data;
}

public function push(int $timestamp, int $limit)
{
$data = array_merge($this->get(), [$timestamp]);

// Check the size of the data before saving it
if ($this->getCacheSize($data) <= $this->maxCacheSize) {
Cache::put($this->cacheKey, $data, $this->cacheTtl);
} else {
// If the data is too large, clear the cache and save only the new data
Cache::forget($this->cacheKey);
Cache::put($this->cacheKey, [$timestamp], $this->cacheTtl);
// Retrieve current data
$data = $this->get();
$data[] = $timestamp;

// Split data into parts and store each part separately
$parts = $this->splitDataIntoParts($data);

// Store each part in the cache with separate keys
foreach ($parts as $index => $partData) {
Cache::put($this->getCacheKey($index + 1), $partData, $this->cacheTtl);
}

// Remove any leftover parts that may have been left from previous larger dataset
$this->cleanupExtraParts(count($parts) + 1);
}

private function splitDataIntoParts(array $data): array
{
$parts = [];
$currentPart = [];

foreach ($data as $item) {
$currentPart[] = $item;
if ($this->getCacheSize($currentPart) > $this->maxCacheSize) {
array_pop($currentPart); // Remove the last item that caused the size to exceed
$parts[] = $currentPart; // Store the current part in the parts array
$currentPart = [$item]; // Start a new part with the last item that was removed
}
}

if (! empty($currentPart)) {
$parts[] = $currentPart; // Store the current part in the parts array
}

return $parts;
}

private function getCacheKey(int $part): string
{
return $this->cacheKey.':'.$part;
}

private function getCacheSize(array $data): int
{
return strlen(serialize($data));
}

private function cleanupExtraParts(int $startPart)
{
while (Cache::has($this->getCacheKey($startPart))) {
Cache::forget($this->getCacheKey($startPart));
$startPart++;
}
}
}

0 comments on commit d69412a

Please sign in to comment.