1+ using System ;
2+ using EasyCaching . Core ;
3+ using EasyCaching . Core . Configurations ;
4+ using EasyCaching . FasterKv ;
5+ using EasyCaching . FasterKv . Configurations ;
6+ using Microsoft . Extensions . Configuration ;
7+ // ReSharper disable CheckNamespace
8+
9+ namespace Microsoft . Extensions . DependencyInjection ;
10+
11+ public static class FasterKvCachingOptionsExtensions
12+ {
13+ /// <summary>
14+ /// Uses the FasterKv provider (specify the config via hard code).
15+ /// </summary>
16+ /// <param name="options">Options.</param>
17+ /// <param name="configure">Configure provider settings.</param>
18+ /// <param name="name">The name of this provider instance.</param>
19+ public static EasyCachingOptions UseFasterKv (
20+ this EasyCachingOptions options ,
21+ Action < FasterKvCachingOptions > configure ,
22+ string name = EasyCachingConstValue . DefaultFasterKvName
23+ )
24+ {
25+ ArgumentCheck . NotNull ( configure , nameof ( configure ) ) ;
26+
27+ options . RegisterExtension ( new FasterKvOptionsExtension ( name , configure ) ) ;
28+ return options ;
29+ }
30+
31+ /// <summary>
32+ /// Uses the FasterKv provider (read config from configuration file).
33+ /// </summary>
34+ /// <param name="options">Options.</param>
35+ /// <param name="configuration">The configuration.</param>
36+ /// <param name="name">The name of this provider instance.</param>
37+ /// <param name="sectionName">The section name in the configuration file.</param>
38+ public static EasyCachingOptions UseFasterKv (
39+ this EasyCachingOptions options ,
40+ IConfiguration configuration ,
41+ string name = EasyCachingConstValue . DefaultFasterKvName ,
42+ string sectionName = EasyCachingConstValue . FasterKvSection
43+ )
44+ {
45+ var dbConfig = configuration . GetSection ( sectionName ) ;
46+ var fasterKvOptions = new FasterKvCachingOptions ( ) ;
47+ dbConfig . Bind ( fasterKvOptions ) ;
48+
49+ void Configure ( FasterKvCachingOptions x )
50+ {
51+ x . EnableLogging = fasterKvOptions . EnableLogging ;
52+ x . MaxRdSecond = fasterKvOptions . MaxRdSecond ;
53+ x . LockMs = fasterKvOptions . LockMs ;
54+ x . SleepMs = fasterKvOptions . SleepMs ;
55+ x . SerializerName = fasterKvOptions . SerializerName ;
56+ x . CacheNulls = fasterKvOptions . CacheNulls ;
57+ x . IndexCount = fasterKvOptions . IndexCount ;
58+ x . MemorySizeBit = fasterKvOptions . MemorySizeBit ;
59+ x . PageSizeBit = fasterKvOptions . PageSizeBit ;
60+ x . ReadCacheMemorySizeBit = fasterKvOptions . ReadCacheMemorySizeBit ;
61+ x . ReadCachePageSizeBit = fasterKvOptions . ReadCachePageSizeBit ;
62+ x . LogPath = fasterKvOptions . LogPath ;
63+ }
64+
65+ options . RegisterExtension ( new FasterKvOptionsExtension ( name , Configure ) ) ;
66+ return options ;
67+ }
68+ }
0 commit comments