终于在博客园开了空间,意味着,再也不用在Sohu Blog里粘代码了
先粘两段爽一下:
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections.Generic;
using System.Web.Caching;
using System.Text;

using IndieCommon;
using IndieORM;
using IndieDB;
using IndieLogic;


/**//// <summary>
/// Vote 获取当前调查
/// 的投票选项统计结果
/// </summary>
[WebService(Namespace = "http://localhost/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Vote : System.Web.Services.WebService


{

public Vote()

{
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}

[WebMethod]
public string GetVoteStatus()

{
// 缓存的键名
string key = "CurrInvest";
// 存储
addInvestToCache(key);

// 从缓存中获取
Invest invest = (Invest)HttpContext.Current.Cache[key];


返回结果#region 返回结果

// 如果当前无调查,则返回空串
if (invest == null)
return string.Empty;

// 计算返回统计
// 格式: 选项名称,被投次数,百分比|选项名称,被投次数,百分比
.
int total = 0;
foreach (InvertObject investObj in invest.InvestList)

{
total += investObj.Total; // 总计票数
}

StringBuilder sb = new StringBuilder();
foreach (InvertObject investObj in invest.InvestList)

{
// 百分比
double per = (Convert.ToDouble(investObj.Total) / Convert.ToDouble(total))
* 100;
sb.Append(string.Format("{0},{1},{2:F1}%|",investObj.Title,investObj.Total
,per));
}

return sb.ToString().TrimEnd(new char[]
{'|'});

#endregion
}


/**//// <summary>
/// 缓存被删除回调方法
/// </summary>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <param name="reason">原因</param>
private void removalCallBack(string key, object value, CacheItemRemovedReason reason)

{
// SQL依赖项被改变
if (reason == CacheItemRemovedReason.DependencyChanged)

{
// 投票项被删除
if (key == "CurrInvest")

{
addInvestToCache("CurrInvest");
}
}
}


/**//// <summary>
/// 在缓存中插入最近一期INVEST对象
/// </summary>
/// <param name="key">键</param>
private void addInvestToCache(string key)

{
// 调查操作对象
InvestOperation io = new InvestOperation();

// 进行数据缓存
if (HttpContext.Current.Cache[key] == null)

{
// 获取当前期调查
Invest currInvest = io.GetLastInvest();

if (currInvest != null)

{
// 数据库和表名
string datatabase = "Indie";
string tablename = "TB_INVEST";
SqlCacheDependency dependency = new SqlCacheDependency(datatabase,tablename);
// 回调
CacheItemRemovedCallback removal = new CacheItemRemovedCallback(removalCallBack);

// SQL SERVER 依赖轮询缓存
HttpContext.Current.Cache.Insert("CurrInvest", currInvest, dependency
,DateTime.MaxValue,Cache.NoSlidingExpiration,CacheItemPriority.Normal,
removal);
}
}
}
}