Skip to content

Commit

Permalink
review comments and nuget references cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
njannink committed May 31, 2018
1 parent 7a6801d commit 9484c15
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 65 deletions.
30 changes: 15 additions & 15 deletions client/Lykke.Service.Assets.Client/AssetsServiceWithCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace Lykke.Service.Assets.Client
{
///<inheritdoc/>
public class AssetsServiceWithCache : IAssetsServiceWithCache
internal class AssetsServiceWithCache : IAssetsServiceWithCache
{
private readonly IDictionaryCache<Asset> _assetsCache;
private readonly IDictionaryCache<AssetPair> _assetPairsCache;
Expand All @@ -20,30 +20,30 @@ public AssetsServiceWithCache(IDictionaryCache<Asset> assetsCache, IDictionaryCa
}

///<inheritdoc/>
public async Task<IReadOnlyCollection<AssetPair>> GetAllAssetPairsAsync(CancellationToken cancellationToken = new CancellationToken())
=> await _assetPairsCache.GetAll(cancellationToken);
public Task<IReadOnlyCollection<AssetPair>> GetAllAssetPairsAsync(CancellationToken cancellationToken = new CancellationToken())
=> _assetPairsCache.GetAll(cancellationToken);

async Task<IReadOnlyCollection<Asset>> IAssetsServiceWithCache.GetAllAssetsAsync(CancellationToken cancellationToken)
=> await GetAllAssetsAsync(false, cancellationToken);
Task<IReadOnlyCollection<Asset>> IAssetsServiceWithCache.GetAllAssetsAsync(CancellationToken cancellationToken = new CancellationToken())
=> GetAllAssetsAsync(false, cancellationToken);

///<inheritdoc/>
public async Task<IReadOnlyCollection<Asset>> GetAllAssetsAsync(bool includeNonTradable, CancellationToken cancellationToken = new CancellationToken())
=> await _assetsCache.GetAll(cancellationToken);
public Task<IReadOnlyCollection<Asset>> GetAllAssetsAsync(bool includeNonTradable, CancellationToken cancellationToken = new CancellationToken())
=> _assetsCache.GetAll(cancellationToken);

///<inheritdoc/>
public async Task<Asset> TryGetAssetAsync(string assetId, CancellationToken cancellationToken = new CancellationToken())
=> await _assetsCache.TryGet(assetId, cancellationToken);
public Task<Asset> TryGetAssetAsync(string assetId, CancellationToken cancellationToken = new CancellationToken())
=> _assetsCache.TryGet(assetId, cancellationToken);

///<inheritdoc/>
public async Task<AssetPair> TryGetAssetPairAsync(string assetPairId, CancellationToken cancellationToken = new CancellationToken())
=> await _assetPairsCache.TryGet(assetPairId, cancellationToken);
public Task<AssetPair> TryGetAssetPairAsync(string assetPairId, CancellationToken cancellationToken = new CancellationToken())
=> _assetPairsCache.TryGet(assetPairId, cancellationToken);

///<inheritdoc/>
public async Task UpdateAssetPairsCacheAsync(CancellationToken cancellationToken = new CancellationToken())
=> await _assetPairsCache.Reset(cancellationToken);
public Task UpdateAssetPairsCacheAsync(CancellationToken cancellationToken = new CancellationToken())
=> _assetPairsCache.Reset(cancellationToken);

///<inheritdoc/>
public async Task UpdateAssetsCacheAsync(CancellationToken cancellationToken = new CancellationToken())
=> await _assetsCache.Reset(cancellationToken);
public Task UpdateAssetsCacheAsync(CancellationToken cancellationToken = new CancellationToken())
=> _assetsCache.Reset(cancellationToken);
}
}
35 changes: 20 additions & 15 deletions client/Lykke.Service.Assets.Client/Cache/DictionaryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,43 @@

