Skip to content

Commit 27016a1

Browse files
authored
Merge pull request #32 from navtech-io/develop
Remove Microsoft.Extensions.Caching.Memory dependency from target fra…
2 parents 3ad3ec8 + 30d644c commit 27016a1

File tree

10 files changed

+78
-36
lines changed

10 files changed

+78
-36
lines changed

src/Simpleflow/CacheOptions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ namespace Simpleflow
88
/// </summary>
99
public class CacheOptions
1010
{
11+
internal static readonly System.TimeSpan DefaultSlidingExpiration = System.TimeSpan.FromMinutes(3);
1112
/// <summary>
1213
/// Gets or sets how long a cache entry can be inactive (e.g. not accessed) before it will be removed.
1314
/// This will not extend the entry lifetime beyond the absolute expiration (if set).
1415
/// </summary>
15-
public System.TimeSpan? SlidingExpiration { get; set; } = System.TimeSpan.FromMinutes(3);
16+
public System.TimeSpan? SlidingExpiration { get; set; } = DefaultSlidingExpiration;
1617

1718
/// <summary>
1819
/// Gets or sets an absolute expiration date for the cache entry.

src/Simpleflow/Services/CacheService.cs

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44
using System;
55
using System.Text;
66
using System.Security.Cryptography;
7+
8+
#if NET48
9+
using System.Runtime.Caching;
10+
#else
711
using Microsoft.Extensions.Caching.Memory;
12+
#endif
13+
814

915
namespace 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)

src/Simpleflow/Simpleflow.csproj

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,24 @@
33
<TargetFrameworks>net48;netcoreapp3.1;net6.0;</TargetFrameworks>
44
<PackageIcon>PackageIcon.png</PackageIcon>
55
<VersionPrefix>1.0.11</VersionPrefix>
6-
<VersionSuffix>beta2</VersionSuffix>
6+
<VersionSuffix>beta3</VersionSuffix>
77
<PackageReadmeFile>README.md</PackageReadmeFile>
88
</PropertyGroup>
99

1010
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
1111
<DocumentationFile>Simpleflow.xml</DocumentationFile>
1212
</PropertyGroup>
1313

14+
<ItemGroup Condition=" '$(TargetFramework)' == 'net48' ">
15+
<PackageReference Include="System.Runtime.Caching" Version="4.7.0" />
16+
</ItemGroup>
17+
18+
<ItemGroup Condition=" '$(TargetFramework)' != 'net48' ">
19+
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="6.0.1" />
20+
</ItemGroup>
21+
1422
<ItemGroup>
1523
<PackageReference Include="Antlr4.Runtime.Standard" Version="4.11.1" />
16-
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="6.0.1" />
1724
</ItemGroup>
1825

1926
<ItemGroup>

test/Simpleflow.Tests/Infrastructure/ArgumentExceptionTest.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace Simpleflow.Tests.Infrastructure
55
{
6+
#if !NET48
67
public class ArgumentExceptionTest
78
{
89
[Fact]
@@ -31,4 +32,5 @@ public void ThrowIfNullOrEmptyWithParamName()
3132
Assert.Throws<ArgumentNullException>("name", () => ArgumentException.ThrowIfNullOrEmpty(name));
3233
}
3334
}
35+
#endif
3436
}

