update readme

This commit is contained in:
lubiana 2022-04-05 00:02:52 +02:00
parent f054af35c3
commit fed49011bd
Signed by: lubiana
SSH key fingerprint: SHA256:vW1EA0fRR3Fw+dD/sM0K+x3Il2gSry6YRYHqOeQwrfk
88 changed files with 7546 additions and 176 deletions

View file

@ -3,14 +3,16 @@
namespace Lubian\NoFramework\Repository;
use Lubian\NoFramework\Model\MarkdownPage;
use Lubian\NoFramework\Settings;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
class CachedMarkdownPageRepo implements MarkdownPageRepo
{
public function __construct(
private CacheInterface $cache,
private MarkdownPageRepo $repo,
private readonly CacheInterface $cache,
private readonly MarkdownPageRepo $repo,
private readonly Settings $settings,
) {
}
@ -20,6 +22,9 @@ class CachedMarkdownPageRepo implements MarkdownPageRepo
public function all(): array
{
$callback = fn () => $this->repo->all();
if ($this->settings->isDev()) {
return $callback();
}
return $this->cache->get('ALLPAGES', function (ItemInterface $item) use ($callback) {
$item->expiresAfter(30);
return $callback();
@ -29,6 +34,9 @@ class CachedMarkdownPageRepo implements MarkdownPageRepo
public function byId(int $id): MarkdownPage
{
$callback = fn () => $this->repo->byId($id);
if ($this->settings->isDev()) {
return $callback();
}
return $this->cache->get('PAGE' . $id, function (ItemInterface $item) use ($callback) {
$item->expiresAfter(30);
return $callback();
@ -38,9 +46,17 @@ class CachedMarkdownPageRepo implements MarkdownPageRepo
public function byTitle(string $title): MarkdownPage
{
$callback = fn () => $this->repo->byTitle($title);
if ($this->settings->isDev()) {
return $callback();
}
return $this->cache->get('PAGE' . $title, function (ItemInterface $item) use ($callback) {
$item->expiresAfter(30);
return $callback();
});
}
public function save(MarkdownPage $page): MarkdownPage
{
return $this->repo->save($page);
}
}

View file

@ -0,0 +1,60 @@
<?php
declare(strict_types=1);
namespace Lubian\NoFramework\Repository;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Lubian\NoFramework\Exception\NotFound;
use Lubian\NoFramework\Model\MarkdownPage;
final class DoctrineMarkdownPageRepo implements MarkdownPageRepo
{
/**
* @var EntityRepository<MarkdownPage>
*/
private EntityRepository $repo;
public function __construct(
private EntityManagerInterface $entityManager
){
$this->repo = $this->entityManager->getRepository(MarkdownPage::class);
}
/**
* @inheritDoc
*/
public function all(): array
{
usleep(rand(500, 1500) * 1000);
return $this->repo->findAll();
}
public function byId(int $id): MarkdownPage
{
usleep(rand(500, 1500) * 1000);
$page = $this->repo->findOneBy(['id' => $id]);
if (!$page instanceof MarkdownPage){
throw new NotFound;
}
return $page;
}
public function byTitle(string $title): MarkdownPage
{
usleep(rand(500, 1500) * 1000);
$page = $this->repo->findOneBy(['title' => $title]);
if (!$page instanceof MarkdownPage){
throw new NotFound;
}
return $page;
}
public function save(MarkdownPage $page): MarkdownPage
{
$this->entityManager->persist($page);
$this->entityManager->flush();
return $page;
}
}

View file

@ -31,7 +31,7 @@ final class MarkdownPageFilesystem implements MarkdownPageRepo
$fileNames = glob($this->dataPath . '*.md');
assert(is_array($fileNames));
return array_map(function (string $name): MarkdownPage {
usleep(100000);
usleep(rand(200, 500) * 1000);
$content = file_get_contents($name);
$name = str_replace($this->dataPath, '', $name);
$name = str_replace('.md', '', $name);
@ -60,4 +60,9 @@ final class MarkdownPageFilesystem implements MarkdownPageRepo
}
return $filtered[0];
}
public function save(MarkdownPage $page): MarkdownPage
{
return $page;
}
}

View file

@ -14,4 +14,6 @@ interface MarkdownPageRepo
public function byId(int $id): MarkdownPage;
public function byTitle(string $title): MarkdownPage;
public function save(MarkdownPage $page): MarkdownPage;
}