Skip to content

Commit

Permalink
Merge pull request #24 from dotnetcore/dev
Browse files Browse the repository at this point in the history
Update InterceptorServiceCollectionExtensions And README.md
  • Loading branch information
catcherwong authored Mar 21, 2018
2 parents 5abc3d6 + 5cd4189 commit 94f2573
Show file tree
Hide file tree
Showing 13 changed files with 412 additions and 115 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ osx_image: xcode9.1
branches:
only:
- master
- dev

script:
- if test "$TRAVIS_OS_NAME" == "linux"; then dotnet restore; fi
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
![](https://raw.githubusercontent.com/catcherwong/EasyCaching/master/media/easycaching-icon.png)
![](media/easycaching-icon.png?raw=true)

EasyCaching is a open source caching library that contains basic usages and some advanced usages of caching which can help us to handle caching more easier!

[![Coverage Status](https://coveralls.io/repos/github/catcherwong/EasyCaching/badge.svg?branch=master)](https://coveralls.io/github/catcherwong/EasyCaching?branch=master)

[![GitHub license](https://img.shields.io/github/license/catcherwong/EasyCaching.svg)](https://github.com/catcherwong/EasyCaching/blob/master/LICENSE)
[![Member project of .NET China Foundation](https://img.shields.io/badge/member_project_of-.NET_CHINA-red.svg?style=flat&colorB=9E20C8)](https://github.com/dotnetcore)
[![GitHub license](https://img.shields.io/github/license/dotnetcore/EasyCaching.svg)](https://github.com/dotnetcore/EasyCaching/blob/master/LICENSE)

## CI Build Status

| Platform | Build Server | Status |
|--------- |------------- |---------|
| AppVeyor | Windows |[![Build status](https://ci.appveyor.com/api/projects/status/ji7513h4uv4ysq2i?svg=true)](https://ci.appveyor.com/project/catcherwong/easycaching) |
| Travis | Linux/OSX | [![Build Status](https://travis-ci.org/catcherwong/EasyCaching.svg?branch=master)](https://travis-ci.org/catcherwong/EasyCaching) |
| AppVeyor | Windows |[![Build status](https://ci.appveyor.com/api/projects/status/4x6qal9c1r10wn6x?svg=true)](https://ci.appveyor.com/project/catcherwong/easycaching-48okb) |
| Travis | Linux/OSX | [![Build Status](https://travis-ci.org/dotnetcore/EasyCaching.svg?branch=master)](https://travis-ci.org/dotnetcore/EasyCaching) |

## Nuget Packages

Expand Down
23 changes: 23 additions & 0 deletions sample/EasyCaching.Demo.Interceptor.AspectCore/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using EasyCaching.Demo.Interceptor.AspectCore.Services;
using EasyCaching.InMemory;
using EasyCaching.Interceptor.AspectCore;
using global::AspectCore.Configuration;
using global::AspectCore.Injector;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
Expand All @@ -26,7 +28,28 @@ public IServiceProvider ConfigureServices(IServiceCollection services)

services.AddMvc();

//1. all default
return services.ConfigureAspectCoreInterceptor();

//2. default and customize
//Action<IServiceContainer> action = x =>
//{
// x.AddType<IDateTimeService, DateTimeService>();
//};

//return services.ConfigureAspectCoreInterceptor(action);

//3. all customize
//Action<IServiceContainer> action = x =>
//{
// x.AddType<IDateTimeService, DateTimeService>();
// x.Configure(config =>
// {
// config.Interceptors.AddTyped<EasyCachingInterceptor>(method => typeof(Core.Internal.IEasyCaching).IsAssignableFrom(method.DeclaringType));
// });
//};

//return services.ConfigureAspectCoreInterceptor(action, true);
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
Expand Down
36 changes: 33 additions & 3 deletions sample/EasyCaching.Demo.Interceptor.Castle/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
namespace EasyCaching.Demo.Interceptor.Castle
{
{
using Autofac;
using Autofac.Extras.DynamicProxy;
using EasyCaching.InMemory;
using EasyCaching.Interceptor.Castle;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;

using System;
using System.Reflection;

public class Startup
{
public Startup(IConfiguration configuration)
Expand All @@ -25,7 +28,34 @@ public IServiceProvider ConfigureServices(IServiceCollection services)

services.AddDefaultInMemoryCache();

//1. all default
return services.ConfigureCastleInterceptor();

//2. default and customize
//Action<ContainerBuilder> action = x =>
//{
// x.RegisterType<DateTimeService>().As<IDateTimeService>();
//};

//return services.ConfigureCastleInterceptor(action);

//3. all customize
//Action<ContainerBuilder> action = x =>
//{
// x.RegisterType<DateTimeService>().As<IDateTimeService>();

// var assembly = Assembly.GetExecutingAssembly();
// x.RegisterType<EasyCachingInterceptor>();

// x.RegisterAssemblyTypes(assembly)
// .Where(type => typeof(Core.Internal.IEasyCaching).IsAssignableFrom(type) && !type.GetTypeInfo().IsAbstract)
// .AsImplementedInterfaces()
// .InstancePerLifetimeScope()
// .EnableInterfaceInterceptors()
// .InterceptedBy(typeof(EasyCachingInterceptor));
//};

//return services.ConfigureCastleInterceptor(action, true);
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
Expand Down
30 changes: 17 additions & 13 deletions src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public async Task<CacheValue<T>> GetAsync<T>(string cacheKey) where T : class
{
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

var result = await Task.Run(() => { return _cache.Get(cacheKey) as T; });
var result = await Task.FromResult((T)_cache.Get(cacheKey));

if (result != null)
return new CacheValue<T>(result, true);
Expand Down Expand Up @@ -161,9 +161,11 @@ public async Task RemoveAsync(string cacheKey)
{
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

await Task.Run(() => _cache.Remove(cacheKey));

_cacheKeys.TryRemove(cacheKey);
await Task.Run(() =>
{
_cache.Remove(cacheKey);
_cacheKeys.TryRemove(cacheKey);
});
}

/// <summary>
Expand Down Expand Up @@ -200,9 +202,11 @@ public async Task SetAsync<T>(string cacheKey, T cacheValue, TimeSpan expiration
ArgumentCheck.NotNull(cacheValue, nameof(cacheValue));
ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration));

await Task.Run(() => _cache.Set(cacheKey, cacheValue, expiration));

_cacheKeys.Add(cacheKey);
await Task.Run(() =>
{
_cache.Set(cacheKey, cacheValue, expiration);
_cacheKeys.Add(cacheKey);
});
}

/// <summary>
Expand All @@ -226,7 +230,7 @@ public async Task<bool> ExistsAsync(string cacheKey)
{
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

return await Task.Run(() => { return _cache.TryGetValue(cacheKey, out object value); });
return await Task.FromResult(_cache.TryGetValue(cacheKey, out object value));
}

/// <summary>
Expand Down Expand Up @@ -272,8 +276,8 @@ public void RemoveByPrefix(string prefix)
{
ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix));

var keys = _cacheKeys.Where(x => x.StartsWith(prefix.Trim(),StringComparison.OrdinalIgnoreCase));
if(keys.Count()>0)
var keys = _cacheKeys.Where(x => x.StartsWith(prefix.Trim(), StringComparison.OrdinalIgnoreCase));
if (keys.Count() > 0)
{
foreach (var item in keys)
{
Expand Down Expand Up @@ -331,7 +335,7 @@ public async Task SetAllAsync<T>(IDictionary<string, T> values, TimeSpan expirat

var tasks = new List<Task>();
foreach (var entry in values)
tasks.Add(SetAsync(entry.Key, entry.Value,expiration));
tasks.Add(SetAsync(entry.Key, entry.Value, expiration));

await Task.WhenAll(tasks);
}
Expand Down Expand Up @@ -386,7 +390,7 @@ public IDictionary<string, CacheValue<T>> GetByPrefix<T>(string prefix) where T

var keys = _cacheKeys.Where(x => x.StartsWith(prefix.Trim(), StringComparison.OrdinalIgnoreCase));
if (keys.Count() > 0)
{
{
foreach (var item in keys)
{
map[item] = this.Get<T>(item);
Expand All @@ -402,7 +406,7 @@ public IDictionary<string, CacheValue<T>> GetByPrefix<T>(string prefix) where T
/// <param name="prefix">Prefix.</param>
/// <typeparam name="T">The 1st type parameter.</typeparam>
public Task<IDictionary<string, CacheValue<T>>> GetByPrefixAsync<T>(string prefix) where T : class
{
{
ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix));

var keys = _cacheKeys.Where(x => x.StartsWith(prefix.Trim(), StringComparison.OrdinalIgnoreCase));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using EasyCaching.Core;
using EasyCaching.Core.Internal;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;

/// <summary>
Expand All @@ -14,41 +15,65 @@
public static class AspectCoreInterceptorServiceCollectionExtensions
{
/// <summary>
/// Configures the easy caching.
/// Configures the AspectCore interceptor.
/// </summary>
/// <returns>The easy caching.</returns>
/// <returns>The aspect core interceptor.</returns>
/// <param name="services">Services.</param>
public static IServiceProvider ConfigureAspectCoreInterceptor(this IServiceCollection services)
{
services.TryAddSingleton<IEasyCachingKeyGenerator,DefaultEasyCachingKeyGenerator>();

services.AddSingleton<IEasyCachingKeyGenerator,DefaultEasyCachingKeyGenerator>();
var container = services.ToServiceContainer();

container.Configure(config =>
return container.Configure(config =>
{
config.Interceptors.AddTyped<EasyCachingInterceptor>(method => typeof(IEasyCaching).IsAssignableFrom(method.DeclaringType));
});

return container.Build();
}).Build();
}

/// <summary>
/// Configures the easy caching.
/// Configures the AspectCore interceptor.
/// </summary>
/// <returns>The easy caching.</returns>
/// <returns>The aspect core interceptor.</returns>
/// <param name="services">Services.</param>
/// <param name="action">Action.</param>
public static IServiceProvider ConfigureAspectCoreInterceptor(this IServiceCollection services, Action<IAspectConfiguration> action)
public static IServiceProvider ConfigureAspectCoreInterceptor(this IServiceCollection services, Action<IServiceContainer> action)
{
services.TryAddSingleton<IEasyCachingKeyGenerator, DefaultEasyCachingKeyGenerator>();

var container = services.ToServiceContainer();

container.Configure(config =>
action(container);

return container.Configure(config =>
{
config.Interceptors.AddTyped<EasyCachingInterceptor>(method => typeof(IEasyCaching).IsAssignableFrom(method.DeclaringType));
action(config);
});
}).Build();
}

return container.Build();
/// <summary>
/// Configures the aspect core interceptor.
/// </summary>
/// <returns>The aspect core interceptor.</returns>
/// <param name="services">Services.</param>
/// <param name="action">Action.</param>
/// <param name="isRemoveDefault">If set to <c>true</c> is remove default.</param>
public static IServiceProvider ConfigureAspectCoreInterceptor(this IServiceCollection services, Action<IServiceContainer> action, bool isRemoveDefault)
{
if (isRemoveDefault)
{
services.TryAddSingleton<IEasyCachingKeyGenerator, DefaultEasyCachingKeyGenerator>();

var container = services.ToServiceContainer();

action(container);

return container.Build();
}
else
{
return services.ConfigureAspectCoreInterceptor(action);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using EasyCaching.Core;
using EasyCaching.Core.Internal;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;
using System.Reflection;

Expand All @@ -21,7 +22,7 @@ public static class CastleInterceptorServiceCollectionExtensions
/// <param name="services">Services.</param>
public static IServiceProvider ConfigureCastleInterceptor(this IServiceCollection services)
{
services.AddSingleton<IEasyCachingKeyGenerator, DefaultEasyCachingKeyGenerator>();
services.TryAddSingleton<IEasyCachingKeyGenerator, DefaultEasyCachingKeyGenerator>();

var builder = new ContainerBuilder();
builder.Populate(services);
Expand All @@ -37,6 +38,58 @@ public static IServiceProvider ConfigureCastleInterceptor(this IServiceCollectio
.InterceptedBy(typeof(EasyCachingInterceptor));

return new AutofacServiceProvider(builder.Build());
}
}

/// <summary>
/// Configures the castle interceptor.
/// </summary>
/// <returns>The castle interceptor.</returns>
/// <param name="services">Services.</param>
/// <param name="action">Action.</param>
public static IServiceProvider ConfigureCastleInterceptor(this IServiceCollection services, Action<ContainerBuilder> action)
{
services.TryAddSingleton<IEasyCachingKeyGenerator, DefaultEasyCachingKeyGenerator>();

var builder = new ContainerBuilder();
builder.Populate(services);

var assembly = Assembly.GetCallingAssembly();
builder.RegisterType<EasyCachingInterceptor>();

builder.RegisterAssemblyTypes(assembly)
.Where(type => typeof(IEasyCaching).IsAssignableFrom(type) && !type.GetTypeInfo().IsAbstract)
.AsImplementedInterfaces()
.InstancePerLifetimeScope()
.EnableInterfaceInterceptors()
.InterceptedBy(typeof(EasyCachingInterceptor));

action(builder);

return new AutofacServiceProvider(builder.Build());
}

/// <summary>
/// Configures the castle interceptor.
/// </summary>
/// <returns>The castle interceptor.</returns>
/// <param name="services">Services.</param>
/// <param name="action">Action.</param>
/// <param name="isRemoveDefault">If set to <c>true</c> is remove default.</param>
public static IServiceProvider ConfigureCastleInterceptor(this IServiceCollection services, Action<ContainerBuilder> action, bool isRemoveDefault)
{
if (isRemoveDefault)
{
services.TryAddSingleton<IEasyCachingKeyGenerator, DefaultEasyCachingKeyGenerator>();

var builder = new ContainerBuilder();
builder.Populate(services);
action(builder);
return new AutofacServiceProvider(builder.Build());
}
else
{
return services.ConfigureCastleInterceptor(action);
}
}
}
}
Loading

0 comments on commit 94f2573

Please sign in to comment.