Skip to content

Commit

Permalink
Refactor asset and symbol cache implementations
Browse files Browse the repository at this point in the history
For #89. Update static assets and symbols.
  • Loading branch information
sonvister committed May 4, 2018
1 parent 571f862 commit e9c364c
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 138 deletions.
19 changes: 14 additions & 5 deletions samples/BinanceCodeGenerator/Asset.template.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public sealed class Asset : IComparable<Asset>, IEquatable<Asset>
/// <summary>
/// Asset cache.
/// </summary>
public static IAssetCache Cache { get; set; }
public static IObjectCache<Asset> Cache { get; set; }

/// <summary>
/// Get the asset symbol.
Expand All @@ -65,15 +65,14 @@ static Asset()
{
try
{
Cache = new InMemoryAssetCache();
Cache = new InMemoryCache<Asset>();

Cache.Load(
Cache.Set(
new[] {
// <<insert asset definitions>>
});

// Redirect (BCH) Bitcoin Cash (BCC = BitConnect)
Cache.Set("BCH", Cache.Get("BCC"));
AddCacheRedirections();
}
catch (Exception e)
{
Expand Down Expand Up @@ -139,6 +138,16 @@ public override string ToString()

#endregion Public Methods

#region Internal Methods

internal static void AddCacheRedirections()
{
// Redirect (BCH) Bitcoin Cash (BCC = BitConnect)
Cache.Set("BCH", Cache.Get("BCC"));
}

#endregion Internal Methods

#region IComparable<Asset>

public int CompareTo(Asset other)
Expand Down
27 changes: 22 additions & 5 deletions samples/BinanceCodeGenerator/Symbol.template.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public sealed class Symbol : IComparable<Symbol>, IEquatable<Symbol>
/// <summary>
/// Symbol cache.
/// </summary>
public static ISymbolCache Cache { get; set; }
public static IObjectCache<Symbol> Cache { get; set; }

/// <summary>
/// Get the symbol status.
Expand Down Expand Up @@ -103,9 +103,9 @@ static Symbol()
{
try
{
Cache = new InMemorySymbolCache();
Cache = new InMemoryCache<Symbol>();

Cache.Load(
Cache.Set(
new[] {
// <<insert symbol definitions>>
});
Expand Down Expand Up @@ -187,7 +187,9 @@ public static async Task UpdateCacheAsync(IBinanceApi api, CancellationToken tok
var symbols = await api.GetSymbolsAsync(token)
.ConfigureAwait(false);

Cache.Load(symbols);
Cache.Clear();
Cache.Set(symbols);
AddCacheRedirections();

var assets = new List<Asset>();

Expand All @@ -200,7 +202,9 @@ public static async Task UpdateCacheAsync(IBinanceApi api, CancellationToken tok
assets.Add(symbol.QuoteAsset);
}

Asset.Cache.Load(assets);
Asset.Cache.Clear();
Asset.Cache.Set(assets);
Asset.AddCacheRedirections();
}

public override string ToString()
Expand All @@ -227,6 +231,19 @@ public override int GetHashCode()

#endregion Public Methods

#region Private Methods

private static void AddCacheRedirections()
{
// Redirect (BCH) Bitcoin Cash (BCC = BitConnect)
Cache.Set("BCH_USDT", Cache.Get("BCC_USDT"));
Cache.Set("BCH_BNB", Cache.Get("BCC_BNB"));
Cache.Set("BCH_BTC", Cache.Get("BCC_BTC"));
Cache.Set("BCH_ETH", Cache.Get("BCC_ETH"));
}

#endregion Private Methods

#region IComparable<Symbol>

public int CompareTo(Symbol other)
Expand Down
21 changes: 15 additions & 6 deletions src/Binance/Asset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public sealed class Asset : IComparable<Asset>, IEquatable<Asset>
/// <summary>
/// When the assets were last updated.
/// </summary>
public static readonly long LastUpdateAt = 1525308794413;
public static readonly long LastUpdateAt = 1525448255603;

public static Asset ADA => Cache.Get("ADA");
public static Asset ADX => Cache.Get("ADX");
Expand Down Expand Up @@ -163,7 +163,7 @@ public sealed class Asset : IComparable<Asset>, IEquatable<Asset>
/// <summary>
/// Asset cache.
/// </summary>
public static IAssetCache Cache { get; set; }
public static IObjectCache<Asset> Cache { get; set; }

/// <summary>
/// Get the asset symbol.
Expand All @@ -189,9 +189,9 @@ static Asset()
{
try
{
Cache = new InMemoryAssetCache();
Cache = new InMemoryCache<Asset>();

Cache.Load(
Cache.Set(
new[] {
new Asset("ADA", 8),
new Asset("ADX", 8),
Expand Down Expand Up @@ -320,8 +320,7 @@ static Asset()
new Asset("ZRX", 8),
});

// Redirect (BCH) Bitcoin Cash (BCC = BitConnect)
Cache.Set("BCH", Cache.Get("BCC"));
AddCacheRedirections();
}
catch (Exception e)
{
Expand Down Expand Up @@ -387,6 +386,16 @@ public override string ToString()

#endregion Public Methods

#region Internal Methods

internal static void AddCacheRedirections()
{
// Redirect (BCH) Bitcoin Cash (BCC = BitConnect)
Cache.Set("BCH", Cache.Get("BCC"));
}

#endregion Internal Methods

#region IComparable<Asset>

public int CompareTo(Asset other)
Expand Down
5 changes: 0 additions & 5 deletions src/Binance/Cache/IAssetCache.cs

This file was deleted.

20 changes: 11 additions & 9 deletions src/Binance/Cache/IObjectCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ namespace Binance.Cache
/// <typeparam name="T"></typeparam>
public interface IObjectCache<T>
{
/// <summary>
/// Remove all items from the cache.
/// </summary>
void Clear();

/// <summary>
/// Get an item with matching key or null.
/// </summary>
Expand All @@ -21,20 +26,17 @@ public interface IObjectCache<T>
/// <returns></returns>
IEnumerable<T> GetAll();

/// <summary>
/// Load the cache, adding new items, replacing existing items,
/// and removing items that are not contained in the list.
///
/// NOTE: This does not clear the cache to preseve redirections.
/// </summary>
/// <param name="items"></param>
void Load(IEnumerable<T> items);

/// <summary>
/// Add/Replace an item by key.
/// </summary>
/// <param name="key"></param>
/// <param name="item"></param>
void Set(string key, T item);

/// <summary>
/// Add/Replace multiple items using the ToString() value as the key.
/// </summary>
/// <param name="items"></param>
void Set(IEnumerable<T> items);
}
}
5 changes: 0 additions & 5 deletions src/Binance/Cache/ISymbolCache.cs

This file was deleted.

74 changes: 0 additions & 74 deletions src/Binance/Cache/InMemoryAssetCache.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,55 @@

namespace Binance.Cache
{
public sealed class InMemorySymbolCache : ISymbolCache
public class InMemoryCache<T> : IObjectCache<T>
{
private readonly IDictionary<string, Symbol> _cache;
private readonly IDictionary<string, T> _cache;

private readonly object _sync = new object();

public InMemorySymbolCache()
public InMemoryCache()
{
_cache = new Dictionary<string, Symbol>();
_cache = new Dictionary<string, T>();
}

public IEnumerable<Symbol> GetAll()
public void Clear()
{
lock (_sync)
{
_cache.Clear();
}
}

public IEnumerable<T> GetAll()
{
lock (_sync)
{
return _cache.Values;
}
}

public Symbol Get(string key)
public T Get(string key)
{
if (key == null)
return null;
return default;

var _key = key.FormatSymbol();

lock (_sync)
{
return _cache.ContainsKey(_key) ? _cache[_key] : null;
return _cache.ContainsKey(_key) ? _cache[_key] : default;
}
}

public void Set(string key, Symbol symbol)
public void Set(string key, T symbol)
{
lock (_sync)
{
_cache[string.Intern(key)] = symbol;
}
}

public void Load(IEnumerable<Symbol> symbols)
public void Set(IEnumerable<T> symbols)
{
Throw.IfNull(symbols, nameof(symbols));

Expand All @@ -54,21 +62,11 @@ public void Load(IEnumerable<Symbol> symbols)

lock (_sync)
{
// Remove any old symbols (preserves redirections).
// ReSharper disable once PossibleMultipleEnumeration
foreach (var symbol in _cache.Values.ToArray())
{
if (!symbols.Contains(symbol))
{
_cache.Remove(symbol);
}
}

// Update existing and add any new symbols.
// ReSharper disable once PossibleMultipleEnumeration
foreach (var symbol in symbols)
{
_cache[string.Intern(symbol)] = symbol;
_cache[string.Intern(symbol.ToString())] = symbol;
}
}
}
Expand Down
Loading

0 comments on commit e9c364c

Please sign in to comment.