From 2e72abf59f6f173d12edd4745fa7808cde1ceb1d Mon Sep 17 00:00:00 2001 From: Sebastien Ros Date: Tue, 21 Jan 2014 10:42:56 -0800 Subject: [PATCH] Adding Orchard.Caching module --- src/Orchard.Azure/Orchard.Azure.sln | 11 +- .../Modules/Orchard.Caching/Module.txt | 11 ++ .../Orchard.Caching/Orchard.Caching.csproj | 127 ++++++++++++++++++ .../Properties/AssemblyInfo.cs | 36 +++++ .../Services/DefaultCacheService.cs | 44 ++++++ .../Services/DefaultCacheStorageProvider.cs | 51 +++++++ .../Orchard.Caching/Services/ICacheService.cs | 44 ++++++ .../Services/ICacheStorageProvider.cs | 11 ++ .../Modules/Orchard.Caching/Web.config | 41 ++++++ src/Orchard.sln | 15 ++- 10 files changed, 389 insertions(+), 2 deletions(-) create mode 100644 src/Orchard.Web/Modules/Orchard.Caching/Module.txt create mode 100644 src/Orchard.Web/Modules/Orchard.Caching/Orchard.Caching.csproj create mode 100644 src/Orchard.Web/Modules/Orchard.Caching/Properties/AssemblyInfo.cs create mode 100644 src/Orchard.Web/Modules/Orchard.Caching/Services/DefaultCacheService.cs create mode 100644 src/Orchard.Web/Modules/Orchard.Caching/Services/DefaultCacheStorageProvider.cs create mode 100644 src/Orchard.Web/Modules/Orchard.Caching/Services/ICacheService.cs create mode 100644 src/Orchard.Web/Modules/Orchard.Caching/Services/ICacheStorageProvider.cs create mode 100644 src/Orchard.Web/Modules/Orchard.Caching/Web.config diff --git a/src/Orchard.Azure/Orchard.Azure.sln b/src/Orchard.Azure/Orchard.Azure.sln index 3c4bfc8eb..b7e97ef7e 100644 --- a/src/Orchard.Azure/Orchard.Azure.sln +++ b/src/Orchard.Azure/Orchard.Azure.sln @@ -1,6 +1,8 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2012 +# Visual Studio 2013 +VisualStudioVersion = 12.0.30110.0 +MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Modules", "Modules", "{8E3DE014-9B28-4B32-8AC1-B2BE404E9B2D}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tools", "Tools", "{75E7476C-C05B-4C41-8E38-081D3EB55659}" @@ -137,6 +139,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Modules.Deprecated", "Modul EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Orchard", "Orchard", "{F2AB7512-139A-420F-AE3A-9ED22CA52CE1}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Orchard.Caching", "..\Orchard.Web\Modules\Orchard.Caching\Orchard.Caching.csproj", "{7528BF74-25C7-4ABE-883A-443B4EEC4776}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -395,6 +399,10 @@ Global {CBC7993C-57D8-4A6C-992C-19E849DFE71D}.Debug|Any CPU.Build.0 = Debug|Any CPU {CBC7993C-57D8-4A6C-992C-19E849DFE71D}.Release|Any CPU.ActiveCfg = Release|Any CPU {CBC7993C-57D8-4A6C-992C-19E849DFE71D}.Release|Any CPU.Build.0 = Release|Any CPU + {7528BF74-25C7-4ABE-883A-443B4EEC4776}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7528BF74-25C7-4ABE-883A-443B4EEC4776}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7528BF74-25C7-4ABE-883A-443B4EEC4776}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7528BF74-25C7-4ABE-883A-443B4EEC4776}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -452,6 +460,7 @@ Global {8A9FDB57-342D-49C2-BAFC-D885AAE5CC7C} = {8E3DE014-9B28-4B32-8AC1-B2BE404E9B2D} {5D13EF34-8B39-4EC5-847F-E12892ACF841} = {8E3DE014-9B28-4B32-8AC1-B2BE404E9B2D} {E649EA64-D213-461B-87F7-D67035801443} = {8E3DE014-9B28-4B32-8AC1-B2BE404E9B2D} + {7528BF74-25C7-4ABE-883A-443B4EEC4776} = {8E3DE014-9B28-4B32-8AC1-B2BE404E9B2D} {33B1BC8D-E292-4972-A363-22056B207156} = {75E7476C-C05B-4C41-8E38-081D3EB55659} {CB70A642-8CEC-4DDE-8C9F-AD08900EC98D} = {84650275-884D-4CBB-9CC0-67553996E211} {9916839C-39FC-4CEB-A5AF-89CA7E87119F} = {F2AB7512-139A-420F-AE3A-9ED22CA52CE1} diff --git a/src/Orchard.Web/Modules/Orchard.Caching/Module.txt b/src/Orchard.Web/Modules/Orchard.Caching/Module.txt new file mode 100644 index 000000000..90ce2a7c4 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Caching/Module.txt @@ -0,0 +1,11 @@ +Name: Orchard.Caching +AntiForgery: enabled +Author: Sébastien Ros +Website: http://orchardproject.net +Version: 1.0 +OrchardVersion: 1.8 +Description: Provides an API to cache business data. +Features: + Orchard.Caching: + Description: Provides an API to cache business data. + Category: Performance \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Caching/Orchard.Caching.csproj b/src/Orchard.Web/Modules/Orchard.Caching/Orchard.Caching.csproj new file mode 100644 index 000000000..3727b0cce --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Caching/Orchard.Caching.csproj @@ -0,0 +1,127 @@ + + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {7528BF74-25C7-4ABE-883A-443B4EEC4776} + {349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc} + Library + Properties + Orchard.Caching + Orchard.Caching + v4.5 + false + + + 4.0 + + + false + + + + + + + true + full + false + bin\ + DEBUG;TRACE + prompt + 4 + AllRules.ruleset + false + + + pdbonly + true + bin\ + TRACE + prompt + 4 + AllRules.ruleset + false + + + + + + + + + + False + ..\..\..\..\lib\aspnetmvc\System.Web.Mvc.dll + + + + + + + + + + + + + + {2D1D92BB-4555-4CBE-8D0E-63563D6CE4C6} + Orchard.Framework + + + + + + + + + + + 10.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + + + + + $(ProjectDir)\..\Manifests + + + + + + + + + + + + False + True + 45979 + / + + + False + True + http://orchard.codeplex.com + False + + + + + \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Caching/Properties/AssemblyInfo.cs b/src/Orchard.Web/Modules/Orchard.Caching/Properties/AssemblyInfo.cs new file mode 100644 index 000000000..a8f214c18 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Caching/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Security; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Orchard.Caching")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyProduct("Orchard")] +[assembly: AssemblyCopyright("Copyright © Outercurve Foundation 2009")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("9eb193b8-f7bc-470e-ac3f-1cc2d7db6b7f")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Revision and Build Numbers +// by using the '*' as shown below: +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] + diff --git a/src/Orchard.Web/Modules/Orchard.Caching/Services/DefaultCacheService.cs b/src/Orchard.Web/Modules/Orchard.Caching/Services/DefaultCacheService.cs new file mode 100644 index 000000000..378f576b3 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Caching/Services/DefaultCacheService.cs @@ -0,0 +1,44 @@ +using System; +using Orchard.Environment.Configuration; + +namespace Orchard.Caching.Services { + /// + /// Provides a per tenant implementation. + /// Default timeout is 20 minutes. + /// + public class DefaultCacheService : ICacheService { + private readonly ICacheStorageProvider _cacheStorageProvider; + private readonly string _prefix; + + public DefaultCacheService( + ShellSettings shellSettings, + ICacheStorageProvider cacheStorageProvider) { + _cacheStorageProvider = cacheStorageProvider; + _prefix = shellSettings.Name; + } + + public object Get(string key) { + return _cacheStorageProvider.Get(BuildFullKey(key)); + } + + public void Put(string key, object value) { + _cacheStorageProvider.Put(BuildFullKey(key), value); + } + + public void Put(string key, object value, TimeSpan validFor) { + _cacheStorageProvider.Put(BuildFullKey(key), value, validFor); + } + + public void Remove(string key) { + _cacheStorageProvider.Remove(BuildFullKey(key)); + } + + public void Clear() { + _cacheStorageProvider.Clear(); + } + + private string BuildFullKey(string key) { + return String.Concat(_prefix, "_", key); + } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Caching/Services/DefaultCacheStorageProvider.cs b/src/Orchard.Web/Modules/Orchard.Caching/Services/DefaultCacheStorageProvider.cs new file mode 100644 index 000000000..47e8e66aa --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Caching/Services/DefaultCacheStorageProvider.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections; +using System.Linq; +using System.Web; + +namespace Orchard.Caching.Services { + public class DefaultCacheStorageProvider : ICacheStorageProvider { + + public void Put(string key, object value) { + HttpRuntime.Cache.Insert( + key, + value, + null, + System.Web.Caching.Cache.NoAbsoluteExpiration, + System.Web.Caching.Cache.NoSlidingExpiration, + System.Web.Caching.CacheItemPriority.Normal, + null); + } + + public void Put(string key, object value, TimeSpan validFor) { + HttpRuntime.Cache.Insert( + key, + value, + null, + System.Web.Caching.Cache.NoAbsoluteExpiration, + validFor, + System.Web.Caching.CacheItemPriority.Normal, + null); + } + + public void Remove(string key) { + HttpRuntime.Cache.Remove(key); + } + + public void Clear() { + var all = HttpRuntime.Cache + .AsParallel() + .Cast() + .Select(x => x.Key.ToString()) + .ToList(); + + foreach (var key in all) { + Remove(key); + } + } + + public object Get(string key) { + return HttpRuntime.Cache.Get(key); + } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Caching/Services/ICacheService.cs b/src/Orchard.Web/Modules/Orchard.Caching/Services/ICacheService.cs new file mode 100644 index 000000000..74ee9d39a --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Caching/Services/ICacheService.cs @@ -0,0 +1,44 @@ +using System; + +namespace Orchard.Caching.Services { + public interface ICacheService : IDependency { + + object Get(string key); + void Put(string key, object value); + void Put(string key, object value, TimeSpan validFor); + + void Remove(string key); + void Clear(); + } + + public static class CachingExtensions { + + public static T Get(this ICacheService cacheService, string key) { + return (T)cacheService.Get(key); + } + + public static T Get(this ICacheService cacheService, string key, Func factory) { + var result = cacheService.Get(key); + if (result == null) { + var computed = factory(); + cacheService.Put(key, computed); + return computed; + } + + // try to convert to T + return (T)result; + } + + public static T Get(this ICacheService cacheService, string key, Func factory, TimeSpan validFor) { + var result = cacheService.Get(key); + if (result == null) { + var computed = factory(); + cacheService.Put(key, computed, validFor); + return computed; + } + + // try to convert to T + return (T)result; + } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Caching/Services/ICacheStorageProvider.cs b/src/Orchard.Web/Modules/Orchard.Caching/Services/ICacheStorageProvider.cs new file mode 100644 index 000000000..545b4f0df --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Caching/Services/ICacheStorageProvider.cs @@ -0,0 +1,11 @@ +using System; + +namespace Orchard.Caching.Services { + public interface ICacheStorageProvider : IDependency { + object Get(string key); + void Put(string key, object value); + void Put(string key, object value, TimeSpan validFor); + void Remove(string key); + void Clear(); + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Caching/Web.config b/src/Orchard.Web/Modules/Orchard.Caching/Web.config new file mode 100644 index 000000000..9589e96c5 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Caching/Web.config @@ -0,0 +1,41 @@ + + + + + + + +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Orchard.sln b/src/Orchard.sln index 685df2adf..6ebad4d4a 100644 --- a/src/Orchard.sln +++ b/src/Orchard.sln @@ -1,6 +1,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 2013 -VisualStudioVersion = 12.0.21005.1 +VisualStudioVersion = 12.0.30110.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Modules", "Modules", "{E9C9F120-07BA-4DFB-B9C3-3AFB9D44C9D5}" EndProject @@ -235,6 +235,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Orchard.Templates.Tests", " EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Orchard.JobsQueue", "Orchard.Web\Modules\Orchard.JobsQueue\Orchard.JobsQueue.csproj", "{085948FF-0E9B-4A9A-B564-F8B8B4BDDDBC}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Orchard.Caching", "Orchard.Web\Modules\Orchard.Caching\Orchard.Caching.csproj", "{7528BF74-25C7-4ABE-883A-443B4EEC4776}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution CodeCoverage|Any CPU = CodeCoverage|Any CPU @@ -983,6 +985,16 @@ Global {085948FF-0E9B-4A9A-B564-F8B8B4BDDDBC}.FxCop|Any CPU.Build.0 = Release|Any CPU {085948FF-0E9B-4A9A-B564-F8B8B4BDDDBC}.Release|Any CPU.ActiveCfg = Release|Any CPU {085948FF-0E9B-4A9A-B564-F8B8B4BDDDBC}.Release|Any CPU.Build.0 = Release|Any CPU + {7528BF74-25C7-4ABE-883A-443B4EEC4776}.CodeCoverage|Any CPU.ActiveCfg = Release|Any CPU + {7528BF74-25C7-4ABE-883A-443B4EEC4776}.CodeCoverage|Any CPU.Build.0 = Release|Any CPU + {7528BF74-25C7-4ABE-883A-443B4EEC4776}.Coverage|Any CPU.ActiveCfg = Release|Any CPU + {7528BF74-25C7-4ABE-883A-443B4EEC4776}.Coverage|Any CPU.Build.0 = Release|Any CPU + {7528BF74-25C7-4ABE-883A-443B4EEC4776}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7528BF74-25C7-4ABE-883A-443B4EEC4776}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7528BF74-25C7-4ABE-883A-443B4EEC4776}.FxCop|Any CPU.ActiveCfg = Release|Any CPU + {7528BF74-25C7-4ABE-883A-443B4EEC4776}.FxCop|Any CPU.Build.0 = Release|Any CPU + {7528BF74-25C7-4ABE-883A-443B4EEC4776}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7528BF74-25C7-4ABE-883A-443B4EEC4776}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -1044,6 +1056,7 @@ Global {36B82383-D69E-4897-A24A-648BABDF80EC} = {E9C9F120-07BA-4DFB-B9C3-3AFB9D44C9D5} {10AB3CE2-A720-467F-9EC8-EBB4BAC9A1C9} = {E9C9F120-07BA-4DFB-B9C3-3AFB9D44C9D5} {085948FF-0E9B-4A9A-B564-F8B8B4BDDDBC} = {E9C9F120-07BA-4DFB-B9C3-3AFB9D44C9D5} + {7528BF74-25C7-4ABE-883A-443B4EEC4776} = {E9C9F120-07BA-4DFB-B9C3-3AFB9D44C9D5} {ABC826D4-2FA1-4F2F-87DE-E6095F653810} = {74E681ED-FECC-4034-B9BD-01B0BB1BDECA} {F112851D-B023-4746-B6B1-8D2E5AD8F7AA} = {74E681ED-FECC-4034-B9BD-01B0BB1BDECA} {6CB3EB30-F725-45C0-9742-42599BA8E8D2} = {74E681ED-FECC-4034-B9BD-01B0BB1BDECA}