1.在配置文件添加:
<appSettings>
<addkey="Svr1"value="192.168.22.236:11211" />
<addkey="Svr2"value="192.168.22.240:11211" />
</appSettings>
2.添加引用(引用dll)
下面是所有的:
ITOO.Library.core确保版本正确
若用wcf,wcf也要添加引用
代码示例:
#regionQueryPaperIDByCourseTypeIDMemcache()根据课程性质ID查询试卷ID-赵寒-2016-3-16
/// <summary>
/// 根据课程性质ID查询试卷ID
/// </summary>
/// <paramname="coursetypeid">课程性质id</param>
/// <returns>试卷id</returns>
public EvalCourseTypeViewModelQueryPaperIDByCourseTypeIDMemcache(string coursetypeid)
{
//定义返回的实体
EvalCourseTypeViewModel paper = newEvalCourseTypeViewModel();
//查询服务器1是否存在表
bool flag3 =MemcacheHelper.IsServersExists("Svr1","evaluationcoursetypeentity");
if (flag3==true)
{
//若1表存在
//查询信息是否存在
bool flag1 =MemcacheHelper.IsServersExists("Svr1",coursetypeid +"pj");
if (flag1 == true)
{
//存在,则取出
paper =(EvalCourseTypeViewModel)MemcacheHelper.GetCounterFrom("Svr1",coursetypeid + "pj");
return paper;
}
//不存在直接从数据库返回该条数据
else
{
List<EvalCourseTypeViewModel> paperlist = newList<EvalCourseTypeViewModel>();
paperlist =QueryPaperIDByCousePropertyID(coursetypeid);
if (paperlist.Count() >0)
{
for (int i = 0; i <paperlist.Count(); i++)
{
MemcacheHelper.SetTo("Svr1", paperlist[i].CourseTypeId +"pj", paperlist[i]);
paper.PaperId =paperlist[0].PaperId;
paper.CourseTypeId= paperlist[0].CourseTypeId;
paper.CourseTypeName = paperlist[0].CourseTypeName;
}
}
return paper;
}
}
else
{
//看2服务器是否存在该表
bool flag4 =MemcacheHelper.IsServersExists("Svr2","evaluationcoursetypeentity");
if (flag4==true)
{
//若2上表存在
//查询信息是否存在
bool flag5 =MemcacheHelper.IsServersExists("Svr2",coursetypeid +"pj");
if (flag5 == true)
{
//存在,则取出
paper=(EvalCourseTypeViewModel)MemcacheHelper.GetCounterFrom("Svr1",coursetypeid + "pj");
return paper;
}
else
{
//2表存在,信息不存在,则插入2表信息
//paper =QueryPaperIDByCousePropertyID(coursetypeid);
//表中无该条数据,从数据库获取返回
List<EvalCourseTypeViewModel> paperlist = newList<EvalCourseTypeViewModel>();
paperlist =QueryPaperIDByCousePropertyID(coursetypeid);
if (paperlist.Count()> 0)
{
for (int i = 0; i< paperlist.Count(); i++)
{
MemcacheHelper.SetTo("Svr2", paperlist[i].CourseTypeId +"pj", paperlist[i]);
paper.PaperId =paperlist[0].PaperId;
paper.CourseTypeId = paperlist[0].CourseTypeId;
paper.CourseTypeName = paperlist[0].CourseTypeName;
}
}
return paper;
}
}
else
{
//表不存在,插入表,插入数据
List<EvalCourseTypeViewModel> coursepaper = newList<EvalCourseTypeViewModel>();
coursepaper =QueryPaperID();
MemcacheHelper.SetTo("Svr1","evaluationcoursetypeentity",coursepaper);
MemcacheHelper.SetTo("Svr2","evaluationcoursetypeentity", coursepaper);
if (coursepaper.Count()> 0)
{
for (int i = 0; i <coursepaper.Count(); i++)
{
MemcacheHelper.SetTo("Svr1", coursepaper[i].CourseTypeId +"pj", coursepaper[i]);
MemcacheHelper.SetTo("Svr2", coursepaper[i].CourseTypeId +"pj", coursepaper[i]);
}
}
paper =(EvalCourseTypeViewModel)MemcacheHelper.GetCounterFrom("Svr1",coursetypeid + "pj");
return paper;
}
}
}
#endregion
总结:底层封装的方法可能会变,但用的思路不变。