终于在博客园开了空间,意味着,再也不用在Sohu Blog里粘代码了![](https://www.cnblogs.com/CuteSoft_Client/CuteEditor/images/emsmiled.gif)
先粘两段爽一下:
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;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using IndieCommon;
using IndieORM;
using IndieDB;
using IndieLogic;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// Vote 获取当前调查
/// 的投票选项统计结果
/// </summary>
[WebService(Namespace = "http://localhost/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Vote : System.Web.Services.WebService
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Vote()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[WebMethod]
public string GetVoteStatus()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 缓存的键名
string key = "CurrInvest";
// 存储
addInvestToCache(key);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 从缓存中获取
Invest invest = (Invest)HttpContext.Current.Cache[key];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
返回结果#region 返回结果
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 如果当前无调查,则返回空串
if (invest == null)
return string.Empty;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 计算返回统计
// 格式: 选项名称,被投次数,百分比|选项名称,被投次数,百分比
.
int total = 0;
foreach (InvertObject investObj in invest.InvestList)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
total += investObj.Total; // 总计票数
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
StringBuilder sb = new StringBuilder();
foreach (InvertObject investObj in invest.InvestList)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 百分比
double per = (Convert.ToDouble(investObj.Total) / Convert.ToDouble(total))
* 100;
sb.Append(string.Format("{0},{1},{2:F1}%|",investObj.Title,investObj.Total
,per));
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
return sb.ToString().TrimEnd(new char[]
{'|'});
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 缓存被删除回调方法
/// </summary>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <param name="reason">原因</param>
private void removalCallBack(string key, object value, CacheItemRemovedReason reason)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// SQL依赖项被改变
if (reason == CacheItemRemovedReason.DependencyChanged)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 投票项被删除
if (key == "CurrInvest")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
addInvestToCache("CurrInvest");
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 在缓存中插入最近一期INVEST对象
/// </summary>
/// <param name="key">键</param>
private void addInvestToCache(string key)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 调查操作对象
InvestOperation io = new InvestOperation();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 进行数据缓存
if (HttpContext.Current.Cache[key] == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 获取当前期调查
Invest currInvest = io.GetLastInvest();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (currInvest != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 数据库和表名
string datatabase = "Indie";
string tablename = "TB_INVEST";
SqlCacheDependency dependency = new SqlCacheDependency(datatabase,tablename);
// 回调
CacheItemRemovedCallback removal = new CacheItemRemovedCallback(removalCallBack);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// SQL SERVER 依赖轮询缓存
HttpContext.Current.Cache.Insert("CurrInvest", currInvest, dependency
,DateTime.MaxValue,Cache.NoSlidingExpiration,CacheItemPriority.Normal,
removal);
}
}
}
}