示例#1
0
        /// <summary>
        /// Remove all memoizer instances having the given Func, from the memoizer registry
        /// </summary>
        /// <param name="functionToUnMemoize">The memoized function to remove from the memoizer registry</param>
        /// <param name="memoizerRegistry">The memoizer registry instance</param>
        /// <returns>Number of memoizer instances removed from memoizer registry</returns>
        internal static int RemoveRegistryMemoizersHavingFunction <T>(object functionToUnMemoize, Memoizer <MemoizerConfiguration, T> memoizerRegistry) where T : IDisposable
        //internal static int RemoveRegistryMemoizersHavingFunction<T>(object functionToUnMemoize, Memoizer<MemoizerConfiguration, T> memoizerRegistry) where T : IMemoizer ...would have been nice
        {
            //lock (memoizerRegistry)
            //{
            int numberOfMemoizersRemovedInTotal = 0;

            if (MEMOIZER_REGISTRY_INSTRUMENTATION)
            {
                Interlocked.Increment(ref numberOfTimesInvoked);
            }

            IEnumerable <string> memoizerKeyList = FindMemoizerKeysInRegistryHavingFunction(functionToUnMemoize, memoizerRegistry);

            foreach (var memoizerKey in memoizerKeyList)
            {
                Task <T>    cacheValueTask = (Task <T>)memoizerRegistry.cache.Get(memoizerKey);
                IDisposable memoizer       = cacheValueTask.Result;
                if (memoizerRegistry.cache.Contains(memoizerKey))
                {
                    // TODO: get this data (using reflection, for now)...
                    //if (MEMOIZER_REGISTRY_INSTRUMENTATION)
                    //{
                    //    int numbersOfItems = memoizer.GetCount();
                    //}
                    memoizer.Dispose();
                    memoizerRegistry.cache.Remove(memoizerKey);
                    ++numberOfMemoizersRemovedInTotal;
                    if (MEMOIZER_REGISTRY_INSTRUMENTATION)
                    {
                        // TODO: ...
                        //Console.Write("memoizer [key=" + memoizerKey + "] removed - contaning " + numbersOfItems + " function invocation argument permutation items...");
                        Console.Write("Memoizer.NET.MemoizerRegistry: Memoizer [key=" + memoizerKey + "] removed!");

                        long memoizerRegistryCount = memoizerRegistry.cache.GetCount();
                        switch (memoizerRegistryCount)
                        {
                        case 0:
                            Console.Write(" [Memoizer registry is empty]");
                            break;

                        case 1:
                            Console.Write(" [Memoizer registry still contains " + memoizerRegistry.cache.GetCount() + " memoizer configuration]");
                            break;

                        default:
                            Console.Write(" [Memoizer registry still contains " + memoizerRegistry.cache.GetCount() + " memoizer configurations]");
                            break;
                        }
                    }
                }
                else
                {
                    if (MEMOIZER_REGISTRY_INSTRUMENTATION)
                    {
                        Console.Write("Memoizer.NET.MemoizerRegistry: Memoizer [key=" + memoizerKey + "] does not exist");
                    }
                }
            }
            if (MEMOIZER_REGISTRY_INSTRUMENTATION)
            {
                if (numberOfMemoizersRemovedInTotal != memoizerKeyList.Count())
                {
                    throw new InvalidOperationException("Number of cached functions: " + memoizerKeyList.Count() + ", number of items removed from cache: " + numberOfMemoizersRemovedInTotal);
                }

                Console.WriteLine(" [RemoveRegistryMemoizersHavingFunction() invocation #" + numberOfTimesInvoked + "]");
            }

            return(numberOfMemoizersRemovedInTotal);
            //}
        }
示例#2
0
        /// <summary>
        /// Coupled with the <code>MemoizerConfiguration.GetHashCode()</code> method.
        /// </summary>
        /// <param name="function">The memoized function to look for</param>
        /// <param name="memoizerRegistry">The memoizer registry instance to look into</param>
        /// <returns>An enumeration of keys pointing to memoizer instances in the memoizer registry, having the given function</returns>
        internal static IEnumerable <string> FindMemoizerKeysInRegistryHavingFunction <T>(object function, Memoizer <MemoizerConfiguration, T> memoizerRegistry)
        {
            IList <string> memoizerKeyList = new List <string>();

            foreach (KeyValuePair <string, object> keyValuePair in memoizerRegistry.cache)
            {
                string funcIdPartOfMemoizerConfigurationHashCode = keyValuePair.Key.PadLeft(10, '0').Substring(0, 5);

                bool firstTime = false;
                //long funcId_long = MemoizerHelper.GetObjectId(function, ref firstTime);
                long funcId_long = HashHelper.GetObjectId(function, ref firstTime);
                if (funcId_long > 21474)
                {
                    throw new InvalidOperationException("Memoizer.NET only supports 21474 different Func references at the moment...");
                }
                string funcIdPartOfFunctionToLookFor = funcId_long.ToString().PadLeft(5, '0');

                if (funcIdPartOfFunctionToLookFor == funcIdPartOfMemoizerConfigurationHashCode)
                {
                    memoizerKeyList.Add(keyValuePair.Key);
                }
            }
            return(memoizerKeyList);
        }