Added non-generic registration interface to IKernel and IWindsor to accommodate dynam...
[castle.git] / MonoRail / Castle.MonoRail.Framework / Attributes / CacheAttribute.cs
blobb974a91b6fb9275d38e4b05f8ed71991e7980194
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.Framework
17 using System;
18 using System.Web;
20 /// <summary>
21 /// Defines the cache configuration for an action.
22 /// </summary>
23 [AttributeUsage(AttributeTargets.Method|AttributeTargets.Class, AllowMultiple = false, Inherited = true), Serializable]
24 public class CacheAttribute : Attribute, ICachePolicyConfigurer
26 private readonly HttpCacheability cacheability;
27 private bool allowInHistory, slidingExpiration, validUntilExpires;
28 private string etag;
29 private int duration;
30 private string varyByCustom, varyByHeaders, varyByParams;
32 /// <summary>
33 /// Initializes a new instance of the <see cref="CacheAttribute"/> class.
34 /// </summary>
35 /// <param name="cacheability">Sets the Cache-Control HTTP header.
36 /// The Cache-Control HTTP header controls how documents are to be cached on the network.</param>
37 public CacheAttribute(HttpCacheability cacheability)
39 this.cacheability = cacheability;
41 allowInHistory = true;
42 validUntilExpires = true;
45 /// <summary>
46 /// From MSDN: Makes the response is available in the client browser
47 /// History cache, regardless of the HttpCacheability setting
48 /// made on the server, when the allow parameter is true.
49 /// </summary>
50 /// <remarks>
51 /// When HttpCacheability is set to NoCache or ServerAndNoCache the Expires
52 /// HTTP header is by default set to -1; this tells the client not to
53 /// cache responses in the History folder, so that when you use the back/forward buttons
54 /// the client requests a new version of the response each time. You can override this
55 /// behavior by calling the SetAllowResponseInBrowserHistory method with the
56 /// allow parameter set to true.
57 /// <para>
58 /// If HttpCacheability is set to values other than NoCache or ServerAndNoCache, calling the SetAllowResponseInBrowserHistory method with either value for allow has no effect.
59 /// </para>
60 /// </remarks>
61 public bool AllowInHistory
63 get { return allowInHistory; }
64 set { allowInHistory = value; }
67 /// <summary>
68 /// From MSDN: Sets cache expiration to from absolute to sliding.
69 /// </summary>
70 /// <remarks>
71 /// When cache expiration is set to sliding, the Cache-Control
72 /// HTTP header will be renewed with each response. This expiration mode
73 /// is identical to the IIS configuration option to add an expiration
74 /// header to all output set relative to the current time.
75 /// <para>
76 /// If you explicitly set sliding expiration to off (false), that setting
77 /// will be preserved and any attempts to enable sliding expiration will
78 /// silently fail. This method does not directly map to an HTTP header.
79 /// It is used by subsequent modules or worker requests to set origin-server cache policy.
80 /// </para>
81 /// </remarks>
82 public bool SlidingExpiration
84 get { return slidingExpiration; }
85 set { slidingExpiration = value; }
88 /// <summary>
89 /// Specifies whether the ASP.NET cache should ignore HTTP Cache-Control
90 /// headers sent by the client that invalidate the cache.
91 /// </summary>
92 /// <remarks>
93 /// This method is provided because some browsers, when refreshing a
94 /// page view, send HTTP cache invalidation headers to the Web server
95 /// and evict the page from the cache. When the validUntilExpires parameter
96 /// is true, ASP.NET ignores cache invalidation headers and the page
97 /// remains in the cache until it expires.
98 /// </remarks>
99 public bool ValidUntilExpires
101 get { return validUntilExpires; }
102 set { validUntilExpires = value; }
105 /// <summary>
106 /// Sets the ETag HTTP header to the specified string.
107 /// </summary>
108 /// <remarks>
109 /// The ETag header is a unique identifier for a specific version of
110 /// a document. It is used by clients to validate client-cached content to
111 /// avoid requesting it again. Once an ETag header is set, subsequent
112 /// attempts to set it fail and an exception is thrown.
113 /// </remarks>
114 public string ETag
116 get { return etag; }
117 set { etag = value; }
120 /// <summary>
121 /// Cache Duration (in seconds)
122 /// </summary>
123 public int Duration
125 get { return duration; }
126 set { duration = value; }
129 /// <summary>
130 /// Specifies a custom text string to vary cached output responses by.
131 /// </summary>
132 public string VaryByCustom
134 get { return varyByCustom; }
135 set { varyByCustom = value; }
138 /// <summary>
139 /// Gets or sets the list of all HTTP headers that will be used to vary cache output.
140 /// </summary>
141 /// <remarks>
142 /// When a cached item has several vary headers, a separate version of
143 /// the requested document is available from the cache for each HTTP header type.
144 /// </remarks>
145 public string VaryByHeaders
147 get { return varyByHeaders; }
148 set { varyByHeaders = value; }
151 /// <summary>
152 /// Gets or sets the list of parameters received by an HTTP GET or HTTP POST that affect caching.
153 /// </summary>
154 /// <remarks>
155 /// A separate version of the requested document is available from the cache
156 /// for each named parameter in the VaryByParams collection.
157 /// </remarks>
158 public string VaryByParams
160 get { return varyByParams; }
161 set { varyByParams = value; }
164 /// <summary>
165 /// Configures ASP.Net's Cache policy based on properties set
166 /// </summary>
167 /// <param name="policy">cache policy to set</param>
168 void ICachePolicyConfigurer.Configure(HttpCachePolicy policy)
170 policy.SetCacheability(cacheability);
171 policy.SetSlidingExpiration(slidingExpiration);
172 policy.SetValidUntilExpires(validUntilExpires);
173 policy.SetAllowResponseInBrowserHistory(allowInHistory);
175 if (duration != 0)
177 policy.SetExpires(DateTime.Now.AddSeconds(duration));
180 if (varyByCustom != null)
182 policy.SetVaryByCustom(varyByCustom);
185 if (varyByHeaders != null)
187 foreach(String header in varyByHeaders.Split(','))
189 policy.VaryByHeaders[header.Trim()] = true;
193 if (varyByParams != null)
195 foreach(String param in varyByParams.Split(','))
197 policy.VaryByParams[param.Trim()] = true;
201 if (etag != null)
203 policy.SetETag(etag);