@@ -10,7 +10,6 @@ open System.Diagnostics.Metrics
1010
1111[<Struct; RequireQualifiedAccess; NoComparison>]
1212type EvictionMethod =
13- | Blocking
1413 | Background
1514 | NoEviction
1615
@@ -28,7 +27,7 @@ type CacheOptions =
2827 MaximumCapacity = 1024
2928 PercentageToEvict = 5
3029 LevelOfConcurrency = Environment.ProcessorCount
31- EvictionMethod = EvictionMethod.Blocking
30+ EvictionMethod = EvictionMethod.Background
3231 }
3332
3433[<Sealed; NoComparison; NoEquality>]
@@ -219,9 +218,6 @@ type Cache<'Key, 'Value when 'Key: not null and 'Key: equality> internal (option
219218 false
220219
221220 member _.TryAdd ( key : 'Key , value : 'Value ) =
222- if options.EvictionMethod.IsBlocking then
223- tryEvictItems ()
224-
225221 let cachedEntity = pool.Acquire( key, value)
226222
227223 if store.TryAdd( key, cachedEntity) then
@@ -232,9 +228,6 @@ type Cache<'Key, 'Value when 'Key: not null and 'Key: equality> internal (option
232228 false
233229
234230 member _.AddOrUpdate ( key : 'Key , value : 'Value ) =
235- if options.EvictionMethod.IsBlocking then
236- tryEvictItems ()
237-
238231 let aquired = pool.Acquire( key, value)
239232
240233 let entity =
@@ -277,17 +270,6 @@ type Cache<'Key, 'Value when 'Key: not null and 'Key: equality> internal (option
277270
278271 override this.Finalize () : unit = this.Dispose()
279272
280- static member Create < 'Key , 'Value >( options : CacheOptions ) =
281- // Increase expected capacity by the percentage to evict, since we want to not resize the dictionary.
282- let capacity =
283- options.MaximumCapacity
284- + int ( float options.MaximumCapacity * float options.PercentageToEvict / 100.0 )
285-
286- let cts = new CancellationTokenSource()
287- let cache = new Cache< 'Key, 'Value>( options, capacity, cts)
288- CacheInstrumentation.AddInstrumentation cache |> ignore
289- cache
290-
291273 member this.GetStats () = CacheInstrumentation.GetStats( this)
292274
293275and CacheInstrumentation ( cache : ICacheEvents ) =
@@ -382,3 +364,31 @@ and CacheInstrumentation(cache: ICacheEvents) =
382364 static member RemoveInstrumentation ( cache : ICacheEvents ) =
383365 instrumentedCaches[ cache]. Dispose()
384366 instrumentedCaches.TryRemove( cache) |> ignore
367+
368+ module Cache =
369+
370+ // During testing a lot of compilations are started in app domains and subprocesses.
371+ // This is a reliable way to pass the override to all of them.
372+ [<Literal>]
373+ let private overrideVariable = " FSHARP_CACHE_OVERRIDE"
374+
375+ /// Use for testing purposes to reduce memory consumption in testhost and its subprocesses.
376+ let OverrideMaxCapacityForTesting () =
377+ Environment.SetEnvironmentVariable( overrideVariable, " true" , EnvironmentVariableTarget.Process)
378+
379+ let Create < 'Key , 'Value when 'Key : not null and 'Key : equality > ( options : CacheOptions ) =
380+
381+ let options =
382+ match Environment.GetEnvironmentVariable( overrideVariable) with
383+ | null -> options
384+ | _ -> { options with MaximumCapacity = 8 * 1024 }
385+
386+ // Increase expected capacity by the percentage to evict, since we want to not resize the dictionary.
387+ let capacity =
388+ options.MaximumCapacity
389+ + int ( float options.MaximumCapacity * float options.PercentageToEvict / 100.0 )
390+
391+ let cts = new CancellationTokenSource()
392+ let cache = new Cache< 'Key, 'Value>( options, capacity, cts)
393+ CacheInstrumentation.AddInstrumentation cache |> ignore
394+ cache
0 commit comments