Browse Source

Implement CacheNameAttribute and multitenancy for caching.

pull/216/head
Halil İbrahim Kalkan 9 years ago
parent
commit
972abb7629
  1. 1
      src/Volo.Abp.Caching/Volo.Abp.Caching.csproj
  2. 2
      src/Volo.Abp.Caching/Volo/Abp/Caching/AbpCachingModule.cs
  3. 16
      src/Volo.Abp.Caching/Volo/Abp/Caching/CacheNameAttribute.cs
  4. 74
      src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCache.cs
  5. 10
      src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/MultiTenancy/IgnoreMultiTenancy.cs

1
src/Volo.Abp.Caching/Volo.Abp.Caching.csproj

@ -18,6 +18,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Volo.Abp.MultiTenancy.Abstractions\Volo.Abp.MultiTenancy.Abstractions.csproj" />
<ProjectReference Include="..\Volo.Abp.Serialization\Volo.Abp.Serialization.csproj" />
<ProjectReference Include="..\Volo.Abp.Threading\Volo.Abp.Threading.csproj" />
</ItemGroup>

2
src/Volo.Abp.Caching/Volo/Abp/Caching/AbpCachingModule.cs

@ -1,5 +1,6 @@
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Modularity;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Serialization;
using Volo.Abp.Threading;
@ -7,6 +8,7 @@ namespace Volo.Abp.Caching
{
[DependsOn(typeof(AbpThreadingModule))]
[DependsOn(typeof(AbpSerializationModule))]
[DependsOn(typeof(AbpMultiTenancyAbstractionsModule))]
public class AbpCachingModule : AbpModule
{
public override void ConfigureServices(IServiceCollection services)

16
src/Volo.Abp.Caching/Volo/Abp/Caching/CacheNameAttribute.cs

@ -0,0 +1,16 @@
using JetBrains.Annotations;
namespace Volo.Abp.Caching
{
public class CacheNameAttribute
{
public string Name { get; }
public CacheNameAttribute([NotNull] string name)
{
Check.NotNull(name, nameof(name));
Name = name;
}
}
}

74
src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCache.cs

@ -1,7 +1,9 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Distributed;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Serialization;
using Volo.Abp.Threading;
@ -10,23 +12,35 @@ namespace Volo.Abp.Caching
public class DistributedCache<TCacheItem> : IDistributedCache<TCacheItem>
where TCacheItem : class
{
protected string CacheName { get; set; }
protected bool IgnoreMultiTenancy { get; set; }
protected IDistributedCache Cache { get; }
protected ICancellationTokenProvider CancellationTokenProvider { get; }
protected IObjectSerializer ObjectSerializer { get; }
protected ICurrentTenant CurrentTenant { get; }
public DistributedCache(
IDistributedCache cache,
ICancellationTokenProvider cancellationTokenProvider,
IObjectSerializer objectSerializer)
IObjectSerializer objectSerializer,
ICurrentTenant currentTenant)
{
Cache = cache;
CancellationTokenProvider = cancellationTokenProvider;
ObjectSerializer = objectSerializer;
CurrentTenant = currentTenant;
SetDefaultOptions();
}
public TCacheItem Get(string key)
public virtual TCacheItem Get(string key)
{
var cachedBytes = Cache.Get(key);
var cachedBytes = Cache.Get(NormalizeKey(key));
if (cachedBytes == null)
{
return null;
@ -35,9 +49,9 @@ namespace Volo.Abp.Caching
return ObjectSerializer.Deserialize<TCacheItem>(cachedBytes);
}
public async Task<TCacheItem> GetAsync(string key, CancellationToken token = default)
public virtual async Task<TCacheItem> GetAsync(string key, CancellationToken token = default)
{
var cachedBytes = await Cache.GetAsync(key, CancellationTokenProvider.FallbackToProvider(token));
var cachedBytes = await Cache.GetAsync(NormalizeKey(key), CancellationTokenProvider.FallbackToProvider(token));
if (cachedBytes == null)
{
return null;
@ -46,43 +60,69 @@ namespace Volo.Abp.Caching
return ObjectSerializer.Deserialize<TCacheItem>(cachedBytes);
}
public void Set(string key, TCacheItem value, DistributedCacheEntryOptions options)
public virtual void Set(string key, TCacheItem value, DistributedCacheEntryOptions options)
{
Cache.Set(
key,
NormalizeKey(key),
ObjectSerializer.Serialize(value),
options ?? new DistributedCacheEntryOptions { SlidingExpiration = TimeSpan.FromMinutes(20) } //TODO: implement per cache item and global defaults!!!
);
}
public Task SetAsync(string key, TCacheItem value, DistributedCacheEntryOptions options = null, CancellationToken token = default)
public virtual Task SetAsync(string key, TCacheItem value, DistributedCacheEntryOptions options = null, CancellationToken token = default)
{
return Cache.SetAsync(
key,
NormalizeKey(key),
ObjectSerializer.Serialize(value),
options ?? new DistributedCacheEntryOptions { SlidingExpiration = TimeSpan.FromMinutes(20) }, //TODO: implement per cache item and global defaults!!!
CancellationTokenProvider.FallbackToProvider(token)
);
}
public void Refresh(string key)
public virtual void Refresh(string key)
{
Cache.Refresh(NormalizeKey(key));
}
public virtual Task RefreshAsync(string key, CancellationToken token = default)
{
Cache.Refresh(key);
return Cache.RefreshAsync(NormalizeKey(key), CancellationTokenProvider.FallbackToProvider(token));
}
public Task RefreshAsync(string key, CancellationToken token = default)
public virtual void Remove(string key)
{
return Cache.RefreshAsync(key, CancellationTokenProvider.FallbackToProvider(token));
Cache.Remove(NormalizeKey(key));
}
public void Remove(string key)
public virtual Task RemoveAsync(string key, CancellationToken token = default)
{
Cache.Remove(key);
return Cache.RemoveAsync(NormalizeKey(key), CancellationTokenProvider.FallbackToProvider(token));
}
public Task RemoveAsync(string key, CancellationToken token = default)
protected virtual string NormalizeKey(string key)
{
return Cache.RemoveAsync(key, CancellationTokenProvider.FallbackToProvider(token));
var normalizedKey = "c:" + CacheName + ",k:" + key;
if (!IgnoreMultiTenancy && CurrentTenant.Id.HasValue)
{
normalizedKey = "t:" + CurrentTenant.Id.Value + "," + normalizedKey;
}
return normalizedKey;
}
protected virtual void SetDefaultOptions()
{
//CacheName
var cacheNameAttribute = typeof(TCacheItem)
.GetCustomAttributes(true)
.OfType<CacheNameAttribute>()
.FirstOrDefault();
CacheName = cacheNameAttribute != null ? cacheNameAttribute.Name : typeof(TCacheItem).Name;
//IgnoreMultiTenancy
IgnoreMultiTenancy = typeof(TCacheItem).IsDefined(typeof(IgnoreMultiTenancyAttribute), true);
}
}
}

10
src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/MultiTenancy/IgnoreMultiTenancy.cs

@ -0,0 +1,10 @@
using System;
namespace Volo.Abp.MultiTenancy
{
[AttributeUsage(AttributeTargets.All)]
public class IgnoreMultiTenancyAttribute : Attribute
{
}
}
Loading…
Cancel
Save