add perfomance chapters

This commit is contained in:
lubiana 2022-04-06 23:43:03 +02:00 committed by Andre Lubian
parent 9a1f78947b
commit ececd7dcb5
101 changed files with 8014 additions and 62 deletions

View file

@ -49,7 +49,7 @@ class Page
$pages = array_map(function (MarkdownPage $page) {
return [
'id' => $page->id,
'title' => $page->content,
'title' => $page->title,
];
}, $this->repo->all());

View file

@ -1,32 +0,0 @@
<?php declare(strict_types=1);
namespace Lubian\NoFramework\Factory;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\ORM\Tools\Setup;
use Lubian\NoFramework\Settings;
final class DoctrineEm
{
public function __construct(private Settings $settings)
{
}
public function create(): EntityManagerInterface
{
$config = Setup::createConfiguration($this->settings->doctrine['devMode']);
$config->setMetadataDriverImpl(
new AttributeDriver(
$this->settings->doctrine['metadataDirs']
)
);
return EntityManager::create(
$this->settings->connection,
$config,
);
}
}

View file

@ -0,0 +1,38 @@
<?php declare(strict_types=1);
namespace Lubian\NoFramework\Middleware;
use Laminas\Diactoros\Response\Serializer;
use Lubian\NoFramework\Service\Cache\EasyCache;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use function assert;
use function base64_encode;
use function is_string;
final class Cache implements MiddlewareInterface
{
public function __construct(
private readonly EasyCache $cache,
private readonly Serializer $serializer,
) {
}
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if ($request->getMethod() !== 'GET') {
return $handler->handle($request);
}
$keyHash = base64_encode($request->getUri()->getPath());
$result = $this->cache->get(
$keyHash,
fn () => $this->serializer::toString($handler->handle($request)),
300
);
assert(is_string($result));
return $this->serializer::fromString($result);
}
}

View file

@ -0,0 +1,49 @@
<?php declare(strict_types=1);
namespace Lubian\NoFramework\Repository;
use Lubian\NoFramework\Model\MarkdownPage;
use Lubian\NoFramework\Service\Cache\EasyCache;
use function assert;
use function base64_encode;
use function is_array;
final class CachedMarkdownPageRepo implements MarkdownPageRepo
{
public function __construct(
private EasyCache $cache,
private MarkdownPageRepo $repo,
) {
}
/**
* @inheritDoc
*/
public function all(): array
{
$key = base64_encode(self::class . 'all');
$result = $this->cache->get(
$key,
fn () => $this->repo->all(),
300
);
assert(is_array($result));
foreach ($result as $page) {
assert($page instanceof MarkdownPage);
}
return $result;
}
public function byName(string $name): MarkdownPage
{
$key = base64_encode(self::class . 'byName' . $name);
$result = $this->cache->get(
$key,
fn () => $this->repo->byName($name),
300
);
assert($result instanceof MarkdownPage);
return $result;
}
}

View file

@ -0,0 +1,21 @@
<?php declare(strict_types=1);
namespace Lubian\NoFramework\Service\Cache;
use function apcu_add;
use function apcu_fetch;
final class ApcuCache implements EasyCache
{
public function get(string $key, callable $callback, int $ttl = 0): mixed
{
$success = false;
$result = apcu_fetch($key, $success);
if ($success === true) {
return $result;
}
$result = $callback();
apcu_add($key, $result, $ttl);
return $result;
}
}

View file

@ -0,0 +1,9 @@
<?php declare(strict_types=1);
namespace Lubian\NoFramework\Service\Cache;
interface EasyCache
{
/** @param callable(): mixed $callback */
public function get(string $key, callable $callback, int $ttl = 0): mixed;
}