Skip to content

Commit

Permalink
add limits to bibcode cache growth (#4380)
Browse files Browse the repository at this point in the history
  • Loading branch information
GlazerMann authored Jan 25, 2024
1 parent be92398 commit 53e6a61
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
15 changes: 15 additions & 0 deletions apiFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function query_pmid_api (array $pmids, array &$templates) : bool { return entrez
function query_pmc_api (array $pmcs, array &$templates) : bool { return entrez_api($pmcs, $templates, 'pmc'); } // Pointer to save memory

final class AdsAbsControl {
const MAX_CACHE_SIZE = 50000;
private static int $big_counter = 0;
private static int $small_counter = 0;
/** @var array<string> $doi2bib **/
Expand Down Expand Up @@ -44,6 +45,7 @@ public static function small_back_on() : void {
}

public static function add_doi_map(string $bib, string $doi) : void {
self::check_memory_use();
if ($bib === '' || $doi === '') {
report_minor_error('Bad parameter in add_doi_map: ' . echoable($bib) . ' : ' . echoable($doi)); // @codeCoverageIgnore
return; // @codeCoverageIgnore
Expand All @@ -61,6 +63,19 @@ public static function get_doi2bib(string $doi) : string {
public static function get_bib2doi(string $bib) : string {
return (string) @self::$bib2doi[$bib];
}

public static function check_memory_use() : void {
$usage = count(self::$doi2bib) + count(self::$bib2doi);
if ($usage > self::MAX_CACHE_SIZE) {
self::free_memory();
}
}
public static function free_memory() : void {
self::$doi2bib = [];
self::$bib2doi = [];
gc_collect_cycles();
}

}

/**
Expand Down
1 change: 1 addition & 0 deletions big_jobs.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function big_jobs_name() : string { // NEVER save this string. Always use this f
/** @param resource $lock_file **/
function big_jobs_we_died($lock_file) : void {
HandleCache::free_memory();
AdsAbsControl::free_memory();
@fclose($lock_file);
hard_unlink(big_jobs_name());
}
Expand Down

0 comments on commit 53e6a61

Please sign in to comment.