Skip to content

Commit

Permalink
#203 Implement caching for settings
Browse files Browse the repository at this point in the history
  • Loading branch information
hikalkan committed Dec 12, 2018
1 parent a0262f2 commit 0405476
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Volo.Abp.PermissionManagement
[Serializable]
public class PermissionGrantCacheItem
{
public string Name { get; set; }
public string Name { get; set; } //TODO: Consider to remove this

public bool IsGranted { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.Ddd.Domain\Volo.Abp.Ddd.Domain.csproj" />
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.Settings\Volo.Abp.Settings.csproj" />
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.Caching\Volo.Abp.Caching.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
using Volo.Abp.Domain;
using Volo.Abp.Caching;
using Volo.Abp.Domain;
using Volo.Abp.Modularity;
using Volo.Abp.Settings;

namespace Volo.Abp.SettingManagement
{
[DependsOn(typeof(AbpSettingsModule))]
[DependsOn(typeof(AbpDddDomainModule))]
[DependsOn(typeof(AbpSettingManagementDomainSharedModule))]
[DependsOn(
typeof(AbpSettingsModule),
typeof(AbpDddDomainModule),
typeof(AbpSettingManagementDomainSharedModule),
typeof(AbpCachingModule)
)]
public class AbpSettingManagementDomainModule : AbpModule
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Volo.Abp.SettingManagement
{
//TODO: Convert to AggregateRoot
public class Setting : Entity<Guid>
{
[NotNull]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;

namespace Volo.Abp.SettingManagement
{
[Serializable]
public class SettingCacheItem
{
public string Value { get; set; }

public SettingCacheItem()
{

}

public SettingCacheItem(string value)
{
Value = value;
}

public static string CalculateCacheKey(string name, string providerName, string providerKey)
{
return "pn:" + providerName + ",pk:" + providerKey + ",n:" + name;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Threading.Tasks;
using Volo.Abp.Caching;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Entities.Events;
using Volo.Abp.EventBus;

namespace Volo.Abp.SettingManagement
{
public class SettingCacheItemInvalidator : ILocalEventHandler<EntityChangedEventData<Setting>>, ITransientDependency
{
protected IDistributedCache<SettingCacheItem> Cache { get; }

public SettingCacheItemInvalidator(IDistributedCache<SettingCacheItem> cache)
{
Cache = cache;
}

public virtual async Task HandleEventAsync(EntityChangedEventData<Setting> eventData)
{
var cacheKey = CalculateCacheKey(
eventData.Entity.Name,
eventData.Entity.ProviderName,
eventData.Entity.ProviderKey
);

await Cache.RemoveAsync(cacheKey);
}

protected virtual string CalculateCacheKey(string name, string providerName, string providerKey)
{
return SettingCacheItem.CalculateCacheKey(name, providerName, providerKey);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Caching;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Guids;
using Volo.Abp.Settings;
Expand All @@ -9,49 +10,81 @@ namespace Volo.Abp.SettingManagement
{
public class SettingStore : ISettingStore, ITransientDependency
{
private readonly ISettingRepository _settingRepository;
private readonly IGuidGenerator _guidGenerator;
protected IDistributedCache<SettingCacheItem> Cache { get; }
protected ISettingRepository SettingRepository { get; }
protected IGuidGenerator GuidGenerator { get; }

public SettingStore(ISettingRepository settingRepository, IGuidGenerator guidGenerator)
public SettingStore(
ISettingRepository settingRepository,
IGuidGenerator guidGenerator,
IDistributedCache<SettingCacheItem> cache)
{
_settingRepository = settingRepository;
_guidGenerator = guidGenerator;
SettingRepository = settingRepository;
GuidGenerator = guidGenerator;
Cache = cache;
}

public async Task<string> GetOrNullAsync(string name, string providerName, string providerKey)
{
var setting = await _settingRepository.FindAsync(name, providerName, providerKey);
return setting?.Value;
var cacheItem = await GetCacheItemAsync(name, providerName, providerKey);
return cacheItem.Value;
}

public async Task SetAsync(string name, string value, string providerName, string providerKey)
{
var setting = await _settingRepository.FindAsync(name, providerName, providerKey);
var setting = await SettingRepository.FindAsync(name, providerName, providerKey);
if (setting == null)
{
setting = new Setting(_guidGenerator.Create(), name, value, providerName, providerKey);
await _settingRepository.InsertAsync(setting);
setting = new Setting(GuidGenerator.Create(), name, value, providerName, providerKey);
await SettingRepository.InsertAsync(setting);
}
else
{
setting.Value = value;
await _settingRepository.UpdateAsync(setting);
await SettingRepository.UpdateAsync(setting);
}
}

public async Task<List<SettingValue>> GetListAsync(string providerName, string providerKey)
{
var settings = await _settingRepository.GetListAsync(providerName, providerKey);
var settings = await SettingRepository.GetListAsync(providerName, providerKey);
return settings.Select(s => new SettingValue(s.Name, s.Value)).ToList();
}

public async Task DeleteAsync(string name, string providerName, string providerKey)
{
var setting = await _settingRepository.FindAsync(name, providerName, providerKey);
var setting = await SettingRepository.FindAsync(name, providerName, providerKey);
if (setting != null)
{
await _settingRepository.DeleteAsync(setting);
await SettingRepository.DeleteAsync(setting);
}
}

protected virtual async Task<SettingCacheItem> GetCacheItemAsync(string name, string providerName, string providerKey)
{
var cacheKey = CalculateCacheKey(name, providerName, providerKey);
var cacheItem = await Cache.GetAsync(cacheKey);

if (cacheItem != null)
{
return cacheItem;
}

var setting = await SettingRepository.FindAsync(name, providerName, providerKey);

cacheItem = new SettingCacheItem(setting?.Value);

await Cache.SetAsync(
cacheKey,
cacheItem
);

return cacheItem;
}

protected virtual string CalculateCacheKey(string name, string providerName, string providerKey)
{
return SettingCacheItem.CalculateCacheKey(name, providerName, providerKey);
}
}
}

0 comments on commit 0405476

Please sign in to comment.