欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

asp.net SAF 中緩存服務(wù)的實(shí)現(xiàn)第4/5頁(yè)

 更新時(shí)間:2008年08月08日 21:28:49   作者:  
對(duì)緩存的興趣源于張子陽(yáng)寫(xiě)的一篇文章《SAF 中緩存服務(wù)的實(shí)現(xiàn)》中的一個(gè)例子:

最后的兩個(gè)方法,GetItem()和GetList()分別用于從緩存中獲取單個(gè)或者多個(gè)對(duì)象。值得注意的是當(dāng)使用GetList()方法時(shí),Xpath應(yīng)該為到達(dá)一個(gè)組結(jié)點(diǎn)的路徑。 
復(fù)制代碼 代碼如下:

// 根據(jù) XPath 獲取對(duì)象    
// 先根據(jù)Xpath獲得對(duì)象的Key,然后再根據(jù)Key獲取實(shí)際對(duì)象    
public virtual object GetItem(string xpath) {    

    object obj = null;    
    xpath = PrepareXPath(xpath);    
    XmlNode node = rootMap.SelectSingleNode(xpath);    

    if (node != null) {    
       // 獲取對(duì)象的Key    
       string key = node.Attributes["key"].Value;    

       // 獲取實(shí)際對(duì)象    
       obj = cacheStrategy.GetItem(key);    
    }    
    return obj;    
}    

// 獲取一組對(duì)象,此時(shí)xpath為一個(gè)組結(jié)點(diǎn)    
public virtual object[] GetList(string xpath) {    
    xpath = PrepareXPath(xpath);    

    XmlNode group = rootMap.SelectSingleNode(xpath);    

    // 獲取該結(jié)點(diǎn)下的所有子結(jié)點(diǎn)(使用[@key]確保子結(jié)點(diǎn)一定包含key屬性)    
    XmlNodeList results = group.SelectNodes(xpath + "/*[@key]");    

    ArrayList objects = new ArrayList();    

    string key;    

    foreach (XmlNode result in results) {    
       key = result.Attributes["key"].Value;    
       Object obj = cacheStrategy.GetItem(key);    
       objects.Add(obj);    
    }    

    return (object[])objects.ToArray(typeof(object));    
}

至此,SAF 的緩存服務(wù)的設(shè)計(jì)和代碼實(shí)現(xiàn)都完成了,現(xiàn)在我們來(lái)看看如何使用它。 

程序測(cè)試 
復(fù)制代碼 代碼如下:

static void Main(string[] args) {    

    CacheService.Cache cache = CacheService.Cache.Instance;    

    // 添加對(duì)象到緩存中    
    cache.AddItem("/WebApplication/Users/Xin", "customer xin");    
    cache.AddItem("/WebApplication/Users/Jimmy", "customer jimmy");    
    cache.AddItem("/WebApplication/Users/Steve", "customer other");    
    cache.AddItem("/WebApplication/GlobalData", "1/1/2008");    
    cache.AddItem("/Version", "v10120080401");    
    cache.AddItem("/Site", "TraceFact.Net");    

    // 獲取所有User    
    object[] objects = cache.GetList("/WebApplication/Users");    
    foreach (object obj in objects) {    
       Console.WriteLine("Customer in cache: {0}", obj.ToString());     
    }    

    // 刪除所有WebApplication下所有子孫結(jié)點(diǎn)    
    cache.RemoveItem("/WebApplication");    

    // 獲取單個(gè)對(duì)象    
    string time = (string)cache.GetItem("/WebApplication/GlobalData");    
    string name = (string)cache.GetItem("/WebApplication/Users/Xin");    

    Console.WriteLine("Time: {0}", time);// 輸出為空,WebApplication下所有結(jié)點(diǎn)已刪除    
    Console.WriteLine("User: {0}", name);// 輸出為空, WebApplication下所有結(jié)點(diǎn)已刪除    

   
    // 獲取根目錄下所有葉結(jié)點(diǎn)    
    objects = cache.GetList("/");    
    foreach (object obj in objects) {    
       Console.WriteLine("Object: {0}", obj.ToString());    
    }    

    Console.ReadLine();    


輸出的結(jié)果為: 
Customer in cache: customer xin 
Customer in cache: customer jimmy 
Customer in cache: customer other 
Time: 
User: 
Object: v10120080401 
Object: Trace

相關(guān)文章

最新評(píng)論