/// <summary> /// Store a result set mapping in a cache /// </summary> /// <typeparam name="T"></typeparam> /// <param name="cache"></param> /// <param name="key"></param> /// <param name="resultSetMapping"></param> private static void SetMappingCache <T>(ref ConcurrentDictionary <ResultSetMappingIdentity, ResultSetMappingCacheItem> cache, ResultSetMappingIdentity key, List <ColumnMapping> resultSetMapping) { if (!_cachingEnabled) { return; // short circuit if caching if disabled } cache[key] = ResultSetMappingCacheItem.CreateInstance <T>(resultSetMapping); }
/// <summary> /// Retrieve a result set mapping from a cache if it exists /// </summary> /// <param name="cache"></param> /// <param name="key"></param> /// <param name="resultSetMapping"></param> /// <returns></returns> private static bool TryGetMappingCache(ref ConcurrentDictionary <ResultSetMappingIdentity, ResultSetMappingCacheItem> cache, ResultSetMappingIdentity key, out List <ColumnMapping> resultSetMapping) { resultSetMapping = null; if (!_cachingEnabled) { return(false); // short circuit if caching if disabled } ResultSetMappingCacheItem cacheItem; if (cache.TryGetValue(key, out cacheItem)) { cacheItem.RecordHit(); resultSetMapping = cacheItem.Mappings; return(true); } return(false); }
/// <summary> /// Get the mappings for a given type and data reader. Caches in appropriate cache. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="reader"></param> /// <param name="mapByPosition"></param> /// <param name="allowUnmappedColumns"></param> /// <returns></returns> private static List <ColumnMapping> GetMappings <T>(OracleDataReader reader, bool mapByPosition, bool allowUnmappedColumns) { // determine which of the four caches should be used ConcurrentDictionary <ResultSetMappingIdentity, ResultSetMappingCacheItem> mappingCache = (mapByPosition ? (allowUnmappedColumns ? _mappingByPositionFlexibleCache : _mappingByPositionCache) : (allowUnmappedColumns ? _mappingByNameFlexibleCache : _mappingByNameCache)); // a unique id can be derived from the C# type and result set alone ResultSetMappingIdentity mappingKey = ResultSetMappingIdentity.CreateInstance <T>(reader); List <ColumnMapping> mappings; if (!TryGetMappingCache(ref mappingCache, mappingKey, out mappings)) { // if they are not in cache, then build mappings now and cache them mappings = BuildMappings <T>(reader, mapByPosition, allowUnmappedColumns); SetMappingCache <T>(ref mappingCache, mappingKey, mappings); } return(mappings); }