Skip to content

Commit

Permalink
Merge pull request #31 from LykkeCity/feature_LWDEV-6370-stateless
Browse files Browse the repository at this point in the history
Changed cache implementation from in-memory into Redis.
  • Loading branch information
polonzer authored Mar 26, 2018
2 parents 11a0c0a + a6a9de0 commit e30f109
Show file tree
Hide file tree
Showing 37 changed files with 405 additions and 318 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.0.0" />
<PackageReference Include="Microsoft.Rest.ClientRuntime" Version="2.3.10" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Version>1.0.0</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Lykke.Common" Version="4.4.3" />
<PackageReference Include="Lykke.SettingsReader" Version="2.1.0" />
<PackageReference Include="Lykke.Common" Version="6.0.0" />
<PackageReference Include="Lykke.SettingsReader" Version="2.2.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace Lykke.Service.Assets.Core.Services
{
public interface IErcContractProcessor
{
Task ProcessErc20ContractAsync(IErc20Token message);
Task ProcessErc20ContractAsync(IErc20Token token);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<ItemGroup>
<PackageReference Include="Autofac" Version="4.6.2" />
<PackageReference Include="AutoMapper" Version="6.2.2" />
<PackageReference Include="Lykke.AzureStorage" Version="5.0.2" />
<PackageReference Include="Lykke.AzureStorage" Version="7.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down
17 changes: 6 additions & 11 deletions src/Lykke.Service.Assets.Services/ErcContractProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
using Lykke.Service.Assets.Core.Domain;
using Lykke.Service.Assets.Core.Repositories;
using Lykke.Service.Assets.Core.Services;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Lykke.Service.Assets.Services
{
public class ErcContractProcessor : IErcContractProcessor
{
private readonly IErc20TokenRepository _erc20TokenRepository;
private readonly IErc20TokenService _erc20TokenService;

public ErcContractProcessor(IErc20TokenRepository erc20TokenRepository)
public ErcContractProcessor(IErc20TokenService erc20TokenService)
{
_erc20TokenRepository = erc20TokenRepository;
_erc20TokenService = erc20TokenService;
}

//TODO: Add more logic here
public async Task ProcessErc20ContractAsync(IErc20Token message)
public async Task ProcessErc20ContractAsync(IErc20Token token)
{
var existingContract = await _erc20TokenRepository.GetByTokenAddressAsync(message.Address);
var existingContract = await _erc20TokenService.GetByTokenAddressAsync(token.Address);

if (existingContract == null)
{
await _erc20TokenRepository.AddAsync(message);
await _erc20TokenService.AddAsync(token);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<ItemGroup>
<PackageReference Include="Autofac" Version="4.6.2" />
<PackageReference Include="AutoMapper" Version="6.2.2" />
<PackageReference Include="Lykke.Cqrs" Version="4.1.0" />
<PackageReference Include="Lykke.Cqrs" Version="4.4.0" />
<PackageReference Include="Lykke.Logs" Version="3.6.0" />
</ItemGroup>

Expand Down
15 changes: 15 additions & 0 deletions src/Lykke.Service.Assets/Cache/CacheSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Lykke.Service.Assets.Cache
{
internal static class CacheSerializer
{
public static T Deserialize<T>(byte[] settings)
{
return MessagePack.MessagePackSerializer.Deserialize<T>(settings, MessagePack.Resolvers.ContractlessStandardResolver.Instance);
}

public static byte[] Serialize<T>(T settings)
{
return MessagePack.MessagePackSerializer.Serialize(settings, MessagePack.Resolvers.ContractlessStandardResolver.Instance);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Lykke.Service.Assets.Cache;
using Lykke.Service.Assets.Core.Domain;
using Lykke.Service.Assets.Core.Services;
using Lykke.Service.Assets.Repositories.Entities;

namespace Lykke.Service.Assets.Managers
namespace Lykke.Service.Assets.Cache
{
public class AssetCategoryManager : IAssetCategoryManager
public class CachedAssetCategoryService : ICachedAssetCategoryService
{
private readonly IAssetCategoryService _assetCategoryService;
private readonly ICache<IAssetCategory> _cache;
private readonly IAssetCategoryService _assetCategoryService;
private readonly DistributedCache<IAssetCategory, AssetCategoryEntity> _cache;
private const string AllEntitiesKey = "All";


public AssetCategoryManager(
public CachedAssetCategoryService(
IAssetCategoryService assetCategoryService,
ICache<IAssetCategory> cache)
DistributedCache<IAssetCategory, AssetCategoryEntity> cache)
{
_assetCategoryService = assetCategoryService;
_cache = cache;
_cache = cache;
}

public async Task<IAssetCategory> AddAsync(IAssetCategory assetCategory)
Expand All @@ -34,26 +34,30 @@ public async Task<IAssetCategory> GetAsync(string id)

public async Task<IEnumerable<IAssetCategory>> GetAllAsync()
{
return await _cache.GetListAsync("All", _assetCategoryService.GetAllAsync);
}

public async Task InvalidateCache()
{
await _cache.InvalidateAsync();
return await _cache.GetListAsync(AllEntitiesKey, () => _assetCategoryService.GetAllAsync());
}

public async Task RemoveAsync(string id)
{
await InvalidateCache();
await InvalidateCache(id);

await _assetCategoryService.RemoveAsync(id);
}

public async Task UpdateAsync(IAssetCategory assetCategory)
{
await InvalidateCache();
await InvalidateCache(assetCategory.Id);

await _assetCategoryService.UpdateAsync(assetCategory);
}

private async Task InvalidateCache(string id = null)
{
if (id != null)
{
await _cache.RemoveAsync(id);
}
await _cache.RemoveAsync(AllEntitiesKey);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Lykke.Service.Assets.Cache;
using Lykke.Service.Assets.Core.Domain;
using Lykke.Service.Assets.Core.Services;
using Lykke.Service.Assets.Repositories.Entities;

namespace Lykke.Service.Assets.Managers
namespace Lykke.Service.Assets.Cache
{
public class AssetPairManager : IAssetPairManager
public class CachedAssetPairService : ICachedAssetPairService
{
private readonly IAssetPairService _assetPairService;
private readonly ICache<IAssetPair> _cache;
private readonly IAssetPairService _assetPairService;
private readonly DistributedCache<IAssetPair, AssetPairEntity> _cache;
private const string AllEntitiesKey = "All";


public AssetPairManager(
public CachedAssetPairService(
IAssetPairService assetPairService,
ICache<IAssetPair> cache)
DistributedCache<IAssetPair, AssetPairEntity> cache)
{
_assetPairService = assetPairService;
_cache = cache;
_cache = cache;
}

public async Task<IAssetPair> AddAsync(IAssetPair assetPair)
Expand All @@ -34,31 +34,35 @@ public IAssetPair CreateDefault()

public async Task<IEnumerable<IAssetPair>> GetAllAsync()
{
return await _cache.GetListAsync("All", _assetPairService.GetAllAsync);
return await _cache.GetListAsync(AllEntitiesKey, _assetPairService.GetAllAsync);
}

public async Task<IAssetPair> GetAsync(string id)
{
return await _cache.GetAsync(id, () => _assetPairService.GetAsync(id));
}

public async Task InvalidateCache()
{
await _cache.InvalidateAsync();
}

public async Task RemoveAsync(string id)
{
await InvalidateCache();
await InvalidateCache(id);

await _assetPairService.RemoveAsync(id);
}

public async Task UpdateAsync(IAssetPair assetPair)
{
await InvalidateCache();
await InvalidateCache(assetPair.Id);

await _assetPairService.UpdateAsync(assetPair);
}

private async Task InvalidateCache(string id = null)
{
if (id != null)
{
await _cache.RemoveAsync(id);
}
await _cache.RemoveAsync(AllEntitiesKey);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Lykke.Service.Assets.Cache;
using Lykke.Service.Assets.Core.Domain;
using Lykke.Service.Assets.Core.Services;
using Lykke.Service.Assets.Tools;
using Lykke.Service.Assets.Repositories.DTOs;

namespace Lykke.Service.Assets.Managers
namespace Lykke.Service.Assets.Cache
{
public class AssetManager : IAssetManager
public class CachedAssetService : ICachedAssetService
{
private readonly IAssetService _assetService;
private readonly ICache<IAsset> _cache;
private readonly IAssetService _assetService;
private readonly DistributedCache<IAsset, AssetDto> _cache;
private const string AllEntitiesKey = "All";
private const string AllTradableKey = "AllTradable";


public AssetManager(
public CachedAssetService(
IAssetService assetService,
ICache<IAsset> cache)
DistributedCache<IAsset, AssetDto> cache)
{
_assetService = assetService;
_cache = cache;
_cache = cache;
}

public async Task<IAsset> AddAsync(IAsset asset)
Expand All @@ -36,35 +36,34 @@ public IAsset CreateDefault()

public async Task DisableAsync(string id)
{
await InvalidateCache();
await InvalidateCache(id);

await _assetService.DisableAsync(id);
}

public async Task EnableAsync(string id)
{
await InvalidateCache();
await InvalidateCache(id);

await _assetService.EnableAsync(id);
}

public async Task<IEnumerable<IAsset>> GetAllAsync(bool includeNonTradable)
{
var listKey = includeNonTradable ? "All" : "AllTradable";
var listKey = includeNonTradable ? AllEntitiesKey : AllTradableKey;

return await _cache.GetListAsync(listKey, () => _assetService.GetAllAsync(includeNonTradable));
}

public async Task<IEnumerable<IAsset>> GetAsync(string[] ids, bool? isTradable)
{
var listKey = ids.Any() ? ids.GetMD5() : "All";

if (isTradable.HasValue)
var assets = await GetAllAsync(isTradable ?? true);
if (ids.Any())
{
listKey = $"{listKey}:IsTradable_{isTradable.Value}";
assets = assets.Where(x => ids.Contains(x.Id));
}

return await _cache.GetListAsync($"ForIdsList:{listKey}", () => _assetService.GetAsync(ids, isTradable));
return assets;
}

public async Task<IAsset> GetAsync(string id)
Expand All @@ -74,26 +73,32 @@ public async Task<IAsset> GetAsync(string id)

public async Task<IEnumerable<IAsset>> GetForCategoryAsync(string categoryId)
{
return await _cache.GetListAsync($"ForCategory:{categoryId}", () => _assetService.GetForCategoryAsync(categoryId));
}

public async Task InvalidateCache()
{
await _cache.InvalidateAsync();
var assets = await GetAllAsync(true);
return assets.Where(x => x.CategoryId == categoryId);
}

public async Task RemoveAsync(string id)
{
await InvalidateCache();
await InvalidateCache(id);

await _assetService.RemoveAsync(id);
}

public async Task UpdateAsync(IAsset asset)
{
await InvalidateCache();
await InvalidateCache(asset.Id);

await _assetService.UpdateAsync(asset);
}

private async Task InvalidateCache(string id = null)
{
if (id != null)
{
await _cache.RemoveAsync(id);
}
await _cache.RemoveAsync(AllEntitiesKey);
await _cache.RemoveAsync(AllTradableKey);
}
}
}
Loading

0 comments on commit e30f109

Please sign in to comment.