/// <summary> /// Thread-safe method to obtain singleton instance of <see cref="ICacheProvider"/> /// matching specified filter. /// </summary> /// <param name="filter"></param> /// <returns></returns> private static ICacheProvider GetProvider(Predicate <ExtensionInfo> filter) { // determine the provider class var point = new CacheProviderExtensionPoint(); var extension = CollectionUtils.FirstElement(point.ListExtensions(filter)); if (extension == null) { throw new CacheException("No cache provider extension found, or those that exist do not support all required features."); } var providerClass = extension.ExtensionClass.Resolve(); // check if we already have an initialized instance of this provider class. ICacheProvider provider; if (!_providers.TryGetValue(providerClass, out provider)) { // if not, create one lock (_providers) { // ensure that another thread hasn't beat us to it if (!_providers.TryGetValue(providerClass, out provider)) { provider = (ICacheProvider)point.CreateExtension( new ClassNameExtensionFilter(providerClass.FullName)); // initialize this provider and store it provider.Initialize(new CacheProviderInitializationArgs()); _providers.Add(providerClass, provider); } } } return(provider); }
/// <summary> /// Thread-safe method to obtain singleton instance of <see cref="ICacheProvider"/> /// matching specified filter. /// </summary> /// <param name="filter"></param> /// <returns></returns> private static ICacheProvider GetProvider(Predicate <ExtensionInfo> filter) { // determine the provider class var point = new CacheProviderExtensionPoint(); var extension = CollectionUtils.FirstElement(point.ListExtensions(filter)); if (extension == null) { throw new CacheException("No cache provider extension found, or those that exist do not support all required features."); } var providerClass = extension.ExtensionClass.Resolve(); return(_providers.GetOrAdd(providerClass, k => { // initialize this provider and store it var provider = (ICacheProvider)point.CreateExtension(new ClassNameExtensionFilter(providerClass.FullName)); provider.Initialize(new CacheProviderInitializationArgs()); return provider; })); }