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

Make Barrel and HttpCache singletons thread-safe #80

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/MonkeyCache.FileStore/Barrel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ public class Barrel : IBarrel

public bool AutoExpire { get ; set; }

static Barrel instance = null;
static readonly Lazy<Barrel> instance = new Lazy<Barrel>(() => new Barrel());

/// <summary>
/// Gets the instance of the Barrel
/// </summary>
public static IBarrel Current => (instance ?? (instance = new Barrel()));
public static IBarrel Current => instance.Value;

/// <summary>
/// FileStore Barrel
Expand Down
10 changes: 5 additions & 5 deletions src/MonkeyCache.LiteDB/Barrel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ public class Barrel : IBarrel

public bool AutoExpire { get; set; }

static Barrel instance = null;
static Lazy<Barrel> instance = new Lazy<Barrel>(() => new Barrel());
static ILiteCollection<Banana> col;

/// <summary>
/// Gets the instance of the Barrel
/// </summary>
public static IBarrel Current => (instance ?? (instance = new Barrel()));
public static IBarrel Current => instance.Value;

public static IBarrel Create(string cacheDirectory, bool cache = false)
{
if (!cache)
return new Barrel(cacheDirectory);

if(instance == null)
instance = new Barrel(cacheDirectory);
return instance;
if(!instance.IsValueCreated)
instance = new Lazy<Barrel>(() => new Barrel(cacheDirectory));
return instance.Value;
}

Barrel(string cacheDirectory = null)
Expand Down
6 changes: 3 additions & 3 deletions src/MonkeyCache.SQLite/Barrel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ public class Barrel : IBarrel

public bool AutoExpire { get; set; }

static Barrel instance = null;
static readonly Lazy<Barrel> instance = new Lazy<Barrel>(() => new Barrel());

/// <summary>
/// Gets the instance of the Barrel
/// </summary>
public static IBarrel Current => (instance ?? (instance = new Barrel()));
public static IBarrel Current => instance.Value;

public static IBarrel Create(string cacheDirectory)
=> new Barrel(cacheDirectory);
Expand Down Expand Up @@ -320,4 +320,4 @@ public void Empty(params string[] key)

#endregion
}
}
}
4 changes: 2 additions & 2 deletions src/MonkeyCache/HttpCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ namespace MonkeyCache
public class HttpCache
{

static HttpCache instance = null;
static readonly Lazy<HttpCache> instance = new Lazy<HttpCache>(() => new HttpCache());

/// <summary>
/// Gets the instance of the HttpCache
/// </summary>
public static HttpCache Current => (instance ?? (instance = new HttpCache()));
public static HttpCache Current => instance.Value;

HttpCache()
{
Expand Down