44using System ;
55using System . Text ;
66using System . Security . Cryptography ;
7+
8+ #if NET48
9+ using System . Runtime . Caching ;
10+ #else
711using Microsoft . Extensions . Caching . Memory ;
12+ #endif
13+
814
915namespace Simpleflow . Services
1016{
@@ -14,7 +20,12 @@ namespace Simpleflow.Services
1420 /// </summary>
1521 public class CacheService : IFlowPipelineService
1622 {
23+ #if NET48
24+ private readonly MemoryCache _cache ;
25+ #else
1726 private readonly IMemoryCache _cache ;
27+ #endif
28+
1829 private readonly CacheOptions _cacheOptions ;
1930
2031 /// <summary>
@@ -32,8 +43,13 @@ public CacheService(CacheOptions cacheOptions)
3243 throw new ArgumentNullException ( nameof ( cacheOptions . HashingAlgToIdentifyScriptUniquely ) ) ;
3344 }
3445
46+
47+ #if NET48
3548 //MemoryCache
49+ _cache = MemoryCache . Default ;
50+ #else
3651 _cache = new MemoryCache ( new MemoryCacheOptions ( ) { } ) ;
52+ #endif
3753
3854 }
3955
@@ -87,20 +103,34 @@ protected virtual string GetScriptUniqueId(CacheOptions contextCacheOptions, str
87103
88104 private void StoreIntoCacheCompiledScript < TArg > ( FlowContext < TArg > context , string id )
89105 {
90- _cache . Set ( key : id ,
91- value : context . Internals . CompiledScript ,
92- options : new MemoryCacheEntryOptions
93- {
94- AbsoluteExpiration = context . Options ? . CacheOptions ? . AbsoluteExpiration ?? _cacheOptions . AbsoluteExpiration ,
95- SlidingExpiration = context . Options ? . CacheOptions ? . SlidingExpiration ?? _cacheOptions . SlidingExpiration
96- } ) ;
97106
107+ #if NET48
108+ _cache . Set ( key : id ,
109+ value : context . Internals . CompiledScript ,
110+ policy : new CacheItemPolicy
111+ {
112+ AbsoluteExpiration = context . Options ? . CacheOptions ? . AbsoluteExpiration ?? _cacheOptions . AbsoluteExpiration ?? DateTimeOffset . MaxValue ,
113+ SlidingExpiration = context . Options ? . CacheOptions ? . SlidingExpiration ?? _cacheOptions . SlidingExpiration ?? CacheOptions . DefaultSlidingExpiration
114+ } ) ;
115+ #else
116+ _cache . Set ( key : id ,
117+ value : context . Internals . CompiledScript ,
118+ options : new MemoryCacheEntryOptions
119+ {
120+ AbsoluteExpiration = context . Options ? . CacheOptions ? . AbsoluteExpiration ?? _cacheOptions . AbsoluteExpiration ,
121+ SlidingExpiration = context . Options ? . CacheOptions ? . SlidingExpiration ?? _cacheOptions . SlidingExpiration
122+ } ) ;
123+ #endif
98124 context . Trace ? . Write ( $ "Saved into cache { id } - Succeeded") ;
99125 }
100126
101127 private bool GetAndSetToContextTheCompiledScript < TArg > ( FlowContext < TArg > context , string id )
102128 {
129+ #if NET48
130+ var compiledScript = _cache . Get ( key : id ) as Action < TArg , FlowOutput , RuntimeContext > ;
131+ #else
103132 var compiledScript = _cache . Get < Action < TArg , FlowOutput , RuntimeContext > > ( key : id ) ;
133+ #endif
104134 var isAvailableInCache = compiledScript != null ;
105135
106136 if ( isAvailableInCache )
0 commit comments