Skip to content

Commit

Permalink
Cache cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-boudry committed Nov 28, 2024
1 parent 32ba917 commit 1959023
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
13 changes: 4 additions & 9 deletions src/Statements/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ abstract class Statement implements IteratorAggregate
{
use LinkedDataFrame;
protected GroupByIterator|CacheStatus $cache = CacheStatus::UNUSED;
public CacheStatus $cacheStatus {
get => $this->cache instanceof CacheStatus ? $this->cache : CacheStatus::SET;
}

protected array $where = [];
protected ?int $limit = null;
protected int $offset = 0;
Expand All @@ -30,15 +34,6 @@ public function __construct(DataFrame $df)
$this->setLinkedDataFrame($df);
}

public function getCacheStatus(): CacheStatus
{
if ($this->cache instanceof CacheStatus) {
return $this->cache;
}

return CacheStatus::SET;
}

protected function invalidateCache(): void
{
$this->cache = CacheStatus::UNUSED;
Expand Down
22 changes: 22 additions & 0 deletions tests/Unit/StatementUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use MammothPHP\WoollyM\DataFrame;
use MammothPHP\WoollyM\Statements\CacheStatus;

beforeEach(function (): void {
$this->df = DataFrame::fromArray([
Expand All @@ -16,3 +17,24 @@
expect(\count($this->df->select()))->toBe(3);
expect($this->df->select())->toHaveCount(3);
});

it('has a cache status', function(): void {
$stmt = $this->df->select('colA')->whereColumn('colA', equal: 1)->groupBy('colA');

expect($stmt->cacheStatus)->toBe(CacheStatus::UNUSED);

$stmt->export();
expect($stmt->cacheStatus)->toBe(CacheStatus::SET);
expect($stmt)->toHaveCount(1);

$stmt->resetWhere();
expect($stmt->cacheStatus)->toBe(CacheStatus::UNUSED);
$stmt->whereKeyBetween(0,2);

expect($stmt->cacheStatus)->toBe(CacheStatus::UNUSED);

expect($stmt)->toHaveCount(3);
expect($stmt->cacheStatus)->toBe(CacheStatus::SET);

expect(fn() => $stmt->cacheStatus = CacheStatus::SET)->toThrow('$cacheStatus is read-only');
});

0 comments on commit 1959023

Please sign in to comment.