infinityloop-dev/graphpinator-persisted-queries

模块,用于在缓存中持久化验证过的查询并提高重复查询的性能。

v1.3 2023-11-10 17:23 UTC

README

⚡🌐⚡ 模块,用于在缓存中持久化验证过的查询并提高重复查询的性能。

简介

此模块允许 GraPHPinator 在服务器上缓存查询以减少服务器负载。该模块旨在通过缓存和重用已知请求来减少 GraphQL 的解析和验证开销。

请注意,此模块不会影响您的解析函数的速度。

安装

使用 composer 安装包

composer require infinityloop-dev/graphpinator-persisted-queries

如何使用

  1. 实现 \Psr\SimpleCache\CacheInterface

您需要一个实现 \Psr\SimpleCache\CacheInterface 的类,其中存储了请求的序列化版本以供以后重用。

  1. PersistedQueriesModule 注册为 GraPHPinator 模块
$persistModule = new \Graphpinator\PersistedQueriesModule\PersistedQueriesModule($schema, $cacheImpl);
$graphpinator = new \Graphpinator\Graphpinator(
    $schema,
    $catchExceptions,
    new \Graphpinator\Module\ModuleSet([$persistModule, /* possibly other modules */]),
    $logger,
);
  1. 您已设置完毕,查询将自动缓存在指定的存储中。

性能改进

简单的基准测试(代码在 bench 目录中)显示 GraphQL 开销大约减少了 80%。

基准测试运行相同的查询 10k 次,之前有 5 次预热查询。使用的缓存是一个简单的本地 Redis 实现,连接是通过 php-redis 扩展完成的。Opcache 已禁用。