Exemplo n.º 1
0
        /// <summary>
        /// Attempt to cache objects, after loading them from the database.
        /// </summary>
        /// <param name="queryCache">The cache.</param>
        /// <param name="keys">The query keys.</param>
        /// <param name="queryParameters">The array of query parameters matching <paramref name="keys"/>.</param>
        /// <param name="returnTypes">The array of query result row types matching <paramref name="keys"/>.</param>
        /// <param name="results">The array of query results matching <paramref name="keys"/>.</param>
        /// <param name="session">The session for which the queries were executed.</param>
        /// <returns>An array of boolean indicating if each query was successfully cached.</returns>
        /// <exception cref="CacheException"></exception>
        public static bool[] PutMany(
            this IQueryCache queryCache,
            QueryKey[] keys,
            QueryParameters[] queryParameters,
            ICacheAssembler[][] returnTypes,
            IList[] results,
            ISessionImplementor session)
        {
            if (queryCache is IBatchableQueryCache batchableQueryCache)
            {
                return(batchableQueryCache.PutMany(
                           keys,
                           queryParameters,
                           returnTypes,
                           results,
                           session));
            }

            var puts = new bool[keys.Length];

            for (var i = 0; i < keys.Length; i++)
            {
                puts[i] = queryCache.Put(keys[i], queryParameters[i], returnTypes[i], results[i], session);
            }

            return(puts);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Put query results in the cache.
        /// </summary>
        /// <param name="queryCache">The cache.</param>
        /// <param name="key">The query key.</param>
        /// <param name="queryParameters">The query parameters.</param>
        /// <param name="returnTypes">The query result row types.</param>
        /// <param name="result">The query result.</param>
        /// <param name="session">The session for which the query was executed.</param>
        /// <returns><see langword="true" /> if the result has been cached, <see langword="false" />
        /// otherwise.</returns>
        public static bool Put(
            this IQueryCache queryCache,
            QueryKey key,
            QueryParameters queryParameters,
            ICacheAssembler[] returnTypes,
            IList result,
            ISessionImplementor session)
        {
            if (queryCache is IBatchableQueryCache batchableQueryCache)
            {
                return(batchableQueryCache.Put(
                           key, queryParameters,
                           returnTypes,
                           result, session));
            }

#pragma warning disable 618
            return(queryCache.Put(
#pragma warning restore 618
                       key,
                       returnTypes,
                       result,
                       queryParameters.NaturalKeyLookup,
                       session));
        }
Exemplo n.º 3
0
        private IList ListUsingQueryCache(HashSet <string> querySpaces)
        {
            IQueryCache queryCache = session.Factory.GetQueryCache(cacheRegion);

            ISet <FilterKey> filterKeys = FilterKey.CreateFilterKeys(session.EnabledFilters);

            List <IType[]> resultTypesList = new List <IType[]>();

            int[] maxRows   = new int[loaders.Count];
            int[] firstRows = new int[loaders.Count];
            for (int i = 0; i < loaders.Count; i++)
            {
                resultTypesList.Add(loaders[i].ResultTypes);
                firstRows[i] = parameters[i].RowSelection.FirstRow;
                maxRows[i]   = parameters[i].RowSelection.MaxRows;
            }

            MultipleQueriesCacheAssembler assembler = new MultipleQueriesCacheAssembler(resultTypesList);
            QueryParameters combinedParameters      = CreateCombinedQueryParameters();
            QueryKey        key = new QueryKey(session.Factory, SqlString, combinedParameters, filterKeys, null)
                                  .SetFirstRows(firstRows)
                                  .SetMaxRows(maxRows);

            IList result =
                assembler.GetResultFromQueryCache(session,
                                                  combinedParameters,
                                                  querySpaces,
                                                  queryCache,
                                                  key);

            if (factory.Statistics.IsStatisticsEnabled)
            {
                if (result == null)
                {
                    factory.StatisticsImplementor.QueryCacheMiss(key.ToString(), queryCache.RegionName);
                }
                else
                {
                    factory.StatisticsImplementor.QueryCacheHit(key.ToString(), queryCache.RegionName);
                }
            }

            if (result == null)
            {
                log.Debug("Cache miss for multi criteria query");
                IList list = DoList();
                result = list;
                if (session.CacheMode.HasFlag(CacheMode.Put))
                {
                    bool put = queryCache.Put(key, combinedParameters, new ICacheAssembler[] { assembler }, new object[] { list }, session);
                    if (put && factory.Statistics.IsStatisticsEnabled)
                    {
                        factory.StatisticsImplementor.QueryCachePut(key.ToString(), queryCache.RegionName);
                    }
                }
            }

            return(GetResultList(result));
        }
Exemplo n.º 4
0
        private IList ListUsingQueryCache()
        {
            IQueryCache queryCache = session.Factory.GetQueryCache(cacheRegion);

            ISet <FilterKey> filterKeys = FilterKey.CreateFilterKeys(session.EnabledFilters);

            ISet <string>  querySpaces     = new HashSet <string>();
            List <IType[]> resultTypesList = new List <IType[]>(Translators.Count);

            for (int i = 0; i < Translators.Count; i++)
            {
                ITranslator queryTranslator = Translators[i];
                querySpaces.UnionWith(queryTranslator.QuerySpaces);
                resultTypesList.Add(queryTranslator.ReturnTypes);
            }
            int[] firstRows = new int[Parameters.Count];
            int[] maxRows   = new int[Parameters.Count];
            for (int i = 0; i < Parameters.Count; i++)
            {
                RowSelection rowSelection = Parameters[i].RowSelection;
                firstRows[i] = rowSelection.FirstRow;
                maxRows[i]   = rowSelection.MaxRows;
            }

            MultipleQueriesCacheAssembler assembler = new MultipleQueriesCacheAssembler(resultTypesList);

            QueryKey key = new QueryKey(session.Factory, SqlString, combinedParameters, filterKeys, null)
                           .SetFirstRows(firstRows)
                           .SetMaxRows(maxRows);

            IList result = assembler.GetResultFromQueryCache(session, combinedParameters, querySpaces, queryCache, key);

            if (result == null)
            {
                log.Debug("Cache miss for multi query");
                var list = DoList();
                queryCache.Put(key, new ICacheAssembler[] { assembler }, new object[] { list }, false, session);
                result = list;
            }

            return(GetResultList(result));
        }
Exemplo n.º 5
0
        private IList ListUsingQueryCache()
        {
            IQueryCache queryCache = session.Factory.GetQueryCache(cacheRegion);

            ISet <FilterKey> filterKeys = FilterKey.CreateFilterKeys(session.EnabledFilters, session.EntityMode);

            ISet <string>  querySpaces     = new HashSet <string>();
            List <IType[]> resultTypesList = new List <IType[]>();

            int[] maxRows   = new int[loaders.Count];
            int[] firstRows = new int[loaders.Count];
            for (int i = 0; i < loaders.Count; i++)
            {
                querySpaces.UnionWith(loaders[i].QuerySpaces);
                resultTypesList.Add(loaders[i].ResultTypes);
                firstRows[i] = parameters[i].RowSelection.FirstRow;
                maxRows[i]   = parameters[i].RowSelection.MaxRows;
            }

            MultipleQueriesCacheAssembler assembler = new MultipleQueriesCacheAssembler(resultTypesList);
            QueryParameters combinedParameters      = CreateCombinedQueryParameters();
            QueryKey        key = new QueryKey(session.Factory, SqlString, combinedParameters, filterKeys, null)
                                  .SetFirstRows(firstRows)
                                  .SetMaxRows(maxRows);

            IList result =
                assembler.GetResultFromQueryCache(session,
                                                  combinedParameters,
                                                  querySpaces,
                                                  queryCache,
                                                  key);

            if (result == null)
            {
                log.Debug("Cache miss for multi criteria query");
                IList list = DoList();
                queryCache.Put(key, new ICacheAssembler[] { assembler }, new object[] { list }, combinedParameters.NaturalKeyLookup, session);
                result = list;
            }

            return(GetResultList(result));
        }
Exemplo n.º 6
0
        public virtual DataSet GetDataSet(string sql, IDbDataParameter[] paramters = null)
        {
            QueryKey key    = new QueryKey(sql, new QueryParameters(paramters));
            DataSet  result = (DataSet)cache.Get(key);

            if (result != null)
            {
                return(result);
            }

            IDbDataAdapter adapter = CreateAdapter();
            DataSet        ds      = GetDataSet(adapter, sql, paramters);

            cache.Put(key, ds, DateTime.Now.AddMinutes(1).Ticks);

            return(ds);
        }
Exemplo n.º 7
0
		private void PutResultInQueryCache(ISessionImplementor session, QueryParameters queryParameters, IType[] resultTypes,
		                                   IQueryCache queryCache, QueryKey key, IList result)
		{
			if ((session.CacheMode & CacheMode.Put) == CacheMode.Put)
			{
				bool put = queryCache.Put(key, resultTypes, result, queryParameters.NaturalKeyLookup, session);
				if (put && factory.Statistics.IsStatisticsEnabled)
				{
					factory.StatisticsImplementor.QueryCachePut(QueryIdentifier, queryCache.RegionName);
				}
			}
		}