Laravel Lighthouse查询不使用redis缓存

6ju8rftf  于 2023-08-02  发布在  Redis
关注(0)|答案(1)|浏览(118)

我有一个非常慢的GraphQL请求(1700条记录需要10秒),但是我可以用10分钟的TTL轻松缓存。这是:

query GetMapAssets($first: Int, $page: Int) {
        assets(first: $first, page: $page, where: {
            AND: [
                {
                    column: "mag_id"
                    operator: IS_NOT_NULL
                }
                {
                    column: "last_mag_data_id"
                    operator: IS_NOT_NULL
                }
            ]
        }) {
            paginatorInfo {
                count
                currentPage
                lastPage
                total
            }
            data {
                id
                reference
                lastPosition {
                    latitude
                    longitude
                }
                asset_type {
                    id
                    name
                }
                state {
                    id
                }
                branches {
                    data {
                        id
                    }
                }
                sites {
                    data {
                        site_type {
                            id
                            name
                        }
                    }
                }
                mag {
                    mag_id
                }
                asset_group {
                    id
                }
                date_enter_site
            }
        }
    }

字符串
根据文档,我在我的Query上添加了一个@cache指令:

extend type Query @guard(with: ["passport", "web"]) {
    assets(
        orderBy: _ @orderBy(columns: [
            "id",
            "reference",
            "buy_value",
            "asset_type_name",
            "asset_mag_id",
            "asset_group_reference",
            "asset_state_name"
        ])
        where: _ @whereConditions
    ): [Asset]
        @paginate(defaultCount: 10, scopes: [
            "filterBranch",
            "assetTypeName",
            "assetMagId",
            "assetGroupReference",
            "assetStateName"
        ])
        @can(ability: "view", model: "App\\Models\\Asset")
        @cache (private: true, maxAge: 600)
    asset(reference: String @where(key: "reference"), id: ID @where(key: "id")): Asset
        @first
        @can(ability: "view", model: "App\\Models\\Asset")
}

type Asset {
    id: ID!
    mag: Mag @belongsTo
    company: Company @belongsTo
    branches(
        orderBy: _ @orderBy(columns: ["id", "name"])
        where: _ @whereConditions
    ): [Branch] @belongsToMany(type: PAGINATOR)
    asset_type: AssetType @belongsTo
    state: AssetState @belongsTo
    reference: String!
    buy_value: Float
    last_mag_data_id: Int
    asset_group: AssetGroup @belongsTo
    responsible(
        orderBy: _ @orderBy(columns: ["id", "name"])
        where: _ @whereConditions
    ): [User] @belongsToMany(relation: "users", type: PAGINATOR)
    shipping_orders: [ShippingOrder] @hasMany(scopes:["tracked"])
    responsibles(
        orderBy: _ @orderBy(columns: ["id", "name"])
        where: _ @whereConditions
    ): [User] @hasMany(relation: "users", type: PAGINATOR)
    mag_datas(
        orderBy: _ @orderBy(columns: ["id", "device_timestamp"])
        where: _ @whereConditions
    ): [MagData] @hasMany(type: PAGINATOR)
    latestMagData: MagData @hasOne
    custom_parameters(
        orderBy: _ @orderBy(columns: ["id", "name"])
        where: _ @whereConditions
    ): [CustomParameter] @morphMany(relation: "custom_parameters", type: PAGINATOR)
    lastTemperature: MagData
    lastPosition: MagData @hasOne
    current_shipping_order: [ShippingOrder] @hasMany(relation: "shipping_orders", scopes: ["tracked"])
    sites(
        orderBy: _ @orderBy(columns: ["id", "name"])
        where: _ @whereConditions
    ): [Site] @belongsToMany(type: PAGINATOR) @can(ability: "view", model: "App\\Models\\Site")
    previous_sites(
        orderBy: _ @orderBy(columns: ["id", "name"])
        where: _ @whereConditions
    ): [Site] @belongsToMany(type: PAGINATOR) @can(ability: "view", model: "App\\Models\\Site")
    date_enter_site: DateTime
}


我的缓存驱动程序是Redis(在所有环境下),我的lighthouse缓存配置看起来像这样:

'cache' => [
        'enable' => env('LIGHTHOUSE_CACHE_ENABLE', 'local' !== env('APP_ENV')),
        'key' => env('LIGHTHOUSE_CACHE_KEY', 'lighthouse-schema'),
        'store' => env('LIGHTHOUSE_CACHE_STORE', null),
        'ttl' => env('LIGHTHOUSE_CACHE_TTL', null),
    ],


无论我尝试什么,我仍然有大约10秒的响应时间,这让我认为我只是没有命中该高速缓存。
我的Redis配置正确,因为我的会话在它上面工作,并且在我的本地环境中也启用了。
编辑:以下是运行查询后我的redis的内容:
我的查询的键看起来与其他查询的键不同,但包含执行get <key>时的结果

1) "company_cache:lighthouse:query:5463d14c79c39a60ba59927e444e37095660a3cc28cf14bf4cc97c2ecb263c34"
2) "company_cache:lighthouse-schema"
3) "company_cache:lighthouse:auth:1:Query::assets:first:2000:page:0:where:{\"operator\":\"=\",\"AND\":[{\"column\":\"mag_id\",\"operator\":\"NotNull\"},{\"column\":\"last_mag_data_id\",\"operator\":\"NotNull\"}]}"

w51jfk4q

w51jfk4q1#

您需要调试该高速缓存在缓存键和指令级别不工作的原因:
该高速缓存键是否考虑了所有的查询参数,包括分页参数,比如$first和$page?
如果这是一个开发系统,使用redis-cli并运行keys,或者在prod上使用scan。直观地看到存储的内容将有助于了解这一点。
@cache是否正确应用于资产?什么是TTL?
打开Lighthouse调试模式并记录该高速缓存使用情况。在lighthouse.php配置文件中,设置'debug' => true
您使用的Lighthouse版本是否支持@cache指令?

相关问题