-
Notifications
You must be signed in to change notification settings - Fork 329
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3aa72e2
commit 0dadaf3
Showing
23 changed files
with
524 additions
and
23 deletions.
There are no files selected for viewing
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
71 changes: 71 additions & 0 deletions
71
sample/EasyCaching.Demo.HybridCache/Controllers/ValuesController.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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
namespace EasyCaching.Demo.HybridCache.Controllers | ||
{ | ||
using EasyCaching.HybridCache; | ||
using Microsoft.AspNetCore.Mvc; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
[Route("api/[controller]")] | ||
public class ValuesController : Controller | ||
{ | ||
private readonly IHybridCachingProvider _provider; | ||
|
||
public ValuesController(IHybridCachingProvider provider) | ||
{ | ||
this._provider = provider; | ||
} | ||
|
||
// GET api/values/get?type=1 | ||
[HttpGet] | ||
[Route("get")] | ||
public string Get(int type = 1) | ||
{ | ||
if (type == 1) | ||
{ | ||
_provider.Remove("demo"); | ||
return "removed"; | ||
} | ||
else if (type == 2) | ||
{ | ||
_provider.Set("demo", "123", TimeSpan.FromMinutes(1)); | ||
return "seted"; | ||
} | ||
else if (type == 3) | ||
{ | ||
var res = _provider.Get("demo", () => "456", TimeSpan.FromMinutes(1)); | ||
return $"cached value : {res}"; | ||
} | ||
else | ||
{ | ||
return "error"; | ||
} | ||
} | ||
|
||
|
||
// GET api/values/getasync?type=1 | ||
[HttpGet] | ||
[Route("getasync")] | ||
public async Task<string> GetAsync(int type = 1) | ||
{ | ||
if (type == 1) | ||
{ | ||
await _provider.RemoveAsync("demo"); | ||
return "removed"; | ||
} | ||
else if (type == 2) | ||
{ | ||
await _provider.SetAsync("demo", "123", TimeSpan.FromMinutes(1)); | ||
return "seted"; | ||
} | ||
else if (type == 3) | ||
{ | ||
var res = await _provider.GetAsync("demo", async () => await Task.FromResult("456"), TimeSpan.FromMinutes(1)); | ||
return $"cached value : {res}"; | ||
} | ||
else | ||
{ | ||
return "error"; | ||
} | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
sample/EasyCaching.Demo.HybridCache/EasyCaching.Demo.HybridCache.csproj
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" /> | ||
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.2.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\EasyCaching.Core\EasyCaching.Core.csproj" /> | ||
<ProjectReference Include="..\..\src\EasyCaching.HybridCache\EasyCaching.HybridCache.csproj" /> | ||
<ProjectReference Include="..\..\src\EasyCaching.InMemory\EasyCaching.InMemory.csproj" /> | ||
<ProjectReference Include="..\..\src\EasyCaching.Redis\EasyCaching.Redis.csproj" /> | ||
</ItemGroup> | ||
</Project> |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace EasyCaching.Demo.HybridCache | ||
{ | ||
using Microsoft.AspNetCore; | ||
using Microsoft.AspNetCore.Hosting; | ||
|
||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
BuildWebHost(args).Run(); | ||
} | ||
|
||
public static IWebHost BuildWebHost(string[] args) => | ||
WebHost.CreateDefaultBuilder(args) | ||
.UseStartup<Startup>() | ||
.UseUrls("http://localhost:9008") | ||
.Build(); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
namespace EasyCaching.Demo.HybridCache | ||
{ | ||
using Autofac; | ||
using Autofac.Extensions.DependencyInjection; | ||
using EasyCaching.Core.Internal; | ||
using EasyCaching.HybridCache; | ||
using EasyCaching.InMemory; | ||
using EasyCaching.Redis; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
using EasyCaching.Core; | ||
using System.Collections.Generic; | ||
|
||
public class Startup | ||
{ | ||
public Startup(IConfiguration configuration) | ||
{ | ||
Configuration = configuration; | ||
} | ||
|
||
public IConfiguration Configuration { get; } | ||
|
||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddMvc(); | ||
|
||
services.AddDefaultInMemoryCacheForHybrid(); | ||
services.AddDefaultRedisCacheForHybrid(option => | ||
{ | ||
option.Endpoints.Add(new ServerEndPoint("127.0.0.1", 6379)); | ||
option.Password = ""; | ||
}); | ||
|
||
services.AddDefaultHybridCache(); | ||
|
||
services.AddSingleton(factory => | ||
{ | ||
Func<string, IEasyCachingProvider> accesor = key => | ||
{ | ||
if(key.Equals(HybridCachingKeyType.LocalKey)) | ||
{ | ||
return factory.GetService<InMemoryCachingProvider>(); | ||
} | ||
else if(key.Equals(HybridCachingKeyType.DistributedKey)) | ||
{ | ||
return factory.GetService<DefaultRedisCachingProvider>(); | ||
} | ||
else | ||
{ | ||
throw new KeyNotFoundException(); | ||
} | ||
}; | ||
return accesor; | ||
}); | ||
|
||
|
||
} | ||
|
||
public void Configure(IApplicationBuilder app, IHostingEnvironment env) | ||
{ | ||
if (env.IsDevelopment()) | ||
{ | ||
app.UseDeveloperExceptionPage(); | ||
} | ||
|
||
app.UseMvc(); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
sample/EasyCaching.Demo.HybridCache/appsettings.Development.json
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"Logging": { | ||
"IncludeScopes": false, | ||
"LogLevel": { | ||
"Default": "Debug", | ||
"System": "Information", | ||
"Microsoft": "Information" | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"Logging": { | ||
"IncludeScopes": false, | ||
"Debug": { | ||
"LogLevel": { | ||
"Default": "Warning" | ||
} | ||
}, | ||
"Console": { | ||
"LogLevel": { | ||
"Default": "Warning" | ||
} | ||
} | ||
} | ||
} |
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
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
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace EasyCaching.Core.Internal | ||
{ | ||
/// <summary> | ||
/// Autofac key type. | ||
/// </summary> | ||
public class HybridCachingKeyType | ||
{ | ||
/// <summary> | ||
/// The local key. | ||
/// </summary> | ||
public const string LocalKey = "Local"; | ||
|
||
/// <summary> | ||
/// The distributed key. | ||
/// </summary> | ||
public const string DistributedKey = "Distributed"; | ||
} | ||
} |
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
26 changes: 26 additions & 0 deletions
26
src/EasyCaching.HybridCache/HybridCacheServiceCollectionExtensions.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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
namespace EasyCaching.HybridCache | ||
{ | ||
using EasyCaching.Core.Internal; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
|
||
/// <summary> | ||
/// Hybrid cache service collection extensions. | ||
/// </summary> | ||
public static class HybridCacheServiceCollectionExtensions | ||
{ | ||
/// <summary> | ||
/// Adds the default hybrid cache. | ||
/// </summary> | ||
/// <returns>The default hybrid cache.</returns> | ||
/// <param name="services">Services.</param> | ||
public static IServiceCollection AddDefaultHybridCache(this IServiceCollection services) | ||
{ | ||
ArgumentCheck.NotNull(services, nameof(services)); | ||
|
||
services.TryAddSingleton<IHybridCachingProvider, HybridCachingProvider>(); | ||
|
||
return services; | ||
} | ||
} | ||
} |
Oops, something went wrong.