1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
21 /// Defines the cache configuration for an action.
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
;
30 private string varyByCustom
, varyByHeaders
, varyByParams
;
33 /// Initializes a new instance of the <see cref="CacheAttribute"/> class.
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;
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.
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.
58 /// If HttpCacheability is set to values other than NoCache or ServerAndNoCache, calling the SetAllowResponseInBrowserHistory method with either value for allow has no effect.
61 public bool AllowInHistory
63 get { return allowInHistory; }
64 set { allowInHistory = value; }
68 /// From MSDN: Sets cache expiration to from absolute to sliding.
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.
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.
82 public bool SlidingExpiration
84 get { return slidingExpiration; }
85 set { slidingExpiration = value; }
89 /// Specifies whether the ASP.NET cache should ignore HTTP Cache-Control
90 /// headers sent by the client that invalidate the cache.
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.
99 public bool ValidUntilExpires
101 get { return validUntilExpires; }
102 set { validUntilExpires = value; }
106 /// Sets the ETag HTTP header to the specified string.
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.
117 set { etag = value; }
121 /// Cache Duration (in seconds)
125 get { return duration; }
126 set { duration = value; }
130 /// Specifies a custom text string to vary cached output responses by.
132 public string VaryByCustom
134 get { return varyByCustom; }
135 set { varyByCustom = value; }
139 /// Gets or sets the list of all HTTP headers that will be used to vary cache output.
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.
145 public string VaryByHeaders
147 get { return varyByHeaders; }
148 set { varyByHeaders = value; }
152 /// Gets or sets the list of parameters received by an HTTP GET or HTTP POST that affect caching.
155 /// A separate version of the requested document is available from the cache
156 /// for each named parameter in the VaryByParams collection.
158 public string VaryByParams
160 get { return varyByParams; }
161 set { varyByParams = value; }
165 /// Configures ASP.Net's Cache policy based on properties set
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
);
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;
203 policy
.SetETag(etag
);