New tests.
[mono.git] / mcs / class / System.Web.Mvc2 / System.Web.Mvc / DefaultViewLocationCache.cs
1 /* ****************************************************************************\r
2  *\r
3  * Copyright (c) Microsoft Corporation. All rights reserved.\r
4  *\r
5  * This software is subject to the Microsoft Public License (Ms-PL). \r
6  * A copy of the license can be found in the license.htm file included \r
7  * in this distribution.\r
8  *\r
9  * You must not remove this notice, or any other, from this software.\r
10  *\r
11  * ***************************************************************************/\r
12 \r
13 namespace System.Web.Mvc {\r
14     using System;\r
15     using System.Diagnostics.CodeAnalysis;\r
16     using System.Web.Caching;\r
17     using System.Web.Mvc.Resources;\r
18 \r
19     [SuppressMessage("Microsoft.Security", "CA2112:SecuredTypesShouldNotExposeFields",\r
20         Justification = "The Null field does not have access to secure information")]\r
21     public class DefaultViewLocationCache : IViewLocationCache {\r
22         private static readonly TimeSpan _defaultTimeSpan = new TimeSpan(0, 15, 0);\r
23 \r
24         public DefaultViewLocationCache()\r
25             : this(_defaultTimeSpan) {\r
26         }\r
27 \r
28         public DefaultViewLocationCache(TimeSpan timeSpan) {\r
29             if (timeSpan.Ticks < 0) {\r
30                 throw new InvalidOperationException(MvcResources.DefaultViewLocationCache_NegativeTimeSpan);\r
31             }\r
32             TimeSpan = timeSpan;\r
33         }\r
34 \r
35         [SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes",\r
36             Justification = "The reference type is immutable. ")]\r
37         public static readonly IViewLocationCache Null = new NullViewLocationCache();\r
38 \r
39         public TimeSpan TimeSpan {\r
40             get;\r
41             private set;\r
42         }\r
43 \r
44         #region IViewLocationCache Members\r
45         public string GetViewLocation(HttpContextBase httpContext, string key) {\r
46             if (httpContext == null) {\r
47                 throw new ArgumentNullException("httpContext");\r
48             }\r
49             return (string)httpContext.Cache[key];\r
50         }\r
51 \r
52         public void InsertViewLocation(HttpContextBase httpContext, string key, string virtualPath) {\r
53             if (httpContext == null) {\r
54                 throw new ArgumentNullException("httpContext");\r
55             }\r
56             httpContext.Cache.Insert(key, virtualPath, null /* dependencies */, Cache.NoAbsoluteExpiration, TimeSpan);\r
57         }\r
58         #endregion\r
59     }\r
60 }\r