Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / ndp / fx / src / DataWeb / Server / System / Data / Services / Caching / MetadataCache.cs / 1 / MetadataCache.cs
//----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Provides a class to cache metadata information.
//
//
// @owner [....]
//---------------------------------------------------------------------
namespace System.Data.Services.Caching
{
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Threading;
using System.Data.Objects;
using System.Data.Common;
using System.Data.EntityClient;
///
/// Use this class to cache metadata through MetadataCacheItem instances.
///
internal static class MetadataCache
{
/// AppDomain-wide cache for metadata items.
private static Dictionary cache = new Dictionary(new MetadataCacheKey.Comparer());
/// Reader/writer lock for AppDomain .
private static ReaderWriterLockSlim cacheLock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
/// Adds a new cache item, and returns the item that is put in the cache.
/// Type of service with metadata being cached.
///
/// Data context instance being cached, possibly segmenting the cache
/// space for .
///
/// Item being added.
/// The item being put in the cache (possibly an existing one).
/// This method is thread-safe but not re-entrant.
internal static MetadataCacheItem AddCacheItem(Type serviceType, object dataContextInstance, MetadataCacheItem item)
{
Debug.Assert(serviceType != null, "serviceType != null");
Debug.Assert(dataContextInstance != null, "dataContextInstance != null");
Debug.Assert(item != null, "item != null");
MetadataCacheKey key = new MetadataCacheKey(serviceType, dataContextInstance as ObjectContext);
MetadataCacheItem result;
cacheLock.EnterWriteLock();
try
{
// If another thread beat the current thread, we return the
// previously created item, which has a higher chance of
// having survived a garbage collection already.
if (!cache.TryGetValue(key, out result))
{
cache.Add(key, item);
result = item;
}
}
finally
{
cacheLock.ExitWriteLock();
}
Debug.Assert(result != null, "result != null -- a null item is never returned.");
Debug.Assert(
result == TryLookup(serviceType, dataContextInstance),
"result == TryLookup(serviceType, dataContextInstance) -- instance from cache is being returned.");
return result;
}
/// Tries to look up metadata for the specifed service type and context instance.
/// Type of service with metadata being cached.
///
/// Data context instance being cached, possibly segmenting the cache
/// space for .
///
/// The cached metadata item, if one exists.
/// This method is thread-safe but not re-entrant.
internal static MetadataCacheItem TryLookup(Type serviceType, object dataContextInstance)
{
Debug.Assert(serviceType != null, "serviceType != null");
Debug.Assert(dataContextInstance != null, "dataContextInstance != null");
MetadataCacheKey key = new MetadataCacheKey(serviceType, dataContextInstance as ObjectContext);
MetadataCacheItem result;
cacheLock.EnterReadLock();
try
{
cache.TryGetValue(key, out result);
}
finally
{
cacheLock.ExitReadLock();
}
return result;
}
/// This type is used as the key in the metadata cache.
internal struct MetadataCacheKey
{
/// Connection string used to segment service type.
private readonly string dataContextConnection;
/// Hash code for this instance.
private readonly int hashCode;
/// Service type.
private readonly Type serviceType;
/// Initializes a new MetadataCacheKey instance.
/// Service type for key.
/// Data context instace for key, possibly null.
internal MetadataCacheKey(Type serviceType, ObjectContext dataContextInstance)
{
Debug.Assert(serviceType != null, "serviceType != null");
this.serviceType = serviceType;
this.dataContextConnection = null;
this.hashCode = this.serviceType.GetHashCode();
if (dataContextInstance != null)
{
EntityConnection connection = dataContextInstance.Connection as EntityConnection;
if (connection != null)
{
this.dataContextConnection = new EntityConnectionStringBuilder(connection.ConnectionString).Metadata;
this.hashCode ^= this.dataContextConnection.GetHashCode();
}
}
}
/// Comparer for metadata cache keys.
internal class Comparer : IEqualityComparer
{
/// Compares the specified keys.
/// First key.
/// Second key.
/// true if equals , false otherwise.
public bool Equals(MetadataCacheKey x, MetadataCacheKey y)
{
return x.dataContextConnection == y.dataContextConnection && x.serviceType == y.serviceType;
}
/// Gets the hash code for the object.
/// Object.
/// The hash code for this key.
public int GetHashCode(MetadataCacheKey obj)
{
return obj.hashCode;
}
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Provides a class to cache metadata information.
//
//
// @owner [....]
//---------------------------------------------------------------------
namespace System.Data.Services.Caching
{
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Threading;
using System.Data.Objects;
using System.Data.Common;
using System.Data.EntityClient;
///
/// Use this class to cache metadata through MetadataCacheItem instances.
///
internal static class MetadataCache
{
/// AppDomain-wide cache for metadata items.
private static Dictionary cache = new Dictionary(new MetadataCacheKey.Comparer());
/// Reader/writer lock for AppDomain .
private static ReaderWriterLockSlim cacheLock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
/// Adds a new cache item, and returns the item that is put in the cache.
/// Type of service with metadata being cached.
///
/// Data context instance being cached, possibly segmenting the cache
/// space for .
///
/// Item being added.
/// The item being put in the cache (possibly an existing one).
/// This method is thread-safe but not re-entrant.
internal static MetadataCacheItem AddCacheItem(Type serviceType, object dataContextInstance, MetadataCacheItem item)
{
Debug.Assert(serviceType != null, "serviceType != null");
Debug.Assert(dataContextInstance != null, "dataContextInstance != null");
Debug.Assert(item != null, "item != null");
MetadataCacheKey key = new MetadataCacheKey(serviceType, dataContextInstance as ObjectContext);
MetadataCacheItem result;
cacheLock.EnterWriteLock();
try
{
// If another thread beat the current thread, we return the
// previously created item, which has a higher chance of
// having survived a garbage collection already.
if (!cache.TryGetValue(key, out result))
{
cache.Add(key, item);
result = item;
}
}
finally
{
cacheLock.ExitWriteLock();
}
Debug.Assert(result != null, "result != null -- a null item is never returned.");
Debug.Assert(
result == TryLookup(serviceType, dataContextInstance),
"result == TryLookup(serviceType, dataContextInstance) -- instance from cache is being returned.");
return result;
}
/// Tries to look up metadata for the specifed service type and context instance.
/// Type of service with metadata being cached.
///
/// Data context instance being cached, possibly segmenting the cache
/// space for .
///
/// The cached metadata item, if one exists.
/// This method is thread-safe but not re-entrant.
internal static MetadataCacheItem TryLookup(Type serviceType, object dataContextInstance)
{
Debug.Assert(serviceType != null, "serviceType != null");
Debug.Assert(dataContextInstance != null, "dataContextInstance != null");
MetadataCacheKey key = new MetadataCacheKey(serviceType, dataContextInstance as ObjectContext);
MetadataCacheItem result;
cacheLock.EnterReadLock();
try
{
cache.TryGetValue(key, out result);
}
finally
{
cacheLock.ExitReadLock();
}
return result;
}
/// This type is used as the key in the metadata cache.
internal struct MetadataCacheKey
{
/// Connection string used to segment service type.
private readonly string dataContextConnection;
/// Hash code for this instance.
private readonly int hashCode;
/// Service type.
private readonly Type serviceType;
/// Initializes a new MetadataCacheKey instance.
/// Service type for key.
/// Data context instace for key, possibly null.
internal MetadataCacheKey(Type serviceType, ObjectContext dataContextInstance)
{
Debug.Assert(serviceType != null, "serviceType != null");
this.serviceType = serviceType;
this.dataContextConnection = null;
this.hashCode = this.serviceType.GetHashCode();
if (dataContextInstance != null)
{
EntityConnection connection = dataContextInstance.Connection as EntityConnection;
if (connection != null)
{
this.dataContextConnection = new EntityConnectionStringBuilder(connection.ConnectionString).Metadata;
this.hashCode ^= this.dataContextConnection.GetHashCode();
}
}
}
/// Comparer for metadata cache keys.
internal class Comparer : IEqualityComparer
{
/// Compares the specified keys.
/// First key.
/// Second key.
/// true if equals , false otherwise.
public bool Equals(MetadataCacheKey x, MetadataCacheKey y)
{
return x.dataContextConnection == y.dataContextConnection && x.serviceType == y.serviceType;
}
/// Gets the hash code for the object.
/// Object.
/// The hash code for this key.
public int GetHashCode(MetadataCacheKey obj)
{
return obj.hashCode;
}
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- TerminatorSinks.cs
- ComponentGuaranteesAttribute.cs
- ItemContainerGenerator.cs
- ComEventsHelper.cs
- FileDialogCustomPlace.cs
- DataColumnPropertyDescriptor.cs
- hwndwrapper.cs
- BypassElement.cs
- CompModSwitches.cs
- WindowsFormsEditorServiceHelper.cs
- LostFocusEventManager.cs
- Ray3DHitTestResult.cs
- DataMemberConverter.cs
- ProjectionRewriter.cs
- TextCollapsingProperties.cs
- _BaseOverlappedAsyncResult.cs
- HtmlControlDesigner.cs
- WindowsScrollBarBits.cs
- TextDecorationLocationValidation.cs
- DesignerOptionService.cs
- CodeStatementCollection.cs
- Column.cs
- X500Name.cs
- FormsAuthenticationConfiguration.cs
- COM2ExtendedBrowsingHandler.cs
- DescendantBaseQuery.cs
- TemplateEditingFrame.cs
- StringAnimationBase.cs
- XslAstAnalyzer.cs
- Bezier.cs
- ReadOnlyHierarchicalDataSource.cs
- DbTransaction.cs
- CommandField.cs
- OdbcEnvironment.cs
- SafeRightsManagementSessionHandle.cs
- XmlEncoding.cs
- InstanceDataCollection.cs
- TrackingMemoryStreamFactory.cs
- WebBrowserContainer.cs
- SystemEvents.cs
- ValidationHelpers.cs
- DictionaryItemsCollection.cs
- Polyline.cs
- ProfileProvider.cs
- ObjectDataSourceMethodEditor.cs
- DLinqAssociationProvider.cs
- ParseHttpDate.cs
- ButtonAutomationPeer.cs
- PolicyException.cs
- ProcessInputEventArgs.cs
- SetIterators.cs
- ObjectHandle.cs
- XmlAttributeProperties.cs
- BinaryMethodMessage.cs
- TdsEnums.cs
- SymbolType.cs
- DataTableReaderListener.cs
- ListDictionaryInternal.cs
- HttpServerProtocol.cs
- MenuRendererStandards.cs
- Page.cs
- RawContentTypeMapper.cs
- EntityDataSourceViewSchema.cs
- DropDownHolder.cs
- DrawingAttributesDefaultValueFactory.cs
- DSGeneratorProblem.cs
- TreeViewAutomationPeer.cs
- ListCardsInFileRequest.cs
- TemplateControl.cs
- InkSerializer.cs
- RoleManagerModule.cs
- QilScopedVisitor.cs
- StylusPointPropertyInfo.cs
- SoundPlayer.cs
- GeometryDrawing.cs
- NullToBooleanConverter.cs
- ComplusEndpointConfigContainer.cs
- GeneralTransform3D.cs
- WindowsButton.cs
- MenuItem.cs
- SearchForVirtualItemEventArgs.cs
- EntryWrittenEventArgs.cs
- TypeDescriptorFilterService.cs
- NoResizeSelectionBorderGlyph.cs
- CacheOutputQuery.cs
- XsltArgumentList.cs
- TdsParameterSetter.cs
- ProcessThread.cs
- ProcessingInstructionAction.cs
- CoTaskMemHandle.cs
- CustomActivityDesigner.cs
- IisHelper.cs
- CanExecuteRoutedEventArgs.cs
- RuntimeIdentifierPropertyAttribute.cs
- InvocationExpression.cs
- CalendarTable.cs
- WebPartChrome.cs
- DLinqDataModelProvider.cs
- CodeParameterDeclarationExpression.cs
- TextSearch.cs