IEnumerable <string> ReadAppState() { var appstatePath = HostingEnvironment.MapPath(AppStateData); List <string> list = _cacheStrategy.RetrieveObject <List <string> >(appstatePath); if (list == null) { list = new List <string>(); if (File.Exists(appstatePath)) { var content = File.ReadAllText(appstatePath, Encoding.UTF8); if (!string.IsNullOrEmpty(content)) { list.AddRange(content.Split(',')); } } else { WriteAppState(null); } _cacheStrategy.AddObjectWithFileChange(appstatePath, list, onRemove, appstatePath); } return(list); }
public void AddObject(string objectId, object o, string file) { lock (lockHelper) { cachedStrategy.AddObjectWithFileChange(objectId, o, file); } }
/// <summary> /// 在XML映射文档中的指定路径,加入当前对象信息 /// </summary> /// <param name="xpath">分级对象的路径 </param> /// <param name="o">被缓存的对象</param> public virtual void AddObject(string xpath, object o, string[] files) { xpath = xpath.Replace(" ", "_SPACE_"); //如果xpath中出现空格,则将空格替换为_SPACE_ lock (lockHelper) { //当缓存到期时间为0或负值,则不再放入缓存 if (cs.TimeOut <= 0) { return; } //整理XPATH表达式信息 string newXpath = PrepareXpath(xpath); int separator = newXpath.LastIndexOf("/"); //找到相关的组名 string group = newXpath.Substring(0, separator); //找到相关的对象 string element = newXpath.Substring(separator + 1); XmlNode groupNode = objectXmlMap.SelectSingleNode(group); //建立对象的唯一键值, 用以映射XML和缓存对象的键 string objectId = ""; XmlNode node = objectXmlMap.SelectSingleNode(PrepareXpath(xpath)); if (node != null) { objectId = node.Attributes["objectId"].Value; } if (objectId == "") { groupNode = CreateNode(group); objectId = Guid.NewGuid().ToString(); //建立新元素和一个属性 for this perticular object XmlElement objectElement = objectXmlMap.OwnerDocument.CreateElement(element); XmlAttribute objectAttribute = objectXmlMap.OwnerDocument.CreateAttribute("objectId"); objectAttribute.Value = objectId; objectElement.Attributes.Append(objectAttribute); //为XML文档建立新元素 groupNode.AppendChild(objectElement); } else { //建立新元素和一个属性 for this perticular object XmlElement objectElement = objectXmlMap.OwnerDocument.CreateElement(element); XmlAttribute objectAttribute = objectXmlMap.OwnerDocument.CreateAttribute("objectId"); objectAttribute.Value = objectId; objectElement.Attributes.Append(objectAttribute); //为XML文档建立新元素 groupNode.ReplaceChild(objectElement, node); } //向缓存加入新的对象 cs.AddObjectWithFileChange(objectId, o, files); } }