File: DataSourceCache.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (287 lines) | stat: -rw-r--r-- 10,266 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//------------------------------------------------------------------------------
// <copyright file="DataSourceCache.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web.UI {

    using System.ComponentModel;
    using System.Web.Caching;
    using System.Web.Util;


    internal class DataSourceCache : IStateManager {


        public const int Infinite = 0;

        private bool _tracking;
        private StateBag _viewState;



        /// <devdoc>
        /// The duration, in seconds, of the expiration. The expiration policy is specified by the ExpirationPolicy property.
        /// </devdoc>
        public virtual int Duration {
            get {
                object o = ViewState["Duration"];
                if (o != null)
                    return (int)o;
                return Infinite;
            }
            set {
                if (value < 0) {
                    throw new ArgumentOutOfRangeException("value", SR.GetString(SR.DataSourceCache_InvalidDuration));
                }
                ViewState["Duration"] = value;
            }
        }


        /// <devdoc>
        /// Whether caching is enabled for this data source.
        /// </devdoc>
        public virtual bool Enabled {
            get {
                object o = ViewState["Enabled"];
                if (o != null)
                    return (bool)o;
                return false;
            }
            set {
                ViewState["Enabled"] = value;
            }
        }


        /// <devdoc>
        /// The expiration policy of the cache. The duration for the expiration is specified by the Duration property.
        /// </devdoc>
        public virtual DataSourceCacheExpiry ExpirationPolicy {
            get {
                object o = ViewState["ExpirationPolicy"];
                if (o != null)
                    return (DataSourceCacheExpiry)o;
                return DataSourceCacheExpiry.Absolute;
            }
            set {
                if (value < DataSourceCacheExpiry.Absolute || value > DataSourceCacheExpiry.Sliding) {
                    throw new ArgumentOutOfRangeException(SR.GetString(SR.DataSourceCache_InvalidExpiryPolicy));
                }
                ViewState["ExpirationPolicy"] = value;
            }
        }


        /// <devdoc>
        /// Indicates an arbitrary cache key to make this cache entry depend on. This allows
        /// the user to further customize when this cache entry will expire.
        /// </devdoc>
        [
        DefaultValue(""),
        NotifyParentProperty(true),
        WebSysDescription(SR.DataSourceCache_KeyDependency),
        ]
        public virtual string KeyDependency {
            get {
                object o = ViewState["KeyDependency"];
                if (o != null)
                    return (string)o;
                return String.Empty;
            }
            set {
                ViewState["KeyDependency"] = value;
            }
        }


        /// <devdoc>
        /// Indicates a dictionary of state information that allows you to save and restore
        /// the state of an object across multiple requests for the same page.
        /// </devdoc>
        [
        Browsable(false),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
        ]
        protected StateBag ViewState {
            get {
                if (_viewState == null) {
                    _viewState = new StateBag();
                    if (_tracking)
                        _viewState.TrackViewState();
                }
                return _viewState;
            }
        }



        /// <devdoc>
        /// Invalidates an ASP.NET cache entry using the specified key.
        /// SECURITY: This method should never accept user-defined inputs
        /// because it invalidates the internal ASP.net cache.
        /// </devdoc>
        public void Invalidate(string key) {
            if (String.IsNullOrEmpty(key)) {
                throw new ArgumentNullException("key");
            }

            Debug.Assert(key.StartsWith(CacheInternal.PrefixDataSourceControl, StringComparison.Ordinal), "All keys passed in should start with the prefix specified in CacheInternal.PrefixDataSourceControl.");

            if (!Enabled) {
                throw new InvalidOperationException(SR.GetString(SR.DataSourceCache_CacheMustBeEnabled));
            }

            HttpRuntime.Cache.InternalCache.Remove(key);
        }


        /// <devdoc>
        /// Loads data from the ASP.NET cache using the specified key.
        /// </devdoc>
        public object LoadDataFromCache(string key) {
            if (String.IsNullOrEmpty(key)) {
                throw new ArgumentNullException("key");
            }

            Debug.Assert(key.StartsWith(CacheInternal.PrefixDataSourceControl, StringComparison.Ordinal), "All keys passed in should start with the prefix specified in CacheInternal.PrefixDataSourceControl.");

            if (!Enabled) {
                throw new InvalidOperationException(SR.GetString(SR.DataSourceCache_CacheMustBeEnabled));
            }

            return HttpRuntime.Cache.InternalCache.Get(key);
        }


        /// <devdoc>
        /// Loads the state of the DataSourceCache object.
        /// </devdoc>
        protected virtual void LoadViewState(object savedState) {
            if (savedState != null) {
                ((IStateManager)ViewState).LoadViewState(savedState);
            }
        }


        /// <devdoc>
        /// Saves data to the ASP.NET cache using the specified key.
        /// </devdoc>
        public void SaveDataToCache(string key, object data) {
            SaveDataToCache(key, data, null);
        }


        /// <devdoc>
        /// Saves data to the ASP.NET cache using the specified key and makes
        /// this entry dependent on the specified dependency.
        /// </devdoc>
        public void SaveDataToCache(string key, object data, CacheDependency dependency) {
            SaveDataToCacheInternal(key, data, dependency);
        }


        /// <devdoc>
        /// Saves data to the ASP.NET cache using the specified key, and makes
        /// it dependent on the specified CacheDependency object.
        /// Override this method if you need to create your own cache dependencies
        /// and call this base implementation to actually save the data to the
        /// cache with the standard properties (expiration policy, duration, etc.).
        /// </devdoc>
        protected virtual void SaveDataToCacheInternal(string key, object data, CacheDependency dependency) {
            if (String.IsNullOrEmpty(key)) {
                throw new ArgumentNullException("key");
            }

            Debug.Assert(key.StartsWith(CacheInternal.PrefixDataSourceControl, StringComparison.Ordinal), "All keys passed in should start with the prefix specified in CacheInternal.PrefixDataSourceControl.");

            if (!Enabled) {
                throw new InvalidOperationException(SR.GetString(SR.DataSourceCache_CacheMustBeEnabled));
            }

            DateTime utcAbsoluteExpiryTime = Cache.NoAbsoluteExpiration;
            TimeSpan slidingExpiryTimeSpan = Cache.NoSlidingExpiration;
            switch (ExpirationPolicy) {
                case DataSourceCacheExpiry.Absolute:
                    // The caching APIs for absolute expiry expect a duration of 0 to mean no expiry,
                    // but for us it means infinite so we use Int32.MaxValue instead.
                    utcAbsoluteExpiryTime = DateTime.UtcNow.AddSeconds(Duration == 0 ? Int32.MaxValue : Duration);
                    break;
                case DataSourceCacheExpiry.Sliding:
                    slidingExpiryTimeSpan = TimeSpan.FromSeconds(Duration);
                    break;
            }

            AggregateCacheDependency aggregateCacheDependency = new AggregateCacheDependency();

            // Set up key dependency, if any
            string[] keyDependencies = null;
            if (KeyDependency.Length > 0) {
                keyDependencies = new string[] { KeyDependency };
                aggregateCacheDependency.Add(new CacheDependency[] { new CacheDependency(null, keyDependencies) });
            }

            // If there are any additional dependencies, create a new CacheDependency for them
            if (dependency != null) {
                aggregateCacheDependency.Add(new CacheDependency[] { dependency });
            }

            HttpRuntime.Cache.InternalCache.Insert(key, data, new CacheInsertOptions() {
                                                                Dependencies = aggregateCacheDependency,
                                                                AbsoluteExpiration = utcAbsoluteExpiryTime,
                                                                SlidingExpiration = slidingExpiryTimeSpan
                                                            });
        }


        /// <devdoc>
        /// Saves the current state of the DataSourceCache object.
        /// </devdoc>
        protected virtual object SaveViewState() {
            return (_viewState != null ? ((IStateManager)_viewState).SaveViewState() : null);
        }


        /// <devdoc>
        /// Starts tracking view state.
        /// </devdoc>
        protected void TrackViewState() {
            _tracking = true;

            if (_viewState != null) {
                _viewState.TrackViewState();
            }
        }


        #region IStateManager implementation

        /// <internalonly/>
        bool IStateManager.IsTrackingViewState {
            get {
                return _tracking;
            }
        }


        /// <internalonly/>
        void IStateManager.LoadViewState(object savedState) {
            LoadViewState(savedState);
        }


        /// <internalonly/>
        object IStateManager.SaveViewState() {
            return SaveViewState();
        }


        /// <internalonly/>
        void IStateManager.TrackViewState() {
            TrackViewState();
        }
        #endregion
    }
}