namespace Lykke.Service.Assets.Client.Cache
{
public class DictionaryCache<T> : IDictionaryCache<T>
/// <summary>
/// Base class for a dictionary cache.
/// </summary>
internal class DictionaryCache<T> : IDictionaryCache<T>
where T : ICacheItem
{
protected const string AllItems = @"AllItems";

private const string AllItems = @"AllItems";
private readonly OnDemandDataCache<Dictionary<string, T>> _innerCache;
private readonly IUpdater<T> _updater;
private readonly TimeSpan _expirationTime;

/// <summary>
/// Create new dictionary cache.
/// </summary>
protected DictionaryCache(IUpdater<T> updater, TimeSpan expirationTime)
{
InnerCache = new OnDemandDataCache<Dictionary<string, T>>();
_innerCache = new OnDemandDataCache<Dictionary<string, T>>();
_updater = updater;
_expirationTime = expirationTime;
}

protected OnDemandDataCache<Dictionary<string, T>> InnerCache { get; }

/// <inheritdoc />
public async Task Reset(CancellationToken token)
{
InnerCache.Remove(AllItems);
_innerCache.Remove(AllItems);
await GetItems(token);
}

/// <inheritdoc />
public async Task<T> TryGet(string id, CancellationToken token)
{
var items = await GetItems(token);
items.TryGetValue(id, out var item);
return item;
}

/// <inheritdoc />
public async Task<IReadOnlyCollection<T>> GetAll(CancellationToken token)
{
var items = await GetItems(token);
Expand All @@ -46,15 +53,13 @@ public async Task<IReadOnlyCollection<T>> GetAll(CancellationToken token)

private async Task<Dictionary<string, T>> GetItems(CancellationToken token)
{
return await InnerCache.GetOrAddAsync(AllItems,
async _ => await RetrieveFromUpdater(token)
, _expirationTime);
}
async Task<Dictionary<string, T>> Refresh()
{
var items = await _updater.GetItemsAsync(token);
return items.ToDictionary(x => x.Id);
}

protected async Task<Dictionary<string, T>> RetrieveFromUpdater(CancellationToken token)
{
var items = await _updater.GetItemsAsync(token);
return items.ToDictionary(x => x.Id);
return await _innerCache.GetOrAddAsync(AllItems, _ => Refresh(), _expirationTime);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@

namespace Lykke.Service.Assets.Client.Cache
{
public class ExpiringDictionaryCache<T> : DictionaryCache<T>
/// <summary>
/// Expiring dictionary cache where the cache entry expires after given time.
/// </summary>
/// <typeparam name="T">the type of cached item</typeparam>
internal sealed class ExpiringDictionaryCache<T> : DictionaryCache<T>
where T : ICacheItem
{
/// <summary>
/// Create a new expiring dictionary cache.
/// </summary>
/// <param name="expirationTime">expiration time</param>
/// <param name="updater">item updater</param>
public ExpiringDictionaryCache(TimeSpan expirationTime, IUpdater<T> updater)
: base(updater, expirationTime)
{
Expand Down
8 changes: 7 additions & 1 deletion client/Lykke.Service.Assets.Client/Cache/ICacheItem.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
namespace Lykke.Service.Assets.Client.Cache
{
public interface ICacheItem
/// <summary>
/// Cache item {T} in a <see cref="IDictionaryCache{T}"/>.
/// </summary>
internal interface ICacheItem
{
/// <summary>
/// The id of the entry
/// </summary>
string Id { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Lykke.Service.Assets.Client.Cache
/// <summary>
/// Simple in-memory client side cache.
/// </summary>
public interface IDictionaryCache<T>
internal interface IDictionaryCache<T>
where T : ICacheItem
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Common;
using Common.Log;
using Lykke.Service.Assets.Client.Updaters;

namespace Lykke.Service.Assets.Client.Cache
{
public class RefreshingDictionaryCache<T> : DictionaryCache<T>, IDisposable
/// <summary>
/// A dictionary cache that refreshes/synchronizes in the background.
/// </summary>
/// <typeparam name="T">the type of cached item</typeparam>
internal sealed class RefreshingDictionaryCache<T> : DictionaryCache<T>, IDisposable
where T : ICacheItem
{
private readonly TimerTrigger _trigger;

public RefreshingDictionaryCache(TimeSpan expirationTime, IUpdater<T> updater, ILog log)
: base(updater, expirationTime.Add(expirationTime))
/// <summary>
/// Creates a new refreshing dictionary cache.
/// </summary>
/// <param name="refreshTime">the refresh time</param>
/// <param name="updater">the item updater</param>
/// <param name="log">the lykke log</param>
public RefreshingDictionaryCache(TimeSpan refreshTime, IUpdater<T> updater, ILog log)
: base(updater, refreshTime.Add(refreshTime))
{
_trigger = new TimerTrigger(nameof(AssetsService), expirationTime, log,
async (x, y, token) => await UpdateCache(token));
_trigger = new TimerTrigger(nameof(AssetsService), refreshTime, log,
async (x, y, token) => await Reset(token));
_trigger.Start();
}

private async Task UpdateCache(CancellationToken token)
{
var items = await RetrieveFromUpdater(token);
InnerCache.Set(AllItems, items);
}

/// <inheritdoc />
public void Dispose()
{
_trigger?.Dispose();
Expand Down
4 changes: 4 additions & 0 deletions client/Lykke.Service.Assets.Client/InternalsVisibleTo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Lykke.Service.Assets.Tests")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@

<ItemGroup>
<PackageReference Include="Lykke.Common" Version="6.8.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.0.0" />
<PackageReference Include="Microsoft.Rest.ClientRuntime" Version="2.3.11" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static class ServiceCollectionExtensions
/// <param name="settings">the asset settings</param>
/// <param name="log">the lykke log</param>
/// <param name="autoRefresh">use expiring caches or use a self refreshing cache for the assets and asset-pairs</param>
public static void RegisterAssetsClient(this IServiceCollection services, AssetServiceSettings settings, ILog log, bool autoRefresh = false)
public static void RegisterAssetsClient(this IServiceCollection services, AssetServiceSettings settings, ILog log, bool autoRefresh = true)
{
services
.AddSingleton<IAssetsService>(x => new AssetsService(settings.BaseUri, new HttpClient()));
Expand All @@ -31,8 +31,7 @@ public static void RegisterAssetsClient(this IServiceCollection services, AssetS
services.AddTransient<IUpdater<AssetPair>>(x => new AssetPairsUpdater(x.GetService<IAssetsService>()));
services.AddTransient(x => CreateDictionaryCache<AssetPair>(x, settings.AssetPairsCacheExpirationPeriod, log, autoRefresh));

services
.AddSingleton<IAssetsServiceWithCache>(x => new AssetsServiceWithCache(
services.AddSingleton<IAssetsServiceWithCache>(x => new AssetsServiceWithCache(
x.GetService<IDictionaryCache<Asset>>(),
x.GetService<IDictionaryCache<AssetPair>>()
));
Expand Down
2 changes: 1 addition & 1 deletion client/Lykke.Service.Assets.Client/Updaters/IUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Lykke.Service.Assets.Client.Updaters
/// Updater of <typeparamref name="T"/> for retrieving the contents of a dictionary cache.
/// </summary>
/// <typeparam name="T">the type of cache entry to retrieve</typeparam>
public interface IUpdater<T>
internal interface IUpdater<T>
where T : ICacheItem
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Autofac" Version="4.6.2" />
<PackageReference Include="AutoMapper" Version="6.2.2" />
<PackageReference Include="Lykke.AzureStorage" Version="7.0.2" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Autofac" Version="4.6.2" />
<PackageReference Include="AutoMapper" Version="6.2.2" />
<PackageReference Include="Lykke.Cqrs" Version="4.4.0" />
<PackageReference Include="Lykke.Logs" Version="3.6.0" />
Expand Down
11 changes: 0 additions & 11 deletions src/Lykke.Service.Assets/Lykke.Service.Assets.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,15 @@
<NoWarn>1701;1702;1705;1591</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.2.2" />
<PackageReference Include="AutoMapper" Version="6.2.2" />
<PackageReference Include="JetBrains.Annotations" Version="11.1.0" />
<PackageReference Include="Lykke.AzureStorage" Version="7.0.2" />
<PackageReference Include="Lykke.Common" Version="6.8.2" />
<PackageReference Include="Lykke.Common.ApiLibrary" Version="1.4.0" />
<PackageReference Include="Lykke.Cqrs" Version="4.4.0" />
<PackageReference Include="Lykke.Logs" Version="3.6.0" />
<PackageReference Include="Lykke.Messaging.RabbitMq" Version="2.0.0" />
<PackageReference Include="Lykke.RabbitMqBroker" Version="7.0.0" />
<PackageReference Include="Lykke.SettingsReader" Version="2.2.0" />
<PackageReference Include="Lykke.SlackNotification.AzureQueue" Version="2.0.4" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.2.1" />
<PackageReference Include="Microsoft.AspNetCore" Version="2.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.3" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.2" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Caching.Redis" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="2.3.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Lykke.Service.Assets.Core\Lykke.Service.Assets.Core.csproj" />
Expand Down

0 comments on commit 9484c15

Please sign in to comment.