@@ -47,41 +47,25 @@ public CacheService() : this(new CacheOptions())
4747 /// <inheritdoc />
4848 public void Run < TArg > ( FlowContext < TArg > context , NextPipelineService < TArg > next )
4949 {
50+ // Add trace for debugging
51+ context . Trace ? . CreateNewTracePoint ( nameof ( CacheService ) ) ;
52+
5053 // Create unique id for script to identify in cache store
51- var id = string . IsNullOrWhiteSpace ( context . Options ? . Id ) ?
54+ var id = string . IsNullOrWhiteSpace ( context . Options ? . Id ) ?
5255 GetScriptUniqueId ( context . Options ? . CacheOptions , context . Script ) : context . Options . Id ;
5356
54- // GetFlowContextOptionsId helps to identify script uniquely along with options
55- // in order to allow or deny functions
56- if ( context . Options != null )
57- {
58- id += "_" + GetFlowContextOptionsId ( context . Options ) ;
59- }
60-
61- context . Trace . Write ( $ "Cache-Key { id } ") ;
57+ context . Trace ? . Write ( $ "Cache-Key { id } ") ;
6258
63- // Get compiled script from cache
64- var compiledScript = _cache . Get < Action < FlowInput < TArg > , FlowOutput , ScriptHelperContext > > ( key : id ) ;
65- if ( compiledScript != null )
66- {
67- context . Trace . Write ( $ "Read from cache { id } - Succeeded") ;
68- context . Internals . CompiledScript = compiledScript ;
69- }
59+ // Get compiled script from cache and set it to context in order to avoid recompilation
60+ var isAvailableInCache = GetAndSetToContextTheCompiledScript ( context , id ) ;
7061
7162 // Invoke next service in pipeline
7263 next ? . Invoke ( context ) ;
7364
74- // Cache compiled script
75- if ( compiledScript == null && context . Internals . CompiledScript != null )
65+ // Cache the compiled script
66+ if ( ! isAvailableInCache && context . Internals . CompiledScript != null )
7667 {
77- _cache . Set ( key : id ,
78- value : context . Internals . CompiledScript ,
79- options : new MemoryCacheEntryOptions {
80- AbsoluteExpiration = context . Options ? . CacheOptions ? . AbsoluteExpiration ?? _cacheOptions . AbsoluteExpiration ,
81- SlidingExpiration = context . Options ? . CacheOptions ? . SlidingExpiration ?? _cacheOptions . SlidingExpiration
82- } ) ;
83-
84- context . Trace . Write ( $ "Saved into cache { id } - Succeeded") ;
68+ StoreIntoCacheCompiledScript ( context , id ) ;
8569 }
8670 }
8771
@@ -98,34 +82,44 @@ protected virtual string GetScriptUniqueId(CacheOptions contextCacheOptions, str
9882 return System . Convert . ToBase64String ( sha1 . ComputeHash ( Encoding . UTF8 . GetBytes ( script ) ) ) ;
9983 }
10084
101- private string GetFlowContextOptionsId ( IContextOptions options )
102- {
103- if (
104- //options.AllowArgumentToMutate == false &&
105- ( options . AllowFunctions == null || options . AllowFunctions . Length == 0 )
106- && ( options . DenyFunctions == null || options . DenyFunctions . Length == 0 )
107- )
108- {
109- return string . Empty ;
110- }
11185
112- StringBuilder sb = new StringBuilder ( ) ;
113- //sb.Append(string.Join(' ', options.AllowArgumentToMutate));
86+ private void StoreIntoCacheCompiledScript < TArg > ( FlowContext < TArg > context , string id )
87+ {
88+ _cache . Set ( key : id ,
89+ value : context . Internals . CompiledScript ,
90+ options : new MemoryCacheEntryOptions
91+ {
92+ AbsoluteExpiration = context . Options ? . CacheOptions ? . AbsoluteExpiration ?? _cacheOptions . AbsoluteExpiration ,
93+ SlidingExpiration = context . Options ? . CacheOptions ? . SlidingExpiration ?? _cacheOptions . SlidingExpiration
94+ } ) ;
95+
96+ context . Trace ? . Write ( $ "Saved into cache { id } - Succeeded") ;
97+ }
11498
115- if ( options . AllowFunctions != null && options . AllowFunctions . Length > 0 )
116- {
117- sb . Append ( "Allow" ) ; //ensure add this to avoid collisions
118- sb . Append ( string . Join ( ' ' , options . AllowFunctions ) ) ;
119- }
99+ private bool GetAndSetToContextTheCompiledScript < TArg > ( FlowContext < TArg > context , string id )
100+ {
101+ var compiledScript = _cache . Get < Action < FlowInput < TArg > , FlowOutput , ScriptHelperContext > > ( key : id ) ;
102+ var isAvailableInCache = compiledScript != null ;
120103
121- if ( options . DenyFunctions != null && options . DenyFunctions . Length > 0 )
104+ if ( isAvailableInCache )
122105 {
123- sb . Append ( "Deny" ) ; //ensure add this to avoid collisions
124- sb . Append ( string . Join ( ' ' , options . DenyFunctions ) ) ;
106+ if ( context . Options ? . ResetCache ?? false )
107+ {
108+ _cache . Remove ( key : id ) ;
109+ isAvailableInCache = false ; // in order to save it back
110+
111+ context . Trace ? . Write ( $ "Reset cache entry '{ id } ' - Succeeded") ;
112+ }
113+ else
114+ {
115+ context . Trace ? . Write ( $ "Read from cache { id } - Succeeded") ;
116+ context . Internals . CompiledScript = compiledScript ;
117+ }
125118 }
126119
127- return GetScriptUniqueId ( options . CacheOptions , sb . ToString ( ) ) ;
120+ return isAvailableInCache ;
128121 }
122+
129123 }
130124
131125}
0 commit comments