lwordserviceproxy.cs

来自「该项目中对 SQLHelper 类进行了简单封装」· CS 代码 · 共 115 行

CS
115
字号
/* 
 * LWordServiceProxy.cs @Microsoft Visual Studio 2008 <.NET Framework 3.5>
 * AfritXia
 * 2008-02-04
 * 
 * Copyright(c) http://www.AfritXia.NET/
 * 
 */

using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Web;
using System.Web.Caching;

using NET.AfritXia.MyHome.HomeManagement.HomeAppServer.LWordServiceRef;
using NET.AfritXia.MyHome.Model;
using NET.AfritXia.MyHome.Model.Message;
using NET.AfritXia.MyHome.ServiceInterface;

namespace NET.AfritXia.MyHome.HomeManagement.CodeLand.ServiceProxy
{
	/// <summary>
	/// 留言服务代理类
	/// </summary>
	public class LWordServiceProxy : ILWordService, IDisposable
	{
		// 缓存键集合
		private static readonly IList<string> g_cacheKeyList = new List<string>();
		// 代理对象
		private LWordServiceClient m_proxyObj = null;

		#region 类构造器
		/// <summary>
		/// 类默认构造器
		/// </summary>
		public LWordServiceProxy()
		{
			this.m_proxyObj = new LWordServiceClient();
			this.m_proxyObj.Open();
		}
		#endregion

		#region ILWordService Members
		public void Append(LWord newLWord)
		{
			this.m_proxyObj.Append(newLWord);

			foreach (string cacheKey in g_cacheKeyList)
				HttpContext.Current.Cache.Remove(cacheKey);

			g_cacheKeyList.Clear();
		}

		public void Delete(int lwordUID)
		{
			this.m_proxyObj.Delete(lwordUID);

			foreach (string cacheKey in g_cacheKeyList)
				HttpContext.Current.Cache.Remove(cacheKey);

			g_cacheKeyList.Clear();
		}

		public IList<LWord> ViewLWordList(int startRecord, int maxRecords, out int allRecordCount)
		{
			allRecordCount = 0;

			string cacheKey = null;

			// 计算缓存键
			cacheKey = "LWordServiceProxy.ViewLWordList(startRecord = {0}, int maxRecords = {1}, -1)";
			cacheKey = String.Format(cacheKey, startRecord, maxRecords);

			if (g_cacheKeyList.Contains(cacheKey) == false)
				g_cacheKeyList.Add(cacheKey);

			// 从缓存中获取留言列表
			IList<LWord> lwordList = HttpContext.Current.Cache[cacheKey] as IList<LWord>;

			if (lwordList != null)
				return lwordList;

			// 调用远程服务获取留言列表
			lwordList = this.m_proxyObj.ViewLWordList(startRecord, maxRecords, out allRecordCount);

			// 缓存留言列表, 20 分钟后过期
			HttpContext.Current.Cache.Insert(cacheKey, lwordList, null,
				DateTime.Now.AddMinutes(20), Cache.NoSlidingExpiration);

			return lwordList;
		}

		public string Actor
		{
			set
			{
				this.m_proxyObj.Actor = value;
			}

			get
			{
				return this.m_proxyObj.Actor;
			}
		}
		#endregion

		#region IDisposable Members
		public void Dispose()
		{
			this.m_proxyObj.Close();
		}
		#endregion
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?