Code:
/ FXUpdate3074 / FXUpdate3074 / 1.1 / DEVDIV / depot / DevDiv / releases / whidbey / QFE / ndp / fx / src / xsp / System / Web / UI / DataSourceCache.cs / 2 / DataSourceCache.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
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;
///
/// The duration, in seconds, of the expiration. The expiration policy is specified by the ExpirationPolicy property.
///
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;
}
}
///
/// Whether caching is enabled for this data source.
///
public virtual bool Enabled {
get {
object o = ViewState["Enabled"];
if (o != null)
return (bool)o;
return false;
}
set {
ViewState["Enabled"] = value;
}
}
///
/// The expiration policy of the cache. The duration for the expiration is specified by the Duration property.
///
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;
}
}
///
/// 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.
///
[
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;
}
}
///
/// 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.
///
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
]
protected StateBag ViewState {
get {
if (_viewState == null) {
_viewState = new StateBag();
if (_tracking)
_viewState.TrackViewState();
}
return _viewState;
}
}
///
/// 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.
///
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.CacheInternal.Remove(key);
}
///
/// Loads data from the ASP.NET cache using the specified key.
///
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.CacheInternal.Get(key);
}
///
/// Loads the state of the DataSourceCache object.
///
protected virtual void LoadViewState(object savedState) {
if (savedState != null) {
((IStateManager)ViewState).LoadViewState(savedState);
}
}
///
/// Saves data to the ASP.NET cache using the specified key.
///
public void SaveDataToCache(string key, object data) {
SaveDataToCache(key, data, null);
}
///
/// Saves data to the ASP.NET cache using the specified key and makes
/// this entry dependent on the specified dependency.
///
public void SaveDataToCache(string key, object data, CacheDependency dependency) {
SaveDataToCacheInternal(key, data, dependency);
}
///
/// 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.).
///
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.CacheInternal.UtcInsert(key, data, aggregateCacheDependency, utcAbsoluteExpiryTime, slidingExpiryTimeSpan);
}
///
/// Saves the current state of the DataSourceCache object.
///
protected virtual object SaveViewState() {
return (_viewState != null ? ((IStateManager)_viewState).SaveViewState() : null);
}
///
/// Starts tracking view state.
///
protected void TrackViewState() {
_tracking = true;
if (_viewState != null) {
_viewState.TrackViewState();
}
}
#region IStateManager implementation
///
bool IStateManager.IsTrackingViewState {
get {
return _tracking;
}
}
///
void IStateManager.LoadViewState(object savedState) {
LoadViewState(savedState);
}
///
object IStateManager.SaveViewState() {
return SaveViewState();
}
///
void IStateManager.TrackViewState() {
TrackViewState();
}
#endregion
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
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;
///
/// The duration, in seconds, of the expiration. The expiration policy is specified by the ExpirationPolicy property.
///
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;
}
}
///
/// Whether caching is enabled for this data source.
///
public virtual bool Enabled {
get {
object o = ViewState["Enabled"];
if (o != null)
return (bool)o;
return false;
}
set {
ViewState["Enabled"] = value;
}
}
///
/// The expiration policy of the cache. The duration for the expiration is specified by the Duration property.
///
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;
}
}
///
/// 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.
///
[
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;
}
}
///
/// 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.
///
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
]
protected StateBag ViewState {
get {
if (_viewState == null) {
_viewState = new StateBag();
if (_tracking)
_viewState.TrackViewState();
}
return _viewState;
}
}
///
/// 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.
///
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.CacheInternal.Remove(key);
}
///
/// Loads data from the ASP.NET cache using the specified key.
///
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.CacheInternal.Get(key);
}
///
/// Loads the state of the DataSourceCache object.
///
protected virtual void LoadViewState(object savedState) {
if (savedState != null) {
((IStateManager)ViewState).LoadViewState(savedState);
}
}
///
/// Saves data to the ASP.NET cache using the specified key.
///
public void SaveDataToCache(string key, object data) {
SaveDataToCache(key, data, null);
}
///
/// Saves data to the ASP.NET cache using the specified key and makes
/// this entry dependent on the specified dependency.
///
public void SaveDataToCache(string key, object data, CacheDependency dependency) {
SaveDataToCacheInternal(key, data, dependency);
}
///
/// 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.).
///
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.CacheInternal.UtcInsert(key, data, aggregateCacheDependency, utcAbsoluteExpiryTime, slidingExpiryTimeSpan);
}
///
/// Saves the current state of the DataSourceCache object.
///
protected virtual object SaveViewState() {
return (_viewState != null ? ((IStateManager)_viewState).SaveViewState() : null);
}
///
/// Starts tracking view state.
///
protected void TrackViewState() {
_tracking = true;
if (_viewState != null) {
_viewState.TrackViewState();
}
}
#region IStateManager implementation
///
bool IStateManager.IsTrackingViewState {
get {
return _tracking;
}
}
///
void IStateManager.LoadViewState(object savedState) {
LoadViewState(savedState);
}
///
object IStateManager.SaveViewState() {
return SaveViewState();
}
///
void IStateManager.TrackViewState() {
TrackViewState();
}
#endregion
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- OneOf.cs
- TokenCreationException.cs
- DirectoryNotFoundException.cs
- ChangeBlockUndoRecord.cs
- BroadcastEventHelper.cs
- Stacktrace.cs
- UniformGrid.cs
- Options.cs
- HtmlContainerControl.cs
- KnownAssemblyEntry.cs
- ExceptionNotification.cs
- RijndaelManagedTransform.cs
- ProxyWebPart.cs
- DataGridColumnReorderingEventArgs.cs
- BamlResourceSerializer.cs
- HostProtectionException.cs
- ReaderOutput.cs
- SessionStateContainer.cs
- MatrixKeyFrameCollection.cs
- CompoundFileStorageReference.cs
- SchemaMerger.cs
- ConnectionConsumerAttribute.cs
- ObjectDataSourceView.cs
- CodeCatchClause.cs
- InteropBitmapSource.cs
- DataKeyArray.cs
- AssemblyName.cs
- OrderablePartitioner.cs
- UriScheme.cs
- NCryptSafeHandles.cs
- XamlReaderHelper.cs
- Pens.cs
- StreamInfo.cs
- InlineCollection.cs
- ListBox.cs
- WindowsMenu.cs
- Bidi.cs
- CodeDomDesignerLoader.cs
- NullRuntimeConfig.cs
- BaseHashHelper.cs
- ComplexTypeEmitter.cs
- TemplateControl.cs
- HttpFileCollection.cs
- TextReturnReader.cs
- ValidatorCompatibilityHelper.cs
- Recipient.cs
- CurrentChangingEventManager.cs
- IndentedTextWriter.cs
- GPStream.cs
- TakeQueryOptionExpression.cs
- StringComparer.cs
- MergeEnumerator.cs
- WebPartEditVerb.cs
- EpmContentSerializerBase.cs
- RegexCompilationInfo.cs
- RelationshipEndCollection.cs
- WindowsIdentity.cs
- PerformanceCounterCategory.cs
- ComplexBindingPropertiesAttribute.cs
- ProbeMatchesCD1.cs
- RestHandler.cs
- QilFunction.cs
- XmlSchemaAnnotated.cs
- UpdatePanelTrigger.cs
- FolderBrowserDialog.cs
- ProgressBarAutomationPeer.cs
- SqlConnectionStringBuilder.cs
- DataGridViewSelectedCellCollection.cs
- PointLight.cs
- WizardForm.cs
- EmbeddedObject.cs
- SystemWebSectionGroup.cs
- KeyValuePairs.cs
- SoapCodeExporter.cs
- StickyNoteAnnotations.cs
- ReaderWriterLock.cs
- AuthenticatedStream.cs
- CodeIdentifier.cs
- InternalPermissions.cs
- PenContext.cs
- BCryptSafeHandles.cs
- OpenFileDialog.cs
- HtmlTextBoxAdapter.cs
- ObjectPersistData.cs
- ServiceChannelFactory.cs
- UriSection.cs
- MenuItemBindingCollection.cs
- SByte.cs
- ToggleProviderWrapper.cs
- XmlEventCache.cs
- StringKeyFrameCollection.cs
- DrawToolTipEventArgs.cs
- SQLGuidStorage.cs
- PropertyValidationContext.cs
- ServiceEndpointElementCollection.cs
- URI.cs
- DataGridViewColumn.cs
- SerializationObjectManager.cs
- ContextStack.cs
- RegisteredDisposeScript.cs