Skip to content

Commit 48db17b

Browse files
committed
1. Add SQL Async OOB pprovider
2. Add kernel cache support 3. Make the cache objects to be serializable
1 parent ac173be commit 48db17b

18 files changed

Lines changed: 775 additions & 55 deletions

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
.vs/
44
msbuild.*
55
obj/
6-
packages/
6+
packages/
7+
/TestSQL
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace Microsoft.AspNet.OutputCache.SQLAsyncOutputCacheProvider {
2+
using System.IO;
3+
using System.Runtime.Serialization.Formatters.Binary;
4+
static class BinarySerializer {
5+
public static byte[] Serialize(object data) {
6+
if (data == null) {
7+
data = new object();
8+
}
9+
var binaryFormatter = new BinaryFormatter();
10+
using (var memoryStream = new MemoryStream()) {
11+
binaryFormatter.Serialize(memoryStream, data);
12+
byte[] objectDataAsStream = memoryStream.ToArray();
13+
return objectDataAsStream;
14+
}
15+
}
16+
17+
public static object Deserialize(byte[] data) {
18+
if (data == null) {
19+
return null;
20+
}
21+
var binaryFormatter = new BinaryFormatter();
22+
using (var memoryStream = new MemoryStream(data, 0, data.Length)) {
23+
memoryStream.Seek(0, SeekOrigin.Begin);
24+
object retObject = (object)binaryFormatter.Deserialize(memoryStream);
25+
return retObject;
26+
}
27+
}
28+
}
29+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props" Condition="Exists('..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" />
4+
<Import Project="..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props" Condition="Exists('..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props')" />
5+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
6+
<PropertyGroup>
7+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
8+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
9+
<ProductVersion>
10+
</ProductVersion>
11+
<SchemaVersion>2.0</SchemaVersion>
12+
<ProjectGuid>{062FD141-4E51-4943-8C69-385DDE3D2792}</ProjectGuid>
13+
<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
14+
<OutputType>Library</OutputType>
15+
<AppDesignerFolder>Properties</AppDesignerFolder>
16+
<RootNamespace>Microsoft.AspNet.OutputCache.SQLAsyncOutputCacheProvider</RootNamespace>
17+
<AssemblyName>Microsoft.AspNet.OutputCache.SQLAsyncOutputCacheProvider</AssemblyName>
18+
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
19+
<UseIISExpress>true</UseIISExpress>
20+
<IISExpressSSLPort />
21+
<IISExpressAnonymousAuthentication />
22+
<IISExpressWindowsAuthentication />
23+
<IISExpressUseClassicPipelineMode />
24+
<UseGlobalApplicationHostFile />
25+
<NuGetPackageImportStamp>
26+
</NuGetPackageImportStamp>
27+
</PropertyGroup>
28+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
29+
<DebugSymbols>true</DebugSymbols>
30+
<DebugType>full</DebugType>
31+
<Optimize>false</Optimize>
32+
<OutputPath>bin\</OutputPath>
33+
<DefineConstants>DEBUG;TRACE</DefineConstants>
34+
<ErrorReport>prompt</ErrorReport>
35+
<WarningLevel>4</WarningLevel>
36+
</PropertyGroup>
37+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
38+
<DebugType>pdbonly</DebugType>
39+
<Optimize>true</Optimize>
40+
<OutputPath>bin\</OutputPath>
41+
<DefineConstants>TRACE</DefineConstants>
42+
<ErrorReport>prompt</ErrorReport>
43+
<WarningLevel>4</WarningLevel>
44+
</PropertyGroup>
45+
<ItemGroup>
46+
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
47+
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
48+
<Private>True</Private>
49+
</Reference>
50+
<Reference Include="System.Data" />
51+
<Reference Include="System.Runtime.Caching" />
52+
<Reference Include="System" />
53+
<Reference Include="System.Web" />
54+
<Reference Include="System.Configuration" />
55+
</ItemGroup>
56+
<ItemGroup>
57+
<Compile Include="BinarySerializer.cs" />
58+
<Compile Include="SQLAsyncOutputCacheProvider.cs" />
59+
<Compile Include="Properties\AssemblyInfo.cs" />
60+
<Compile Include="SQLHelper.cs" />
61+
</ItemGroup>
62+
<ItemGroup>
63+
<Content Include="packages.config" />
64+
</ItemGroup>
65+
<ItemGroup>
66+
<ProjectReference Include="..\src\OutputCacheModuleAsync\Microsoft.AspNet.OutputCache.OutputCacheModuleAsync.csproj">
67+
<Project>{3b446e33-7b1c-4a32-aeb8-92dc6ce94f77}</Project>
68+
<Name>Microsoft.AspNet.OutputCache.OutputCacheModuleAsync</Name>
69+
</ProjectReference>
70+
</ItemGroup>
71+
<PropertyGroup>
72+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
73+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
74+
</PropertyGroup>
75+
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
76+
<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
77+
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />
78+
<ProjectExtensions>
79+
<VisualStudio>
80+
<FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}">
81+
<WebProjectProperties>
82+
<UseIIS>True</UseIIS>
83+
<AutoAssignPort>True</AutoAssignPort>
84+
<DevelopmentServerPort>18424</DevelopmentServerPort>
85+
<DevelopmentServerVPath>/</DevelopmentServerVPath>
86+
<IISUrl>http://localhost:18424/</IISUrl>
87+
<NTLMAuthentication>False</NTLMAuthentication>
88+
<UseCustomServer>False</UseCustomServer>
89+
<CustomServerUrl>
90+
</CustomServerUrl>
91+
<SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
92+
</WebProjectProperties>
93+
</FlavorProperties>
94+
</VisualStudio>
95+
</ProjectExtensions>
96+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
97+
<PropertyGroup>
98+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
99+
</PropertyGroup>
100+
<Error Condition="!Exists('..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props'))" />
101+
<Error Condition="!Exists('..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props'))" />
102+
</Target>
103+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
104+
Other similar extension points exist, see Microsoft.Common.targets.
105+
<Target Name="BeforeBuild">
106+
</Target>
107+
<Target Name="AfterBuild">
108+
</Target>
109+
-->
110+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<UseIISExpress>true</UseIISExpress>
5+
<ProjectView>ProjectFiles</ProjectView>
6+
</PropertyGroup>
7+
<ProjectExtensions>
8+
<VisualStudio>
9+
<FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}">
10+
<WebProjectProperties>
11+
<StartPageUrl>
12+
</StartPageUrl>
13+
<StartAction>CurrentPage</StartAction>
14+
<AspNetDebugging>True</AspNetDebugging>
15+
<SilverlightDebugging>False</SilverlightDebugging>
16+
<NativeDebugging>False</NativeDebugging>
17+
<SQLDebugging>False</SQLDebugging>
18+
<ExternalProgram>
19+
</ExternalProgram>
20+
<StartExternalURL>
21+
</StartExternalURL>
22+
<StartCmdLineArguments>
23+
</StartCmdLineArguments>
24+
<StartWorkingDirectory>
25+
</StartWorkingDirectory>
26+
<EnableENC>True</EnableENC>
27+
<AlwaysStartWebServerOnDebug>True</AlwaysStartWebServerOnDebug>
28+
</WebProjectProperties>
29+
</FlavorProperties>
30+
</VisualStudio>
31+
</ProjectExtensions>
32+
</Project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("Microsoft.AspNet.OutputCache.SQLOutputCacheAsyncProvider")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("Microsoft.AspNet.OutputCache.SQLOutputCacheAsyncProvider")]
13+
[assembly: AssemblyCopyright("Copyright © 2017")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("062fd141-4e51-4943-8c69-385dde3d2792")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Revision and Build Numbers
33+
// by using the '*' as shown below:
34+
[assembly: AssemblyVersion("1.0.0.0")]
35+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
namespace Microsoft.AspNet.OutputCache.SQLAsyncOutputCacheProvider {
2+
using OutputCache;
3+
using System;
4+
using System.Collections.Specialized;
5+
using System.Runtime.Caching;
6+
using System.Threading.Tasks;
7+
using System.Web.Caching;
8+
9+
/// <summary>
10+
/// Async OutputCache Provider using SQL as storage.
11+
/// </summary>
12+
public class SQLAsyncOutputCacheProvider : OutputCacheProviderAsync, ICacheDependencyHandler {
13+
14+
static SQLHelper sqlUtilityHelper;
15+
public override void Initialize(string name, NameValueCollection config) {
16+
if (config == null) {
17+
throw new ArgumentNullException("config");
18+
}
19+
if (String.IsNullOrEmpty(name)) {
20+
name = "SqlAsyncOutputCacheProvider";
21+
}
22+
base.Initialize(name, config);
23+
sqlUtilityHelper = new SQLHelper(config);
24+
}
25+
26+
#region async methods
27+
/// <summary>
28+
/// Asynchronously inserts the specified entry into the output cache.
29+
/// </summary>
30+
/// <param name="key"></param>
31+
/// <param name="entry"></param>
32+
/// <param name="utcExpiry"></param>
33+
/// <returns></returns>
34+
public override async Task<object> AddAsync(string key, object entry, DateTime utcExpiry) {
35+
return await sqlUtilityHelper.AddAsync(key, entry, utcExpiry);
36+
}
37+
38+
/// <summary>
39+
/// Asynchronously returns a reference to the specified entry in the output cache.
40+
/// </summary>
41+
/// <param name="key"></param>
42+
/// <returns></returns>
43+
public override async Task<object> GetAsync(string key) {
44+
return await sqlUtilityHelper.GetAsync(key);
45+
}
46+
47+
/// <summary>
48+
/// Asynchronously Inserts the specified entry into the output cache, overwriting the entry if it is already cached.
49+
/// </summary>
50+
/// <param name="key"></param>
51+
/// <param name="entry"></param>
52+
/// <param name="utcExpiry"></param>
53+
/// <returns></returns>
54+
public override async Task SetAsync(string key, object entry, DateTime utcExpiry) {
55+
await sqlUtilityHelper.SetAsync(key, entry, utcExpiry);
56+
}
57+
58+
/// <summary>
59+
/// Asynchronously removes the specified entry from the output cache.
60+
/// </summary>
61+
/// <param name="key"></param>
62+
/// <returns></returns>
63+
public override async Task RemoveAsync(string key) {
64+
await sqlUtilityHelper.RemoveAsync(key);
65+
}
66+
#endregion
67+
68+
#region sync methods
69+
/// <summary>
70+
/// Returns a reference to the specified entry in the output cache.
71+
/// </summary>
72+
/// <param name="key"></param>
73+
/// <returns></returns>
74+
public override object Get(string key) {
75+
return GetAsync(key);
76+
}
77+
78+
/// <summary>
79+
/// Inserts the specified entry into the output cache.
80+
/// </summary>
81+
/// <param name="key"></param>
82+
/// <param name="entry"></param>
83+
/// <param name="utcExpiry"></param>
84+
/// <returns></returns>
85+
public override object Add(string key, object entry, DateTime utcExpiry) {
86+
return AddAsync(key, entry, utcExpiry);
87+
}
88+
89+
/// <summary>
90+
/// Inserts the specified entry into the output cache, overwriting the entry if it is already cached
91+
/// </summary>
92+
/// <param name="key"></param>
93+
/// <param name="entry"></param>
94+
/// <param name="utcExpiry"></param>
95+
public override void Set(string key, object entry, DateTime utcExpiry) {
96+
SetAsync(key, entry, utcExpiry);
97+
}
98+
99+
/// <summary>
100+
/// Removes the specified entry from the output cache.
101+
/// </summary>
102+
/// <param name="key"></param>
103+
public override void Remove(string key) {
104+
RemoveAsync(key);
105+
}
106+
#endregion
107+
108+
#region Methods support CacheItemPolicy
109+
110+
public async Task<object> AddAsync(string key, object entry, CacheItemPolicy cacheItemPolicy) {
111+
//TODO: Decide what to work on the monitors
112+
return await sqlUtilityHelper.AddAsync(key, entry, cacheItemPolicy.AbsoluteExpiration.DateTime);
113+
}
114+
115+
public async Task SetAsync(string key, object entry, CacheItemPolicy cacheItemPolicy) {
116+
//TODO: Decide what to work on the monitors
117+
await sqlUtilityHelper.SetAsync(key, entry, cacheItemPolicy.AbsoluteExpiration.DateTime);
118+
}
119+
}
120+
#endregion
121+
}
122+

0 commit comments

Comments
 (0)