1 /* ****************************************************************************
\r
3 * Copyright (c) Microsoft Corporation. All rights reserved.
\r
5 * This software is subject to the Microsoft Public License (Ms-PL).
\r
6 * A copy of the license can be found in the license.htm file included
\r
7 * in this distribution.
\r
9 * You must not remove this notice, or any other, from this software.
\r
11 * ***************************************************************************/
\r
13 namespace System.Web.Mvc {
\r
15 using System.Collections.Generic;
\r
16 using System.Threading;
\r
18 internal sealed class ActionMethodSelectorCache {
\r
20 private ReaderWriterLock _rwLock = new ReaderWriterLock();
\r
21 private Dictionary<Type, ActionMethodSelector> _selectorDictionary = new Dictionary<Type, ActionMethodSelector>();
\r
23 // This method could potentially return multiple selectors for the same controllerType due to
\r
24 // upgrading the lock from reader to writer, but the dictionary won't be corrupted as a result.
\r
25 public ActionMethodSelector GetSelector(Type controllerType) {
\r
26 ActionMethodSelector selector;
\r
28 _rwLock.AcquireReaderLock(Timeout.Infinite);
\r
30 if (_selectorDictionary.TryGetValue(controllerType, out selector)) {
\r
35 _rwLock.ReleaseReaderLock();
\r
38 // if we got this far, the selector was not in the cache
\r
39 selector = new ActionMethodSelector(controllerType);
\r
40 _rwLock.AcquireWriterLock(Timeout.Infinite);
\r
42 _selectorDictionary[controllerType] = selector;
\r
46 _rwLock.ReleaseWriterLock();
\r