williambirkin

恭喜发财!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
资源文件的使用比较简单。
资源文件主要是包括:“支持的语言”(languages.xml),“系统消息”(Messages.xml)和“文字资源”(Resources.xml)。
通过使用ResourceManager的静态方法获得资源数据。
当数据已经加载到缓存中时读取缓存中的数据。如果没有加载到缓存中则先加载到缓存,然后读取缓存数据。
cache会自动在设置的有效时间内监视资源文件。

Public class to load all language XML files into cache for quick access.
public class ResourceManager
获得支持的语言文件,并加载到缓存中。
        
public static NameValueCollection GetSupportedLanguages()
        
{
            ……

            
if (forumContext.Context.Cache[cacheKey] == null)
            
{
                ……
在存储于 ASP.NET 应用程序的 Cache 对象中的项与文件、缓存键、文件或缓存键的数组或另一个 CacheDependency 对象之间建立依附性关系。
                CacheDependency dp 
= new CacheDependency(filePath);
                supportedLanguages 
= new NameValueCollection();
加载xml资源文件
                XmlDocument d 
= new XmlDocument();
                d.Load(filePath);

                
foreach (XmlNode n in d.SelectSingleNode("root").ChildNodes)
                
{
                    
if (n.NodeType != XmlNodeType.Comment)
                    
{
                        supportedLanguages.Add(n.Attributes[
"name"].Value, n.Attributes["key"].Value);
                    }

                }

向 Cache 对象插入项。向 Cache 中插入具有依赖项和过期策略的对象。
                forumContext.Context.Cache.Insert(cacheKey, supportedLanguages, dp, DateTime.MaxValue, TimeSpan.Zero);
            }


            
return (NameValueCollection)forumContext.Context.Cache[cacheKey];
        }


获得键值
public static string GetString(string name)
        
{

            Hashtable resources 
= GetResource(ResourceManagerType.String);

            ……

            
return (string)resources[name];
        }


获得系统消息内容。
public static ForumMessage GetMessage(ForumExceptionType exceptionType)
        
{
加载消息资源文件到缓存中
            Hashtable resources 
= GetResource(ResourceManagerType.ErrorMessage);

            ……

            
return (ForumMessage)resources[(int)exceptionType];
        }


加载资源文件到缓存中,并返回键值。
private static Hashtable GetResource(ResourceManagerType resourceType)
        
{
            ForumContext forumContext 
= ForumContext.Current;
            Hashtable resources;
……
            
// Attempt to get the resources from the Cache
            
//
            if (forumContext.Context.Cache[cacheKey] == null)
            
{
                resources 
= new Hashtable();
                LoadResource(resourceType, resources, 
"zh-CN", cacheKey);

               ……
            }


            
return (Hashtable)forumContext.Context.Cache[cacheKey];
        }


加载资源文件
private static void LoadResource(ResourceManagerType resourceType, Hashtable target, string language, string cacheKey)
        
{
            ………

在存储于 ASP.NET 应用程序的 Cache 对象中的项与文件、缓存键、文件或缓存键的数组或另一个 CacheDependency 对象之间建立依附性关系。
            CacheDependency dp 
= new CacheDependency(filePath);
加载xml资源文件到hashtable中
            ……

向 Cache 对象插入项。向 Cache 中插入具有依赖项和过期策略的对象。
            forumContext.Context.Cache.Insert(cacheKey, target, dp, cacheUntil, TimeSpan.Zero);

        }


 
posted on 2006-12-14 17:34  williambirkin  阅读(309)  评论(0编辑  收藏  举报