1 //
2 // System.Web.UI.ControlCachePolicy
3 //
4 // Authors:
5 //	Dick Porter  <dick@ximian.com>
6 //      Marek Habersack <mhabersack@novell.com>
7 //
8 // Copyright (C) 2005-2010 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29 
30 using System.Web.Caching;
31 
32 namespace System.Web.UI
33 {
34 	public sealed class ControlCachePolicy
35 	{
36 		BasePartialCachingControl bpcc;
37 		bool cached;
38 
ControlCachePolicy()39 		internal ControlCachePolicy () : this (null)
40 		{
41 		}
42 
ControlCachePolicy(BasePartialCachingControl bpcc)43 		internal ControlCachePolicy (BasePartialCachingControl bpcc)
44 		{
45 			this.bpcc = bpcc;
46 		}
47 
48 		public bool Cached
49 		{
50 			get {
51 				AssertBasePartialCachingControl ();
52 				return cached;
53 			}
54 
55 			set {
56 				AssertBasePartialCachingControl ();
57 				cached = value;
58 			}
59 		}
60 
61 		public CacheDependency Dependency
62 		{
63 			get {
64 				AssertBasePartialCachingControl ();
65 				return bpcc.Dependency;
66 			}
67 			set {
68 				AssertBasePartialCachingControl ();
69 				bpcc.Dependency = value;
70 			}
71 		}
72 
73 		public TimeSpan Duration
74 		{
75 			get {
76 				AssertBasePartialCachingControl ();
77 				return TimeSpan.FromMinutes (bpcc.Duration);
78 			}
79 			set {
80 				AssertBasePartialCachingControl ();
81 				bpcc.Duration = value.Minutes;
82 			}
83 		}
84 		public string ProviderName {
85 			get {
86 				AssertBasePartialCachingControl ();
87 				return bpcc.ProviderName;
88 			}
89 
90 			set {
91 				AssertBasePartialCachingControl ();
92 				bpcc.ProviderName = value;
93 			}
94 		}
95 		public bool SupportsCaching
96 		{
97 			get {
98 				return bpcc != null;
99 			}
100 		}
101 
102 		public string VaryByControl
103 		{
104 			get {
105 				AssertBasePartialCachingControl ();
106 				return bpcc.VaryByControls;
107 			}
108 			set {
109 				AssertBasePartialCachingControl ();
110 				bpcc.VaryByControls = value;
111 			}
112 		}
113 
114 		public HttpCacheVaryByParams VaryByParams {
115 			get {
116 				AssertBasePartialCachingControl ();
117 				throw new NotImplementedException ();
118 			}
119 		}
120 
SetExpires(DateTime expirationTime)121 		public void SetExpires (DateTime expirationTime)
122 		{
123 			AssertBasePartialCachingControl ();
124 			bpcc.ExpirationTime = expirationTime;
125 		}
126 
SetSlidingExpiration(bool useSlidingExpiration)127 		public void SetSlidingExpiration (bool useSlidingExpiration)
128 		{
129 			AssertBasePartialCachingControl ();
130 			bpcc.SlidingExpiration = useSlidingExpiration;
131 		}
132 
SetVaryByCustom(string varyByCustom)133 		public void SetVaryByCustom (string varyByCustom)
134 		{
135 			AssertBasePartialCachingControl ();
136 			bpcc.VaryByCustom = varyByCustom;
137 		}
138 
AssertBasePartialCachingControl()139 		void AssertBasePartialCachingControl ()
140 		{
141 			if (bpcc == null)
142 				throw new HttpException ("The user control is not associated with a 'BasePartialCachingControl' and is not cacheable.");
143 		}
144 
145 	}
146 }
147