20 private int $individualLookupsInRequest = 0;
21 private const int MAX_INDIVIDUAL_LOOKUPS_PER_REQ = 50;
29 private readonly LoggerInterface $logger
46 private function computeIsAlwaysKnownUnbatchedParts(
LinkTarget $link ): bool {
49 if ( !$this->cache->has( $cacheKey ) || $this->cache->get( $cacheKey ) !==
null ) {
50 throw new LogicException(
51 __METHOD__ .
' can be only called when cached value is null'
56 $this->hookRunner->onTitleIsAlwaysKnown(
57 $this->titleFactory->newFromLinkTarget( $link ),
61 if ( $isKnown ===
null ) {
67 } elseif ( $this->shadowPageLoader->existsForLink( $link ) ) {
85 if ( $isKnown ===
null ) {
86 throw new LogicException(
87 __METHOD__ .
' should have the final call; $isKnown === null should not be possible'
91 $this->cache->set( $cacheKey, $isKnown );
107 private function computeIsAlwaysKnownBatch( array $links ): void {
108 $isKnownArr = array_fill_keys( array_keys( $links ), null );
109 $this->hookRunner->onLinkTargetIsAlwaysKnownBatch( $links, $isKnownArr );
111 foreach ( $links as $i => $link ) {
112 $isKnown = $isKnownArr[$i] ??
null;
127 public function preload( array $links ): void {
129 foreach ( $links as $link ) {
130 if ( !$this->cache->has( CacheKeyHelper::getKeyForPage( $link ) ) ) {
131 $uncachedLinks[] = $link;
140 if ( $uncachedLinks ) {
141 $this->computeIsAlwaysKnownBatch( $uncachedLinks );
147 if ( !$this->cache->has( $key ) ) {
149 $this->computeIsAlwaysKnownBatch( [ $page ] );
151 if ( ++$this->individualLookupsInRequest >= self::MAX_INDIVIDUAL_LOOKUPS_PER_REQ ) {
152 $this->logger->warning(
153 __METHOD__ .
' was used more than {limit} times (value: {value}), use batching',
155 'limit' => self::MAX_INDIVIDUAL_LOOKUPS_PER_REQ,
156 'value' => $this->individualLookupsInRequest,
157 'exception' =>
new RuntimeException,
163 $cachedValue = $this->cache->get( $key );
164 if ( $cachedValue !==
null ) {
165 return (
bool)$cachedValue;
170 return $this->computeIsAlwaysKnownUnbatchedParts( $page );
__construct(private readonly HookRunner $hookRunner, private readonly TitleFactory $titleFactory, private readonly ShadowPageLoader $shadowPageLoader, private readonly RepoGroup $repoGroup, private readonly SpecialPageFactory $specialPageFactory, private readonly LoggerInterface $logger)