11using BenchmarkDotNet . Attributes ;
22using BitFaster . Caching ;
3+ using BitFaster . Caching . Benchmarks . Lru ;
34using BitFaster . Caching . Lru ;
5+ using Microsoft . Extensions . Caching . Memory ;
46using System ;
57using System . Collections . Concurrent ;
68using System . Collections . Generic ;
79using System . Linq ;
8- using System . Runtime . Caching ;
910using System . Text ;
1011using System . Threading . Tasks ;
1112
@@ -23,60 +24,79 @@ public class LruJustGet
2324 private static readonly FastConcurrentTLru < int , int > fastConcurrentTLru = new FastConcurrentTLru < int , int > ( 8 , 9 , EqualityComparer < int > . Default , TimeSpan . FromMinutes ( 1 ) ) ;
2425
2526 private static readonly int key = 1 ;
26- private static MemoryCache memoryCache = MemoryCache . Default ;
27+ private static System . Runtime . Caching . MemoryCache memoryCache = System . Runtime . Caching . MemoryCache . Default ;
28+
29+ Microsoft . Extensions . Caching . Memory . MemoryCache exMemoryCache
30+ = new Microsoft . Extensions . Caching . Memory . MemoryCache ( new MemoryCacheOptionsAccessor ( ) ) ;
2731
2832 [ GlobalSetup ]
2933 public void GlobalSetup ( )
3034 {
31- memoryCache . Set ( key . ToString ( ) , "test" , new CacheItemPolicy ( ) ) ;
35+ memoryCache . Set ( key . ToString ( ) , "test" , new System . Runtime . Caching . CacheItemPolicy ( ) ) ;
36+ exMemoryCache . Set ( key , "test" ) ;
3237 }
3338
3439 [ Benchmark ( Baseline = true ) ]
35- public void ConcurrentDictionaryGetOrAdd ( )
40+ public void ConcurrentDictionary ( )
3641 {
3742 Func < int , int > func = x => x ;
3843 dictionary . GetOrAdd ( 1 , func ) ;
3944 }
4045
4146 [ Benchmark ( ) ]
42- public void FastConcurrentLruGetOrAdd ( )
47+ public void FastConcurrentLru ( )
4348 {
4449 Func < int , int > func = x => x ;
4550 fastConcurrentLru . GetOrAdd ( 1 , func ) ;
4651 }
4752
4853 [ Benchmark ( ) ]
49- public void ConcurrentLruGetOrAdd ( )
54+ public void ConcurrentLru ( )
5055 {
5156 Func < int , int > func = x => x ;
5257 concurrentLru . GetOrAdd ( 1 , func ) ;
5358 }
5459
5560 [ Benchmark ( ) ]
56- public void FastConcurrentTLruGetOrAdd ( )
61+ public void FastConcurrentTLru ( )
5762 {
5863 Func < int , int > func = x => x ;
5964 fastConcurrentTLru . GetOrAdd ( 1 , func ) ;
6065 }
6166
6267 [ Benchmark ( ) ]
63- public void ConcurrentTLruGetOrAdd ( )
68+ public void ConcurrentTLru ( )
6469 {
6570 Func < int , int > func = x => x ;
6671 concurrentTlru . GetOrAdd ( 1 , func ) ;
6772 }
6873
6974 [ Benchmark ( ) ]
70- public void ClassicLruGetOrAdd ( )
75+ public void ClassicLru ( )
7176 {
7277 Func < int , int > func = x => x ;
7378 classicLru . GetOrAdd ( 1 , func ) ;
7479 }
7580
7681 [ Benchmark ( ) ]
77- public void MemoryCacheGetStringKey ( )
82+ public void RuntimeMemoryCacheGet ( )
7883 {
7984 memoryCache . Get ( "1" ) ;
8085 }
86+
87+ [ Benchmark ( ) ]
88+ public void ExtensionsMemoryCacheGet ( )
89+ {
90+ exMemoryCache . Get ( 1 ) ;
91+ }
92+
93+ public class MemoryCacheOptionsAccessor
94+ : Microsoft . Extensions . Options . IOptions < MemoryCacheOptions >
95+ {
96+ private readonly MemoryCacheOptions options = new MemoryCacheOptions ( ) ;
97+
98+ public MemoryCacheOptions Value => this . options ;
99+
100+ }
81101 }
82102}
0 commit comments