gpt4 book ai didi

Singletone in next js on server side(服务器端next js中的Singletone)

转载 作者:bug小助手 更新时间:2023-10-22 13:15:28 33 4
gpt4 key购买 nike



I tried to use @singleton() on server side in next.js and it class run some action by setTimemout.
But this is class dont created as singletone and created every container.resolve() and as result i have many requests.

我试着在next.js的服务器端使用@singleton(),它类通过setTimemout运行一些操作。但这是一个类,并没有创建为singletone,而是创建了每个容器.resolve(),因此我有很多请求。


@singleton() export class PriceProvider {   public constructor() {
setInterval(() => void this.load(), INTERVAL);
void this.load(); } }

Also I tried to save in appGlobalThis

我还试图保存在appGlobalThis中


const appGlobalThis = globalThis as unknown as {   priceProviderStarted: boolean; };

export class PriceProvider {
public constructor(
private readonly config: Config,
private readonly raceRepository: RaceRepository,
) {
if (!appGlobalThis.priceProviderStarted) {
appGlobalThis.priceProviderStarted = true;
void this.step();
}
}

But priceProvider run step every time

但是priceProvider每次都运行步骤


更多回答

Does this answer your question? How to prevent Next.js from instantiating a singleton class/object multiple times?

这能回答你的问题吗?如何防止Next.js多次实例化单例类/对象?

@singletone not working in dev?

@singletone不在dev中工作?

优秀答案推荐
更多回答

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com