Skip to content

Commit

Permalink
💥 Hybridcaching Provider.
Browse files Browse the repository at this point in the history
  • Loading branch information
catcherwong committed Jan 22, 2018
1 parent 3aa72e2 commit 0dadaf3
Show file tree
Hide file tree
Showing 23 changed files with 524 additions and 23 deletions.
7 changes: 7 additions & 0 deletions EasyCaching.sln
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCaching.Demo.Intercepto
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCaching.HybridCache", "src\EasyCaching.HybridCache\EasyCaching.HybridCache.csproj", "{013F6582-CF26-4F5A-BE0B-B383347CF656}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCaching.Demo.HybridCache", "sample\EasyCaching.Demo.HybridCache\EasyCaching.Demo.HybridCache.csproj", "{E287B21A-9025-46D0-B67B-19DA2C0BBC8E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -121,6 +123,10 @@ Global
{013F6582-CF26-4F5A-BE0B-B383347CF656}.Debug|Any CPU.Build.0 = Debug|Any CPU
{013F6582-CF26-4F5A-BE0B-B383347CF656}.Release|Any CPU.ActiveCfg = Release|Any CPU
{013F6582-CF26-4F5A-BE0B-B383347CF656}.Release|Any CPU.Build.0 = Release|Any CPU
{E287B21A-9025-46D0-B67B-19DA2C0BBC8E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E287B21A-9025-46D0-B67B-19DA2C0BBC8E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E287B21A-9025-46D0-B67B-19DA2C0BBC8E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E287B21A-9025-46D0-B67B-19DA2C0BBC8E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{CE61FAA2-0233-451C-991D-4222ED61C84B} = {A0F5CC7E-155F-4726-8DEB-E966950B3FE9}
Expand All @@ -141,5 +147,6 @@ Global
{C343BF4C-F025-4DB6-8A9B-DE8B12DCCAAE} = {F88D727A-9F9C-43D9-90B1-D4A02BF8BC98}
{36D12667-E5C2-4C91-A450-A070DE07A3E6} = {F88D727A-9F9C-43D9-90B1-D4A02BF8BC98}
{013F6582-CF26-4F5A-BE0B-B383347CF656} = {A0F5CC7E-155F-4726-8DEB-E966950B3FE9}
{E287B21A-9025-46D0-B67B-19DA2C0BBC8E} = {F88D727A-9F9C-43D9-90B1-D4A02BF8BC98}
EndGlobalSection
EndGlobal
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";
}
}
}
}
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>
19 changes: 19 additions & 0 deletions sample/EasyCaching.Demo.HybridCache/Program.cs
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();
}
}
72 changes: 72 additions & 0 deletions sample/EasyCaching.Demo.HybridCache/Startup.cs
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 sample/EasyCaching.Demo.HybridCache/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
15 changes: 15 additions & 0 deletions sample/EasyCaching.Demo.HybridCache/appsettings.json
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"
}
}
}
}
5 changes: 4 additions & 1 deletion src/EasyCaching.Core/EasyCaching.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<Owners>Catcher Wong</Owners>
<Authors>Catcher Wong</Authors>
<Version>0.1.1</Version>
<Version>0.1.2</Version>
<Description>
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!
</Description>
Expand All @@ -15,6 +15,9 @@
<ProjectUrl>https://github.com/catcherwong/EasyCaching</ProjectUrl>
<PackageIconUrl>https://raw.githubusercontent.com/catcherwong/EasyCaching/master/media/nuget-icon.png</PackageIconUrl>
<PackageReleaseNotes>
v0.1.2
1. Support HybridCaching Probider.

v0.1.1
1. IEasyCachingSerializer add new APIs in order to support Memcached Transcoder.
2. IEasyCachingProvider add new APIs without data retriever.
Expand Down
18 changes: 10 additions & 8 deletions src/EasyCaching.Core/IEasyCachingSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ public interface IEasyCachingSerializer
/// <typeparam name="T">The 1st type parameter.</typeparam>
byte[] Serialize<T>(T value);

///// <summary>
///// Deserialize the specified bytes and target.
///// </summary>
///// <returns>The deserialize.</returns>
///// <param name="bytes">Bytes.</param>
///// <param name="target">Target.</param>
//object Deserialize(byte[] bytes, Type target);

/// <summary>
/// Deserialize the specified bytes.
/// </summary>
Expand All @@ -31,8 +23,18 @@ public interface IEasyCachingSerializer
/// <typeparam name="T">The 1st type parameter.</typeparam>
T Deserialize<T>(byte[] bytes);

/// <summary>
/// Serializes the object.
/// </summary>
/// <returns>The object.</returns>
/// <param name="obj">Object.</param>
ArraySegment<byte> SerializeObject(object obj);

/// <summary>
/// Deserializes the object.
/// </summary>
/// <returns>The object.</returns>
/// <param name="value">Value.</param>
object DeserializeObject(ArraySegment<byte> value);
}
}
18 changes: 18 additions & 0 deletions src/EasyCaching.Core/Internal/HybridCachingKeyType.cs
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";
}
}
18 changes: 17 additions & 1 deletion src/EasyCaching.HybridCache/EasyCaching.HybridCache.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,28 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Owners>Catcher Wong</Owners>
<Authors>Catcher Wong</Authors>
<Version>0.1.0</Version>
<Description>
EasyCaching.HybridCache contains local caching and distributed caching.
</Description>
<PackageTags>Caching,Cache,Distributed,Memory,Interceptor,Synchronization</PackageTags>
<PackageProjectUrl>https://github.com/catcherwong/EasyCaching</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/catcherwong/EasyCaching/blob/master/LICENSE</PackageLicenseUrl>
<RepositoryUrl>https://github.com/catcherwong/EasyCaching</RepositoryUrl>
<ProjectUrl>https://github.com/catcherwong/EasyCaching</ProjectUrl>
<PackageIconUrl>https://raw.githubusercontent.com/catcherwong/EasyCaching/master/media/nuget-icon.png</PackageIconUrl>
<PackageReleaseNotes>
v0.1.0
1. Init.
</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\EasyCaching.Core\EasyCaching.Core.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Autofac" Version="4.6.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
</ItemGroup>
</Project>
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;
}
}
}
Loading

0 comments on commit 0dadaf3

Please sign in to comment.