Skip to content

Commit d7eacd2

Browse files
author
Jeff Treuting
committed
Added Windows Azure Shared Caching provider
It seems like this is the same code as the AppFabric stuff so I'm a bit confused. Again haven't tested it yet. Seems like the AppFabric stuff was used as the start for the azure caching so they share things, but the code really is identical even though I included different NuGet packages for them. http://msdn.microsoft.com/en-us/library/windowsazure/gg185678.aspx
1 parent 4846e06 commit d7eacd2

17 files changed

+2132
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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("SharpRepository.Caching.WindowsAzure")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("SharpRepository.Caching.WindowsAzure")]
13+
[assembly: AssemblyCopyright("Copyright © 2012")]
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("dddb540e-2ed4-4080-ad47-8abf332aa73d")]
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 Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{F025C038-9F9A-4730-9A03-3E3E6101E249}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>SharpRepository.Caching.WindowsAzure</RootNamespace>
11+
<AssemblyName>SharpRepository.Caching.WindowsAzure</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<DebugSymbols>true</DebugSymbols>
17+
<DebugType>full</DebugType>
18+
<Optimize>false</Optimize>
19+
<OutputPath>bin\Debug\</OutputPath>
20+
<DefineConstants>DEBUG;TRACE</DefineConstants>
21+
<ErrorReport>prompt</ErrorReport>
22+
<WarningLevel>4</WarningLevel>
23+
</PropertyGroup>
24+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
25+
<DebugType>pdbonly</DebugType>
26+
<Optimize>true</Optimize>
27+
<OutputPath>bin\Release\</OutputPath>
28+
<DefineConstants>TRACE</DefineConstants>
29+
<ErrorReport>prompt</ErrorReport>
30+
<WarningLevel>4</WarningLevel>
31+
</PropertyGroup>
32+
<ItemGroup>
33+
<Reference Include="Microsoft.ApplicationServer.Caching.Client, Version=101.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
34+
<HintPath>..\packages\WindowsAzure.Caching.1.7.0.0\lib\net35-full\Microsoft.ApplicationServer.Caching.Client.dll</HintPath>
35+
</Reference>
36+
<Reference Include="Microsoft.ApplicationServer.Caching.Core, Version=101.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
37+
<HintPath>..\packages\WindowsAzure.Caching.1.7.0.0\lib\net35-full\Microsoft.ApplicationServer.Caching.Core.dll</HintPath>
38+
</Reference>
39+
<Reference Include="Microsoft.Web.DistributedCache, Version=101.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
40+
<HintPath>..\packages\WindowsAzure.Caching.1.7.0.0\lib\net35-full\Microsoft.Web.DistributedCache.dll</HintPath>
41+
</Reference>
42+
<Reference Include="Microsoft.WindowsFabric.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
43+
<HintPath>..\packages\WindowsAzure.Caching.1.7.0.0\lib\net35-full\Microsoft.WindowsFabric.Common.dll</HintPath>
44+
</Reference>
45+
<Reference Include="Microsoft.WindowsFabric.Data.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
46+
<HintPath>..\packages\WindowsAzure.Caching.1.7.0.0\lib\net35-full\Microsoft.WindowsFabric.Data.Common.dll</HintPath>
47+
</Reference>
48+
<Reference Include="System" />
49+
<Reference Include="System.Core" />
50+
<Reference Include="System.Runtime.Caching" />
51+
</ItemGroup>
52+
<ItemGroup>
53+
<Compile Include="WindowsAzureCachingProvider.cs" />
54+
<Compile Include="Properties\AssemblyInfo.cs" />
55+
</ItemGroup>
56+
<ItemGroup>
57+
<ProjectReference Include="..\SharpRepository.Repository\SharpRepository.Repository.csproj">
58+
<Project>{710dee79-25ce-4f68-b8b1-d08a135ad154}</Project>
59+
<Name>SharpRepository.Repository</Name>
60+
</ProjectReference>
61+
</ItemGroup>
62+
<ItemGroup>
63+
<None Include="app.config" />
64+
<None Include="packages.config" />
65+
</ItemGroup>
66+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
67+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
68+
Other similar extension points exist, see Microsoft.Common.targets.
69+
<Target Name="BeforeBuild">
70+
</Target>
71+
<Target Name="AfterBuild">
72+
</Target>
73+
-->
74+
</Project>
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
using System;
2+
using System.Runtime.Caching;
3+
using Microsoft.ApplicationServer.Caching;
4+
using SharpRepository.Repository.Caching;
5+
6+
namespace SharpRepository.Caching.WindowsAzure
7+
{
8+
public class WindowsAzureCachingProvider : ICachingProvider
9+
{
10+
protected DataCacheFactory CacheFactory { get; set;}
11+
protected DataCache Cache { get; set; }
12+
13+
private static readonly object LockObject = new object();
14+
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="WindowsAzureCachingProvider"/> class.
17+
/// </summary>
18+
/// <param name="cacheName">Name of the cache.</param>
19+
public WindowsAzureCachingProvider(string cacheName = null)
20+
: this(new DataCacheFactory(), cacheName)
21+
{
22+
}
23+
24+
/// <summary>
25+
/// Initializes a new instance of the <see cref="WindowsAzureCachingProvider"/> class.
26+
/// </summary>
27+
/// <param name="configuration">The AbbFabric Caching configuration.</param>
28+
/// <param name="cacheName">Name of the cache.</param>
29+
public WindowsAzureCachingProvider(DataCacheFactoryConfiguration configuration, string cacheName = null)
30+
: this(new DataCacheFactory(configuration), cacheName)
31+
{
32+
if (configuration == null) throw new ArgumentNullException("configuration");
33+
}
34+
35+
/// <summary>
36+
/// Initializes a new instance of the <see cref="WindowsAzureCachingProvider"/> class.
37+
/// </summary>
38+
/// <param name="cacheFactory">The cache factory.</param>
39+
/// <param name="cacheName">Name of the cache.</param>
40+
public WindowsAzureCachingProvider(DataCacheFactory cacheFactory, string cacheName = null)
41+
{
42+
if (cacheFactory == null) throw new ArgumentNullException("cacheFactory");
43+
44+
CacheFactory = cacheFactory;
45+
46+
// TODO: don't know enough about Azure caching to know if we should use the GetDefaultCache() if no cache name provided, or if we should use our own name like SharpRepository
47+
Cache = String.IsNullOrEmpty(cacheName) ? cacheFactory.GetDefaultCache() : cacheFactory.GetCache(cacheName);
48+
}
49+
50+
public void Set<T>(string key, T value, CacheItemPriority priority = CacheItemPriority.Default, int? cacheTime = null)
51+
{
52+
if (String.IsNullOrEmpty(key)) throw new ArgumentNullException("key");
53+
54+
if (!cacheTime.HasValue)
55+
{
56+
Cache.Put(key, value);
57+
}
58+
else
59+
{
60+
Cache.Put(key, value, TimeSpan.FromSeconds(cacheTime.Value));
61+
}
62+
}
63+
64+
public void Clear(string key)
65+
{
66+
if (String.IsNullOrEmpty(key)) throw new ArgumentNullException("key");
67+
68+
Cache.Remove(key);
69+
}
70+
71+
public bool Exists(string key)
72+
{
73+
if (String.IsNullOrEmpty(key)) throw new ArgumentNullException("key");
74+
75+
return Cache.Get(key) != null;
76+
}
77+
78+
public bool Get<T>(string key, out T value)
79+
{
80+
if (String.IsNullOrEmpty(key)) throw new ArgumentNullException("key");
81+
82+
value = default(T);
83+
84+
try
85+
{
86+
var item = Cache.Get(key);
87+
if (item == null)
88+
return false;
89+
90+
value = (T)item;
91+
}
92+
catch (Exception)
93+
{
94+
return false;
95+
}
96+
97+
return true;
98+
}
99+
100+
public int Increment(string key, int defaultValue, int value, CacheItemPriority priority = CacheItemPriority.Default)
101+
{
102+
if (String.IsNullOrEmpty(key)) throw new ArgumentNullException("key");
103+
104+
lock (LockObject)
105+
{
106+
int current;
107+
if (!Get(key, out current))
108+
{
109+
current = defaultValue;
110+
}
111+
112+
var newValue = current + value;
113+
Set(key, newValue, priority);
114+
return newValue;
115+
}
116+
}
117+
118+
public void Dispose()
119+
{
120+
Cache = null;
121+
CacheFactory.Dispose();
122+
}
123+
}
124+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<configSections>
4+
<!-- Append below entry to configSections. Do not overwrite the full section. -->
5+
<section name="dataCacheClients" type="Microsoft.ApplicationServer.Caching.DataCacheClientsSection, Microsoft.ApplicationServer.Caching.Core" allowLocation="true" allowDefinition="Everywhere" />
6+
</configSections>
7+
<dataCacheClients>
8+
<dataCacheClient name="default">
9+
<hosts>
10+
<host name="[Insert Cache EndPoint]" cachePort="22233" />
11+
</hosts>
12+
<securityProperties mode="Message">
13+
<messageSecurity authorizationInfo="[Encrypted ACS token goes here]"></messageSecurity>
14+
</securityProperties>
15+
</dataCacheClient>
16+
<dataCacheClient name="SslEndpoint">
17+
<hosts>
18+
<host name="[Insert Cache EndPoint]" cachePort="22243" />
19+
</hosts>
20+
<securityProperties mode="Message" sslEnabled="true">
21+
<messageSecurity authorizationInfo="[Encrypted ACS token goes here]"></messageSecurity>
22+
</securityProperties>
23+
</dataCacheClient>
24+
</dataCacheClients>
25+
</configuration>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="WindowsAzure.Caching" version="1.7.0.0" targetFramework="net45" />
4+
</packages>

SharpRepository.sln

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpRepository.Caching.App
4242
EndProject
4343
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpRepository.Caching.Redis", "SharpRepository.Caching.Redis\SharpRepository.Caching.Redis.csproj", "{1174F5B3-D3BE-453D-BD9F-D54EEFB0ADFE}"
4444
EndProject
45+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpRepository.Caching.WindowsAzure", "SharpRepository.Caching.WindowsAzure\SharpRepository.Caching.WindowsAzure.csproj", "{F025C038-9F9A-4730-9A03-3E3E6101E249}"
46+
EndProject
47+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Caching Providers", "Caching Providers", "{FB7E3040-CA92-4B81-86E6-106E2FDD745E}"
48+
EndProject
4549
Global
4650
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4751
Debug|Any CPU = Debug|Any CPU
@@ -104,6 +108,10 @@ Global
104108
{1174F5B3-D3BE-453D-BD9F-D54EEFB0ADFE}.Debug|Any CPU.Build.0 = Debug|Any CPU
105109
{1174F5B3-D3BE-453D-BD9F-D54EEFB0ADFE}.Release|Any CPU.ActiveCfg = Release|Any CPU
106110
{1174F5B3-D3BE-453D-BD9F-D54EEFB0ADFE}.Release|Any CPU.Build.0 = Release|Any CPU
111+
{F025C038-9F9A-4730-9A03-3E3E6101E249}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
112+
{F025C038-9F9A-4730-9A03-3E3E6101E249}.Debug|Any CPU.Build.0 = Debug|Any CPU
113+
{F025C038-9F9A-4730-9A03-3E3E6101E249}.Release|Any CPU.ActiveCfg = Release|Any CPU
114+
{F025C038-9F9A-4730-9A03-3E3E6101E249}.Release|Any CPU.Build.0 = Release|Any CPU
107115
EndGlobalSection
108116
GlobalSection(SolutionProperties) = preSolution
109117
HideSolutionNode = FALSE
@@ -112,5 +120,9 @@ Global
112120
{208B5ECD-8CF5-4B63-BC3E-131D45F23374} = {2D1FDDD5-70D2-4896-BEE7-5FE60946A686}
113121
{72E04281-9654-41BE-952D-A18ED24E3ACB} = {307222D2-CC06-4017-9554-E0ED81BA7304}
114122
{85E4FB46-FFB2-47DC-83B5-4ECBD80B66D8} = {9DEAA991-4B0E-4C9C-85AE-6FF398D34401}
123+
{3583AEBE-F1B5-44D9-A0D2-4F6FA1F459CA} = {FB7E3040-CA92-4B81-86E6-106E2FDD745E}
124+
{06111314-669B-4E35-A0B9-AF67FA3F0FFD} = {FB7E3040-CA92-4B81-86E6-106E2FDD745E}
125+
{1174F5B3-D3BE-453D-BD9F-D54EEFB0ADFE} = {FB7E3040-CA92-4B81-86E6-106E2FDD745E}
126+
{F025C038-9F9A-4730-9A03-3E3E6101E249} = {FB7E3040-CA92-4B81-86E6-106E2FDD745E}
115127
EndGlobalSection
116128
EndGlobal
Binary file not shown.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
4+
<configSections>
5+
<!-- Append below entry to configSections. Do not overwrite the full section. -->
6+
<section name="dataCacheClients" type="Microsoft.ApplicationServer.Caching.DataCacheClientsSection, Microsoft.ApplicationServer.Caching.Core" allowLocation="true" allowDefinition="Everywhere" />
7+
</configSections>
8+
9+
<dataCacheClients>
10+
11+
<dataCacheClient name="default">
12+
<hosts>
13+
<host name="[Insert Cache EndPoint]" cachePort="22233" />
14+
</hosts>
15+
<securityProperties mode="Message">
16+
<messageSecurity authorizationInfo="[Encrypted ACS token goes here]"></messageSecurity>
17+
</securityProperties>
18+
</dataCacheClient>
19+
20+
<dataCacheClient name="SslEndpoint">
21+
<hosts>
22+
<host name="[Insert Cache EndPoint]" cachePort="22243" />
23+
</hosts>
24+
<securityProperties mode="Message" sslEnabled="true">
25+
<messageSecurity authorizationInfo="[Encrypted ACS token goes here]"></messageSecurity>
26+
</securityProperties>
27+
</dataCacheClient>
28+
29+
</dataCacheClients>
30+
</configuration>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0"?>
2+
3+
<!--
4+
For more information on how to configure your ASP.NET application, please visit
5+
http://go.microsoft.com/fwlink/?LinkId=169433
6+
-->
7+
8+
<configuration>
9+
10+
<configSections>
11+
<!-- Append below entry to configSections. Do not overwrite the full section. -->
12+
<section name="dataCacheClients" type="Microsoft.ApplicationServer.Caching.DataCacheClientsSection, Microsoft.ApplicationServer.Caching.Core" allowLocation="true" allowDefinition="Everywhere"/>
13+
</configSections>
14+
15+
<dataCacheClients>
16+
17+
<dataCacheClient name="default">
18+
<hosts>
19+
<host name="[Insert Cache EndPoint]" cachePort="22233" />
20+
</hosts>
21+
<securityProperties mode="Message">
22+
<messageSecurity authorizationInfo="[Encrypted ACS token goes here]"></messageSecurity>
23+
</securityProperties>
24+
</dataCacheClient>
25+
26+
<dataCacheClient name="SslEndpoint">
27+
<hosts>
28+
<host name="[Insert Cache EndPoint]" cachePort="22243" />
29+
</hosts>
30+
<securityProperties mode="Message" sslEnabled="true">
31+
<messageSecurity authorizationInfo="[Encrypted ACS token goes here]"></messageSecurity>
32+
</securityProperties>
33+
</dataCacheClient>
34+
35+
</dataCacheClients>
36+
37+
38+
</configuration>

0 commit comments

Comments
 (0)