Developing Application Frameworks in .Net --- (Note 3 Cach)

1. Caching Service

static variable and HashTable

XML
    ---- Element  .InnerText
    ---- attribute .attribute[key].Value

XmlDocument , XmlElement, XmlNode

XPath

/RootElement/ChildElement/Element[.='something']  [@attribute = 'somevalue'] [ChildElement = 'somevalue'] [postion() = 1] [... and/or ...] 

//Element  //ChildElement/element /Element//ChildElement

----- Configuration file:
< configuration >
    
< configSections >
      
< section name = " Framework "  type = " SAF.Configuration.ConfigurationHandler,SAF.Configuration "   />
       
< section name = " Application "  type = " SAF.Configuration.ConfigurationHandler,SAF.Configuration "   />
    
</ configSections >
    
    
< Framework type = " SAF.Configuration.ConfigurationManager,SAF.Configuration " >
        
< SAF.Cache >
            
< CacheStrategy type  =   " SAF.Cache.DefaultCacheStrategy,SAF.Cache " />
        
</ SAF.Cache >
    
</ Framework >
</ configuration >

----- Configuration Class
CacheConfig  =   new   CacheConfigration(cam.GetData( " SAF.Cache " ));
....

public   class  CacheConfigration
    
{
        
private XmlNode cacheXml;
        
public CacheConfigration(XmlNode configData){
            cacheXml 
= configData;
        }


        
public object GetCacheStrategy(){
            
string typeName = cacheXml.SelectSingleNode("CacheStrategy").Attributes["type"].Value;
            Type type 
= Type.GetType(typeName);
            
return Activator.CreateInstance(type,null);
        }

    }

----- Cach class (singleton and stratage design pattern)
public   class  Cache
    
{
        
private  XmlElement objectXmlMap ;

       
// Stragtegy class
        private static SAF.Cache.ICacheStrategy cs;
        
private static Cache cache;
        
private XmlDocument rootXml = new XmlDocument();


        
///
protected  construtor, required for singleton design pattern.
        protected Cache()
        
{
            
//retrieve setting from configuration file
            ConfigurationManager cm = (ConfigurationManager)ConfigurationSettings.GetConfig("Framework");
            
//load the cache strategy object
            cs = (ICacheStrategy)cm.CacheConfig.GetCacheStrategy();
            
//create an Xml used as a map between  xml expression and object cached in the
            
//physical storage.
            objectXmlMap = rootXml.CreateElement("Cache");
            
//build the internal xml document.
            rootXml.AppendChild(objectXmlMap);    
        }


        
/// Singlton method used to return the instance of Cache class
        public static Cache GetSAFCacheService()
        
{
            
if (cache == null)
            
{
                cache 
= new Cache();
            }

            
return cache;
        }


        
/// Add the object to the underlying storage and Xml mapping document
        public virtual void AddObject(string xpath, object o)
        
{
            
//determin if group is already exist?, if not, create one.
            if (groupNode == null)
            
{
                
lock(this)
                
{
                    
//build the xml tree
                    groupNode = CreateNode(group);
                }

            }

            
//get a unique key to identity of object, it is used to map
            
//between xml and object key used in the cache strategy
            string objectId = System.Guid.NewGuid().ToString();
            
//create an new element and new attribute for this perticular object
            XmlElement objectElement = objectXmlMap.OwnerDocument.CreateElement(element);
            XmlAttribute objectAttribute 
=objectXmlMap.OwnerDocument.CreateAttribute("objectId");
            objectAttribute.Value 
= objectId;
            objectElement.Attributes.Append(objectAttribute);
            
//Add the object element to the Xml document
            groupNode.AppendChild(objectElement);

            
//add the object to the underlying storage through cache strategy
            cs.AddObject(objectId,o);        
        }


        
/// Retrieve the cached object using its hierarchical location
        public virtual object RetrieveObject(string xpath)
        
{
            
//....
            if ( node != null)
            
{
                
string objectId = node.Attributes["objectId"].Value;
                
//retrieve the object through cache strategy
                o = cs.RetrieveObject(objectId);
            }

            
return o;
            
        }


        
/// Remove the object from the storage and clear the Xml assocated withthe object
        public virtual void RemoveObject(string xpath)
        
{
            XmlNode result 
= objectXmlMap.SelectSingleNode(PrepareXpath(xpath));
            
//check if the xpath refers to a group(container) or
            
//actual element for cached object
            if (result.HasChildNodes)
            
{
                
//remove all the cached object in the hastable
                
//and remove all the child nodes 
                XmlNodeList objects = result.SelectNodes("*[@objectId]");
                
string objectId ="";
                
foreach (XmlNode node in objects)
                
{
                    objectId 
= node.Attributes["objectId"].Value;
                    node.ParentNode.RemoveChild(node);
                    
//use cache strategy to remove the objects from the 
                    
//underlying storage
                    cs.RemoveObject(objectId);
                }

            }

            
else
            
{
                
//just remove the element node and the object associate with it
                string objectId = result.Attributes["objectId"].Value;
                result.ParentNode.RemoveChild(result);
                cs.RemoveObject(objectId);        
            }

        }



    
/// the interface for cache strategy.
    
/// each class that is pluggable to the SAF.Cache must 
    
/// implement this interface.

    public interface ICacheStrategy
    
{
        
void AddObject(string objId, object o);
        
void RemoveObject(string objId);
        
object RetrieveObject(string objId);
    }


----- Default strategy
public   class  DefaultCacheStrategy : ICacheStrategy
    
{
        
private Hashtable objectTable;
        
        
public DefaultCacheStrategy()
        
{
            objectTable 
= new Hashtable();
        }


        
/// Add an object to the underlying storage
        public void AddObject(string objId, object o)
        
{
            objectTable.Add(objId,o);
        }


        
/// Remove an object from the underlying storage
        public void RemoveObject(string objId)
        
{
            objectTable.Remove(objId);
        }


        
/// Retrieve an object from the underlying storage
        public object RetrieveObject(string objId)
        
{

            
return objectTable[objId];
        }

    }




 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值