Skip to content

Commit

Permalink
Refactor ObjectArray
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Mar 31, 2024
1 parent e2b2c56 commit 9a5641d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 14 deletions.
53 changes: 41 additions & 12 deletions src/Internal/Containers/ObjectContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
use Revolt\EventLoop;
use Traversable;

use function Amp\async;

/**
* @template TKey as array-key
* @template TValue as DbObject
Expand Down Expand Up @@ -87,25 +89,35 @@ public function get(string|int $index): mixed
{
if (isset($this->cache[$index])) {
$obj = $this->cache[$index];
$ref = $obj->reference->get();
$ref = $obj->get();
if ($ref !== null) {
$obj->ttl = \time() + $this->cacheTtl;
return $ref;
}
// @codeCoverageIgnoreStart
unset($this->cache[$index]);
// @codeCoverageIgnoreEnd
}

$result = $this->inner->get($index);
if (isset($this->cache[$index])) {
return $this->cache[$index]->reference->get();
return $this->cache[$index]->get();
}
if ($result === null) {
return null;
}

$result->initDb($this, $index, $this->config);

$this->cache[$index] = new ObjectReference($result, \time() + $this->cacheTtl);
$this->cache[$index] = new ObjectReference(
$result,
\time() + $this->cacheTtl,
$f = async(
$result->initDb(...),
$this,
$index,
$this->config
)
);
$f->await();

return $result;
}
Expand All @@ -116,11 +128,19 @@ public function get(string|int $index): mixed
*/
public function set(string|int $key, DbObject $value): void
{
if (isset($this->cache[$key]) && $this->cache[$key]->reference->get() === $value) {
if (isset($this->cache[$key]) && $this->cache[$key]->get() === $value) {
return;
}
$value->initDb($this, $key, $this->config);
$this->cache[$key] = new ObjectReference($value, \time() + $this->cacheTtl);
$value->initDb(
$this,
$key,
$this->config
);
$this->cache[$key] = new ObjectReference(
$value,
\time() + $this->cacheTtl,
null
);
$value->save();
}

Expand All @@ -137,15 +157,24 @@ public function getIterator(): Traversable
foreach ($this->inner->getIterator() as $key => $value) {
if (isset($this->cache[$key])) {
$obj = $this->cache[$key];
$ref = $obj->reference->get();
$ref = $obj->get();
if ($ref !== null) {
$obj->ttl = \time() + $this->cacheTtl;
yield $key => $ref;
continue;
}
}
$value->initDb($this, $key, $this->config);
$this->cache[$key] = new ObjectReference($value, \time() + $this->cacheTtl);
$this->cache[$key] = new ObjectReference(
$value,
\time() + $this->cacheTtl,
$f = async(
$value->initDb(...),
$this,
$key,
$this->config
)
);
$f->await();
yield $key => $value;
}
}
Expand Down Expand Up @@ -177,7 +206,7 @@ public function flushCache(): void
if ($value->ttl <= $now) {
$value->obj = null;
}
if ($value->reference->get() !== null) {
if ($value->get() !== null) {
$new[$key] = $value;
}
}
Expand Down
20 changes: 18 additions & 2 deletions src/Internal/Containers/ObjectReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace danog\AsyncOrm\Internal\Containers;

use Amp\Future;
use danog\AsyncOrm\DbObject;
use WeakReference;

Expand All @@ -29,14 +30,29 @@
final class ObjectReference
{
/** @var WeakReference<TObject> */
public readonly WeakReference $reference;
private readonly WeakReference $reference;
public ?DbObject $obj;
/** @param TObject $object */
public function __construct(
DbObject $object,
public int $ttl
public int $ttl,
private ?Future $initFuture
) {
$this->obj = $object;
$this->reference = WeakReference::create($object);
}

/** @return ?TObject */
public function get(): ?DbObject
{
$ref = $this->reference->get();
if ($ref === null) {
return $ref;
}
if ($this->initFuture !== null) {
$this->initFuture->await();
$this->initFuture = null;
}
return $ref;
}
}

0 comments on commit 9a5641d

Please sign in to comment.