11import { LRUCache } from "npm:lru-cache@10.2.0" ;
2- import { ValueType } from "../../deps.ts" ;
3- import { logger } from "../../observability/otel/config.ts" ;
4- import { meter } from "../../observability/otel/metrics.ts" ;
52import {
63 assertCanBeCached ,
74 assertNoOptions ,
85 baseCache ,
96 createBaseCacheStorage ,
107} from "./utils.ts" ;
118
12- const lruEvictionCounter = meter . createCounter ( "lru_cache_eviction" , {
13- unit : "1" ,
14- valueType : ValueType . DOUBLE ,
15- } ) ;
16-
179// keep compatible with old variable name
1810const CACHE_MAX_SIZE = parseInt (
1911 Deno . env . get ( "CACHE_MAX_SIZE" ) ?? Deno . env . get ( "MAX_CACHE_SIZE" ) ??
@@ -38,61 +30,17 @@ const cacheOptions = (cache: Cache) => (
3830 maxSize : CACHE_MAX_SIZE ,
3931 ttlAutopurge : CACHE_TTL_AUTOPURGE ,
4032 ttlResolution : CACHE_TTL_RESOLUTION ,
41- dispose : async ( _value : boolean , key : string , reason : string ) => {
42- lruEvictionCounter . add ( 1 , { reason } ) ;
33+ dispose : async ( _value : boolean , key : string ) => {
4334 await cache . delete ( key ) ;
4435 } ,
4536 }
4637) ;
4738
48- const lruSizeGauge = meter . createObservableGauge ( "lru_cache_keys" , {
49- description : "number of keys in the LRU cache" ,
50- unit : "1" ,
51- valueType : ValueType . DOUBLE ,
52- } ) ;
53-
54- const lruBytesGauge = meter . createObservableGauge ( "lru_cache_bytes" , {
55- description : "total bytes tracked by the LRU cache" ,
56- unit : "bytes" ,
57- valueType : ValueType . DOUBLE ,
58- } ) ;
59-
60- // deno-lint-ignore no-explicit-any
61- const activeCaches = new Map < string , LRUCache < string , any > > ( ) ;
62-
63- lruSizeGauge . addCallback ( ( observer ) => {
64- for ( const [ name , lru ] of activeCaches ) {
65- observer . observe ( lru . size , { cache : name } ) ;
66- }
67- } ) ;
68-
69- // Warn when LRU disk usage exceeds this fraction of CACHE_MAX_SIZE.
70- // At this point the LRU is evicting aggressively and disk is nearly full.
71- const LRU_DISK_WARN_RATIO = parseFloat (
72- Deno . env . get ( "LRU_DISK_WARN_RATIO" ) ?? "0.9" ,
73- ) ;
74-
75- lruBytesGauge . addCallback ( ( observer ) => {
76- for ( const [ name , lru ] of activeCaches ) {
77- observer . observe ( lru . calculatedSize , { cache : name } ) ;
78- const ratio = lru . calculatedSize / CACHE_MAX_SIZE ;
79- if ( ratio >= LRU_DISK_WARN_RATIO ) {
80- logger . warn (
81- `lru_cache: disk usage for cache "${ name } " is at ` +
82- `${ Math . round ( lru . calculatedSize / 1024 / 1024 ) } MB / ` +
83- `${ Math . round ( CACHE_MAX_SIZE / 1024 / 1024 ) } MB (${ Math . round ( ratio * 100 ) } %). ` +
84- `LRU is evicting aggressively. Consider increasing CACHE_MAX_SIZE or reducing CACHE_MAX_AGE_S.` ,
85- ) ;
86- }
87- }
88- } ) ;
89-
9039function createLruCacheStorage ( cacheStorageInner : CacheStorage ) : CacheStorage {
9140 const caches = createBaseCacheStorage (
9241 cacheStorageInner ,
9342 ( _cacheName , cacheInner , requestURLSHA1 ) => {
9443 const fileCache = new LRUCache ( cacheOptions ( cacheInner ) ) ;
95- activeCaches . set ( _cacheName , fileCache ) ;
9644 return Promise . resolve ( {
9745 ...baseCache ,
9846 delete : async (
0 commit comments