test/Simpleflow.Tests/Infrastructure/FlowContextCacheOptionsTest.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace Simpleflow.Tests.Infrastructure
88
{
99
public class FlowContextCacheOptionsTest
1010
{
11+
#if !NET48
1112
[Fact]
1213
public void CheckFlowContextCacheOptions()
1314
{
@@ -38,6 +39,7 @@ public void CheckFlowContextCacheOptions()
3839

3940
// Assert
4041
}
42+
#endif
4143

4244
class CacheService2 : Services.CacheService
4345
{

test/Simpleflow.Tests/Infrastructure/SimpleflowTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ var engine
3333
public void BuildAndRunSimpleflow_CheckWhetherEachServiceIsInvoked(int id, string expect)
3434
{
3535
// Arrange
36-
string script = @$"rule when arg.Id > 0 then
36+
string script = @"rule when arg.Id > 0 then
3737
message ""Member exists""
3838
/*end rule*/
39-
rule when arg.Id == {id} then
40-
message ""The {expect} Member""
39+
rule when arg.Id == " + id + @" then
40+
message ""The " + expect + @" Member""
4141
4242
";
4343

4444
// Act
45-
FlowOutput result = _flow.Run(script, new Member { Id = id});
45+
FlowOutput result = _flow.Run(script, new Member { Id = id });
4646

4747

4848
// Assert
@@ -83,7 +83,7 @@ public void Run_CheckArgumentException()
8383

8484
// Act & Assert
8585
Assert.Throws<ArgumentNullException>(() => _flow.Run(script, new { Test = 1 }, config: null));
86-
Assert.Throws<ArgumentNullException>(() => _flow.Run(script, new { Test = 1, Abc=2 }, options: null));
86+
Assert.Throws<ArgumentNullException>(() => _flow.Run(script, new { Test = 1, Abc = 2 }, options: null));
8787
Assert.Throws<ArgumentNullException>(() => _flow.Run(script, new { Test = 1 }, options: null, config: null));
8888
Assert.Throws<ArgumentNullException>(() => _flow.Run(script, new { Test = 1 }, options: new FlowContextOptions(), config: null));
8989
Assert.Throws<ArgumentNullException>(() => _flow.Run(script, new { Test = 1 }, options: null, config: new FunctionRegister()));

test/Simpleflow.Tests/Infrastructure/SyntaxTreeTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ public void SimpleflowAst()
1616
{
1717
// Arrange
1818
var flowScript =
19-
@$"
19+
@"
2020
/* Declare and initialize variables */
2121
let userId = none
2222
let currentDate = $GetCurrentDateTime (
23-
timezone: ""{TestsHelper.Timezone}"" )
23+
timezone: """ + TestsHelper.Timezone + @""" )
2424
2525
/* Define Rules */
2626
rule when arg.Name == """"
@@ -45,10 +45,10 @@ rule when context.HasErrors then
4545
end rule
4646
4747
/* Set current date time */
48-
partial set arg = {{
48+
partial set arg = {
4949
RegistrationDate: currentDate,
5050
IsActive: true
51-
}}
51+
}
5252
5353
";
5454

@@ -66,7 +66,7 @@ public void SimpleflowAstWithErrors()
6666
{
6767
// Arrange
6868
var flowScript =
69-
@$"
69+
@"
7070
let userId = none
7171
test xyz
7272
";

test/Simpleflow.Tests/Scripting/ScriptContextArgTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public void ContextHasErrors()
1212
{
1313
// Arrange
1414
var script =
15-
@$"
15+
@"
1616
error ""test""
1717
output context.haserrors
1818
";
@@ -27,7 +27,7 @@ public void ContextHasNoErrors()
2727
{
2828
// Arrange
2929
var script =
30-
@$"
30+
@"
3131
output context.HasErrors
3232
";
3333

test/Simpleflow.Tests/Simpleflow.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netcoreapp3.1;net6.0</TargetFrameworks>
4+
<TargetFrameworks>net48;netcoreapp3.1;net6.0;</TargetFrameworks>
55

66
<IsPackable>false</IsPackable>
77
</PropertyGroup>

test/Simpleflow.Tests/SimpleflowTry.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ public class SimpleflowTry
88
[Fact]
99
public void Try()
1010
{
11-
// Arrange -- Don't change the indentation of variable, its written for a type of testing of parser
12-
var flowScript =
13-
@$"
11+
// Arrange -- Don't change the indentation of variable, its written for a type of testing of parser
12+
var flowScript =
13+
@"
1414
let text = 'Hello, विश्वम्‌'
15-
let today = $GetCurrentDateTime ( timezone: '{TestsHelper.Timezone}' )
15+
let today = $GetCurrentDateTime ( timezone: '" + TestsHelper.Timezone + @"' )
1616
1717
/* Comment: Message when UniversalId is 2 and New is true */
1818
rule when arg.UniversalId == 2 and (arg.New or arg.Verified) then
@@ -32,13 +32,13 @@ public void Try1()
3232
{
3333
// Arrange
3434
var flowScript =
35-
@$"
35+
@"
3636
37-
let today = $GetCurrentDateTime ( timezone: ""{TestsHelper.Timezone}"" )
37+
let today = $GetCurrentDateTime ( timezone: """ + TestsHelper.Timezone + @""" )
3838
3939
# Write rules
4040
rule when arg.UniversalId == 2 and (arg.New or arg.Verified) then
41-
message `Hello, World! 🌄, Universal Id {{arg.UniversalId}}`
41+
message `Hello, World! 🌄, Universal Id {arg.UniversalId}`
4242
end rule
4343
4444
# Output
@@ -57,9 +57,9 @@ public void Try2()
5757
{
5858
// Arrange
5959
var flowScript =
60-
@$"
60+
@"
6161
let text = ""Hello, विश्वम्‌""
62-
let today = $GetCurrentDateTime ( timezone: ""{TestsHelper.Timezone}"" )
62+
let today = $GetCurrentDateTime ( timezone: """ + TestsHelper.Timezone + @""" )
6363
6464
/* Comment: Message when UniversalId is 2 and New is true */
6565
rule when arg.UniversalId == 2 and (arg.New or arg.Verified) then
@@ -81,7 +81,7 @@ public void Try3()
8181
{
8282
// Arrange
8383
var flowScript =
84-
@$"
84+
@"
8585
/* Declare and initialize variables */
8686
let userId = none
8787
@@ -109,11 +109,11 @@ rule when context.HasErrors then
109109
end rule
110110
111111
/* Set current date time */
112-
partial set arg = {{
112+
partial set arg = {
113113
RegistrationDate: $GetCurrentDateTime (
114-
timezone: ""{TestsHelper.Timezone}"" ),
114+
timezone: """ + TestsHelper.Timezone + @""" ),
115115
IsActive: true
116-
}}
116+
}
117117
118118
set _, err = $SendEmail(body: 'test',
119119
to: arg.email)

0 commit comments

Comments
 (0)