예제 #1
0
        public static ICache GetInstance(string name)
        {
            CacheSetting setting = CacheSection.GetSetting();

            if (name == null || name.Trim().Length <= 0)
            {
                name = setting.DefaultCacheName;
            }
            if (name == null)
            {
                throw new ConfigurationErrorsException("The default cache name is not configured in config file.");
            }
            if (s_CacheProviders.ContainsKey(name))
            {
                return(s_CacheProviders[name]);
            }
            lock (s_SyncObj)
            {
                if (s_CacheProviders.ContainsKey(name))
                {
                    return(s_CacheProviders[name]);
                }
                if (!setting.ContainsKey(name))
                {
                    throw new ConfigurationErrorsException("The cache named '" + name + "' is not be found in config file.");
                }
                CacheItemConfig item = setting[name];
                if (item.Type == null || item.Type.Trim().Length <= 0)
                {
                    throw new ConfigurationErrorsException("The type of cache '" + name + "' cannot be empty.");
                }
                Type p = Type.GetType(item.Type, true);
                if (!typeof(ICache).IsAssignableFrom(p))
                {
                    throw new ConfigurationErrorsException("The type '" + p.AssemblyQualifiedName + "' of cache '" + name + "' dosen't implement the interface '" + typeof(ICache).AssemblyQualifiedName + "'.");
                }
                ICache rst = (ICache)Activator.CreateInstance(p);
                rst.InitFromConfig(name, item.Parameters);
                s_CacheProviders.Add(name, rst);
                return(rst);
            }
        }
예제 #2
0
        public object Create(object parent, object configContext, XmlNode section)
        {
            CacheSetting setting = new CacheSetting();

            if (section != null)
            {
                string tmp = GetNodeAttribute(section, "default");
                setting.DefaultCacheName = (tmp != null && tmp.Length > 0) ? tmp : null;
                XmlNode[] nodeList = GetChildrenNodes(section, "item");
                foreach (XmlNode node in nodeList)
                {
                    string name = GetNodeAttribute(node, "name");
                    if (name == null || name.Length <= 0)
                    {
                        throw new ConfigurationErrorsException("The attribute 'name' of the xml node 'cache/item' cannot be empty in config file.");
                    }
                    if (setting.ContainsKey(name))
                    {
                        throw new ConfigurationErrorsException("Duplicated name '" + name + "' of the xml node 'cache/item' in config file.");
                    }
                    string type = GetNodeAttribute(node, "type");
                    if (type == null || type.Length <= 0)
                    {
                        throw new ConfigurationErrorsException("The attribute 'type' of the xml node 'cache/item' cannot be empty in config file.");
                    }
                    NameValueCollection parms     = new NameValueCollection();
                    XmlNode[]           paramList = GetChildrenNodes(node.SelectSingleNode("parameters"), "add");
                    foreach (XmlNode pa in paramList)
                    {
                        parms.Add(GetNodeAttribute(pa, "key"), GetNodeAttribute(pa, "value"));
                    }
                    setting.Add(name, new CacheItemConfig
                    {
                        Name       = name,
                        Type       = type,
                        Parameters = parms
                    });
                }
            }
            return(setting);
        }