//public virtual void AddObject(string xpath, object o) //{ // lock (lockHelper) // { // cs.AddObject(xpath, o); // } //} public virtual void AddObject(string xpath, object o, int expire) { lock (lockHelper) { cs.AddObject(xpath, o, expire); } }
/// <summary> /// 添加指定ID的对象 /// </summary> /// <param name="objId"></param> /// <param name="o"></param> public override void AddObject(string objId, object o) { if (objId == null || objId.Length == 0 || o == null) { return; } lock (lockObject) { if (Timeout <= 0) { dataCache.Store(StoreMode.Add, GetInputKey(objId), o); } else { dataCache.Store(StoreMode.Add, GetInputKey(objId), o, DateTime.Now.AddSeconds(Timeout)); } //处理本地缓存 if (localCache != null) { localCache.AddObject(objId, o, localTimeSpan); } } }
/// <summary> /// 在XML映射文档中的指定路径,加入当前对象信息 /// </summary> /// <param name="xpath">分级对象的路径 </param> /// <param name="o">被缓存的对象</param> public virtual void AddObject(string xpath, object o) { 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); //CachesFileMonitor.UpdateCacheItem(newXpath);//更新xml //建立对象的唯一键值, 用以映射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.AddObject(objectId, o); } }
/// <summary> /// 添加缓存 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="cacheKey"></param> /// <param name="cacheValue"></param> /// <param name="cacheTime"></param> public void AddCache <T>(string cacheKey, T cacheValue, int cacheTime) { if (cacheTime > 0) { //组合CacheKey cacheKey = string.Format("{0}_{1}_{2}", connectName, typeof(T).FullName, cacheKey); strategy.AddObject(cacheKey, cacheValue, TimeSpan.FromSeconds(cacheTime)); } }
/// <summary> /// 存储key及value /// </summary> /// <param name="cacheKey"></param> /// <param name="cacheValue"></param> /// <param name="timeSpan"></param> public void Add(string cacheKey, object cacheValue, TimeSpan timeSpan) { //存入缓存 cache.AddObject(cacheKey, cacheValue, timeSpan); //记录过期时间 var timeSpanKey = string.Format("SessionCache_{0}", cacheKey); CacheHelper.Permanent(timeSpanKey, timeSpan); }
/// <summary> /// 插入缓存数据 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="seconds"></param> internal static void InsertCache(this ICacheStrategy cache, string key, object value, int seconds) { if (cache == null) { CacheHelper.Insert(key, value, seconds); } else { cache.AddObject(key, value, TimeSpan.FromSeconds(seconds)); } }
/// <summary> /// 在XML映射文档中的指定路径,加入当前对象信息 /// </summary> /// <param name="xpath">分级对象的路径 </param> /// <param name="o">被缓存的对象</param> public virtual void AddObject(string xpath, object o) { lock (LockHelper) { { //当缓存到期时间为0或负值,则不再放入缓存 if (_cs.TimeOut <= 0) { return; } //整理XPATH表达式信息 var newXpath = PrepareXpath(xpath); var separator = newXpath.LastIndexOf("/", StringComparison.Ordinal); //找到相关的组名 var group = newXpath.Substring(0, separator); //找到相关的对象 var element = newXpath.Substring(separator + 1); var groupNode = _objectXmlMap.SelectSingleNode(@group); //建立对象的唯一键值, 用以映射XML和缓存对象的键 var objectId = ""; var node = _objectXmlMap.SelectSingleNode(PrepareXpath(xpath)); if (node != null) { if (node.Attributes != null) { objectId = node.Attributes["objectId"].Value; } } if (objectId == "") { groupNode = CreateNode(@group); objectId = Guid.NewGuid().ToString(); //建立新元素和一个属性 for this perticular object if (_objectXmlMap.OwnerDocument != null) { var objectElement = _objectXmlMap.OwnerDocument.CreateElement(element); var objectAttribute = _objectXmlMap.OwnerDocument.CreateAttribute("objectId"); objectAttribute.Value = objectId; objectElement.Attributes.Append(objectAttribute); //为XML文档建立新元素 groupNode.AppendChild(objectElement); } } else { //建立新元素和一个属性 for this perticular object if (_objectXmlMap.OwnerDocument != null) { var objectElement = _objectXmlMap.OwnerDocument.CreateElement(element); var objectAttribute = _objectXmlMap.OwnerDocument.CreateAttribute("objectId"); objectAttribute.Value = objectId; objectElement.Attributes.Append(objectAttribute); //为XML文档建立新元素 if (groupNode != null) { if (node != null) { groupNode.ReplaceChild(objectElement, node); } } } } //向缓存加入新的对象 _cs.AddObject(objectId, o); } } }
/// <summary> /// 在XML映射文档中的指定路径,加入当前对象信息 /// </summary> /// <param name="xpath">分级对象的路径 </param> /// <param name="o">被缓存的对象</param> public virtual void AddObject(string xpath, object o) { lock (lockHelper) { //if (applyMemCached || applyRedis) { //向缓存加入新的对象 cs.AddObject(xpath, o); } //else //{ // //当缓存到期时间为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.AddObject(objectId, o); //} } }
/// <summary> /// 添加缓存对象 /// </summary> /// <param name="key">缓存Key</param> /// <param name="o">缓存对象</param> /// <param name="sec">缓存时间(单位:秒)</param> public void AddObject(string key, object o, int sec) { cachedStrategy.TimeOut = sec; cachedStrategy.AddObject(key, o); }