-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add background cache update functionality.
- Loading branch information
Showing
8 changed files
with
246 additions
and
30 deletions.
There are no files selected for viewing
46 changes: 35 additions & 11 deletions
46
client/Lykke.Service.Assets.Client/AssetsServiceWithCache.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 52 additions & 9 deletions
61
client/Lykke.Service.Assets.Client/Cache/DictionaryCache.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
client/Lykke.Service.Assets.Client/Cache/IDictionaryCache.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,39 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Common.Log; | ||
|
||
namespace Lykke.Service.Assets.Client.Cache | ||
{ | ||
/// <summary> | ||
/// Simple in-memory client side cache. | ||
/// </summary> | ||
public interface IDictionaryCache<T> | ||
where T : ICacheItem | ||
{ | ||
/// <summary> | ||
/// Starts an automatic updater that keeps the cache updated on a background thread. | ||
/// </summary> | ||
IDisposable StartAutoUpdate(string componentName, ILog log, Func<Task<IEnumerable<T>>> getAllAsync); | ||
|
||
/// <summary> | ||
/// Update the cache when cache has expired. | ||
/// </summary> | ||
Task EnsureCacheIsUpdatedAsync(Func<Task<IEnumerable<T>>> getAllAsync); | ||
|
||
/// <summary> | ||
/// Update the cache with given data. | ||
/// </summary> | ||
void Update(IEnumerable<T> items); | ||
|
||
/// <summary> | ||
/// Try to get cached item with given id. | ||
/// </summary> | ||
T TryGet(string id); | ||
|
||
/// <summary> | ||
/// Get all cached items. | ||
/// </summary> | ||
IReadOnlyCollection<T> GetAll(); | ||
} | ||
} |
34 changes: 31 additions & 3 deletions
34
client/Lykke.Service.Assets.Client/IAssetsServiceWithCache.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,56 @@ | ||
using System.Collections.Generic; | ||
using Lykke.Service.Assets.Client.Models; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Lykke.Service.Assets.Client.Models; | ||
|
||
namespace Lykke.Service.Assets.Client | ||
{ | ||
/// <summary> | ||
/// Client side in-memory cached version of the <see cref="IAssetsService"/>. | ||
/// </summary> | ||
public interface IAssetsServiceWithCache | ||
{ | ||
/// <summary> | ||
/// Get all asset-pairs. | ||
/// </summary> | ||
Task<IReadOnlyCollection<AssetPair>> GetAllAssetPairsAsync(CancellationToken cancellationToken = new CancellationToken()); | ||
|
||
/// <summary> | ||
/// Get all assets | ||
/// </summary> | ||
[Obsolete("Use GetAllAssetsAsync(bool) instead")] | ||
Task<IReadOnlyCollection<Asset>> GetAllAssetsAsync(CancellationToken cancellationToken = new CancellationToken()); | ||
|
||
/// <summary> | ||
/// Get all assets | ||
/// </summary> | ||
Task<IReadOnlyCollection<Asset>> GetAllAssetsAsync(bool includeNonTradable, CancellationToken cancellationToken = new CancellationToken()); | ||
|
||
/// <summary> | ||
/// Try to find an asset with given id. | ||
/// </summary> | ||
Task<Asset> TryGetAssetAsync(string assetId, CancellationToken cancellationToken = new CancellationToken()); | ||
|
||
/// <summary> | ||
/// Try to find an asset-pair with given id. | ||
/// </summary> | ||
Task<AssetPair> TryGetAssetPairAsync(string assetPairId, CancellationToken cancellationToken = new CancellationToken()); | ||
|
||
/// <summary> | ||
/// Forcibly updates client-side asset pairs cache | ||
/// Forcibly updates client-side asset-pairs cache | ||
/// </summary> | ||
Task UpdateAssetPairsCacheAsync(CancellationToken cancellationToken = new CancellationToken()); | ||
|
||
/// <summary> | ||
/// Forcibly updates client-side assets cache | ||
/// </summary> | ||
Task UpdateAssetsCacheAsync(CancellationToken cancellationToken = new CancellationToken()); | ||
|
||
/// <summary> | ||
/// Starts an automatic update process that will keep the caches updated the background. | ||
/// </summary> | ||
/// <returns>the update process, when disposed the auto update will stop</returns> | ||
IDisposable StartAutoCacheUpdate(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 14 additions & 5 deletions
19
client/Lykke.Service.Assets.Client/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,33 @@ | ||
using Lykke.Service.Assets.Client.Cache; | ||
using Common.Log; | ||
using Lykke.Service.Assets.Client.Cache; | ||
using Lykke.Service.Assets.Client.Models; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Lykke.Service.Assets.Client | ||
{ | ||
public static class ServiceCollectionExtensions | ||
{ | ||
public static void RegisterAssetsClient(this IServiceCollection services, AssetServiceSettings settings) | ||
public static void RegisterAssetsClient(this IServiceCollection services, AssetServiceSettings settings, | ||
ILog log) | ||
{ | ||
services | ||
.AddSingleton<IAssetsService>(x => new AssetsService(settings.BaseUri, settings.Handlers)); | ||
|
||
services | ||
.AddTransient<IAssetsServiceWithCache, AssetsServiceWithCache>(); | ||
.AddSingleton<IDictionaryCache<Asset>>(x => | ||
new DictionaryCache<Asset>(new DateTimeProvider(), settings.AssetsCacheExpirationPeriod)); | ||
|
||
services | ||
.AddSingleton<IDictionaryCache<Asset>>(x => new DictionaryCache<Asset>(new DateTimeProvider(), settings.AssetsCacheExpirationPeriod)); | ||
.AddSingleton<IDictionaryCache<AssetPair>>(x => | ||
new DictionaryCache<AssetPair>(new DateTimeProvider(), settings.AssetPairsCacheExpirationPeriod)); | ||
|
||
services | ||
.AddSingleton<IDictionaryCache<AssetPair>>(x => new DictionaryCache<AssetPair>(new DateTimeProvider(), settings.AssetPairsCacheExpirationPeriod)); | ||
.AddSingleton<IAssetsServiceWithCache>(x => new AssetsServiceWithCache( | ||
x.GetService<IAssetsService>(), | ||
x.GetService<IDictionaryCache<Asset>>(), | ||
x.GetService<IDictionaryCache<AssetPair>>(), | ||
log | ||
)); | ||
} | ||
} | ||
} |
Oops, something went wrong.