Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to use async friendly datastore #44

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Gauge.CSharp.Lib/DataStoreExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,39 @@ internal static void Clear(this ThreadLocal<ConcurrentDictionary<object, object>
store.Value.Clear();
}
}
internal static object Get(this ConcurrentDictionary<object, object> store, string key)
{
lock (store)
{
object outVal;
var valueExists = store.TryGetValue(key, out outVal);
return valueExists ? outVal : null;
}
}

internal static T Get<T>(this ConcurrentDictionary<object, object> store, string key)
{
lock (store)
{
return (T)store.Get(key);
}
}

internal static void Add(this ConcurrentDictionary<object, object> store, string key, object value)
{
lock (store)
{
store[key] = value;

}
}

internal static void Clear(this ConcurrentDictionary<object, object> store)
{
lock (store)
{
store.Clear();
}
}
}
}
6 changes: 3 additions & 3 deletions Gauge.CSharp.Lib/Gauge.CSharp.Lib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<Description>CSharp bindings for Gauge. Write CSharp step implementation for Gauge specs. https://gauge.org</Description>
<Version>0.10.3</Version>
<AssemblyVersion>0.10.3.0</AssemblyVersion>
<FileVersion>0.10.3.0</FileVersion>
<Version>0.11.0</Version>
<AssemblyVersion>0.11.0.0</AssemblyVersion>
<FileVersion>0.11.0.0</FileVersion>
<Authors>getgauge</Authors>
<Company>ThoughtWorks Inc.</Company>
<Copyright>Copyright © ThoughtWorks Inc. 2018</Copyright>
Expand Down
25 changes: 23 additions & 2 deletions Gauge.CSharp.Lib/ScenarioDataStore.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Concurrent;
using System.Threading;

Expand All @@ -7,24 +8,44 @@ public class ScenarioDataStore
{
private static ThreadLocal<ConcurrentDictionary<object, object>> store = new ThreadLocal<ConcurrentDictionary<object, object>>(() => new ConcurrentDictionary<object, object>());

private static ConcurrentDictionary<object, object> asyncStore = new ConcurrentDictionary<object, object>();

public static object Get(string key)
{
return store.Get(key);
return UseAsyncFriendlyDatastore() ? asyncStore.Get(key) : store.Get(key);
}

public static T Get<T>(string key)
{
return store.Get<T>(key);
return UseAsyncFriendlyDatastore() ? asyncStore.Get<T>(key) : store.Get<T>(key);
}

public static void Add(string key, object value)
{
if(UseAsyncFriendlyDatastore())
{
asyncStore.Add(key, value);
return;
}
store.Add(key, value);
}

public static void Clear()
{
if(UseAsyncFriendlyDatastore())
{
asyncStore.Clear();
return;
}
store.Clear();
}

private static Boolean UseAsyncFriendlyDatastore()
{
var useAsyncFriendlyDatastore = Environment.GetEnvironmentVariable("use_async_friendly_datastores");
if(String.IsNullOrEmpty(useAsyncFriendlyDatastore))
return false;
return useAsyncFriendlyDatastore.Equals("true", StringComparison.OrdinalIgnoreCase);
}
}
}
25 changes: 23 additions & 2 deletions Gauge.CSharp.Lib/SpecDataStore.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Concurrent;
using System.Threading;

Expand All @@ -7,24 +8,44 @@ public class SpecDataStore
{
private static ThreadLocal<ConcurrentDictionary<object, object>> store = new ThreadLocal<ConcurrentDictionary<object, object>>(() => new ConcurrentDictionary<object, object>());

private static ConcurrentDictionary<object, object> asyncStore = new ConcurrentDictionary<object, object>();

public static object Get(string key)
{
return store.Get(key);
return useAsyncFriendlyDatastore() ? asyncStore.Get(key) : store.Get(key);
}

public static T Get<T>(string key)
{
return store.Get<T>(key);
return useAsyncFriendlyDatastore() ? asyncStore.Get<T>(key) : store.Get<T>(key);
}

public static void Add(string key, object value)
{
if(useAsyncFriendlyDatastore())
{
asyncStore.Add(key, value);
return;
}
store.Add(key, value);
}

public static void Clear()
{
if(useAsyncFriendlyDatastore())
{
asyncStore.Clear();
return;
}
store.Clear();
}

private static Boolean useAsyncFriendlyDatastore()
{
string useAsyncFriendlyDatastore = Environment.GetEnvironmentVariable("use_async_friendly_datastores");
if(String.IsNullOrEmpty(useAsyncFriendlyDatastore))
return false;
return useAsyncFriendlyDatastore.Equals("true", StringComparison.OrdinalIgnoreCase);
}
}
}
27 changes: 24 additions & 3 deletions Gauge.CSharp.Lib/SuiteDataStore.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Concurrent;
using System.Threading;

Expand All @@ -7,24 +8,44 @@ public class SuiteDataStore
{
private static ThreadLocal<ConcurrentDictionary<object, object>> store = new ThreadLocal<ConcurrentDictionary<object, object>>(() => new ConcurrentDictionary<object, object>());

private static ConcurrentDictionary<object, object> asyncStore = new ConcurrentDictionary<object, object>();

public static object Get(string key)
{
return store.Get(key);
return UseAsyncFriendlyDatastore() ? asyncStore.Get(key) : store.Get(key);
}

public static T Get<T>(string key)
{
return store.Get<T>(key);
return UseAsyncFriendlyDatastore() ? asyncStore.Get<T>(key) : store.Get<T>(key);
}

public static void Add(string key, object value)
{
if(UseAsyncFriendlyDatastore())
{
asyncStore.Add(key, value);
return;
}
store.Add(key, value);
}

public static void Clear()
{
if(UseAsyncFriendlyDatastore())
{
asyncStore.Clear();
return;
}
store.Clear();
}

private static Boolean UseAsyncFriendlyDatastore()
{
var useAsyncFriendlyDatastore = Environment.GetEnvironmentVariable("use_async_friendly_datastores");
if(String.IsNullOrEmpty(useAsyncFriendlyDatastore))
return false;
return useAsyncFriendlyDatastore.Equals("true", StringComparison.OrdinalIgnoreCase);
}
}
}
}
Loading