前言:我是借鉴别人的“redis缓存分页”的思路进行实现的,下面是代码实现部分(在我的博客中有一篇“转载”就是写的思路实现部分的,大家可以借鉴一下)
///
/// 得到分页数据
/// 说明:使用了redis缓存分页
///
///
///
public Page.pa_ForumMain getPageData(int pageNo)
{
string userNum = "2091928133";
//string userNum = HttpContext.Current.Session["UNumber"].ToString();
Page.pa_ForumMain forumMain = new Page.pa_ForumMain();
forumMain.pageNo = pageNo;//当前页
forumMain.pageSize = (int)nf_ForumMain.PageNum;//每页显示条数
forumMain.pageCountByParameter = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(new DAL.DAL_nf_ForumMain().getPageCount(userNum, forumMain.pageSize, forumMain.pageNo) + .0000)));//获得总页数【有一个三元表达式:防止用户没有博文抛异常】
forumMain.pageCount = new BLL_nf_ForumMain().getDataCount(userNum);
if (forumMain.pageCountByParameter < 0)//判断用户请求页数是否有数据
return new Page.pa_ForumMain() { pageCountByParameter = 0 };
using (var client = RedisManager.GetClient())//判断redis中的ZSet集合里面是否存在数据
{
if (client.GetSortedSetCount(userNum + "userBlogsZSet") == 0)//如果“ZSet集合”里面没有数据,但是数据库里面有
new VO.vo_redis_nf_ForumMain().getInitUserBlogsIdByRedisToZSet(userNum, forumMain);
};//using end
//如果“ZSet集合”存在数据
using (var client = RedisManager.GetClient())
{
//如果redis的ZSet里面存在数据,则查看Hash集合里面时候存在“指定区间”的数据。【存在就返回,不存在就先去数据库里面找。两个判断都要判断过期时间】
if (client.GetSortedSetCount(userNum + "userBlogsZSet") != 0)//如果“ZSet集合里面存在数据”,就获取指定区间内的
{
List<string> strlins = new VO.vo_redis_nf_ForumMain().getRedisBlogsIdByZSet(userNum, forumMain);//将“ZSet集合指定区间内的数据暂时存储到泛型集合里面”
if (strlins.Count > 0)//如果“ZSet集合”的数据存在,“Hash集合里面又没有数据”,就不进行“Hash过期时间的判断”
{
foreach (string item in strlins)
{
if (client.GetHashCount(item) == 0)//如果“ZSet集合”的数据存在,并且指定“Hash集合里面不存在数据”,就增加指定区间内的数据
{
if (new VO.vo_redis_nf_ForumMain().getInitUserBlogsIdByRedisToHash(userNum, strlins) == true)
return new Page.pa_ForumMain() {pageNo= forumMain.pageNo, pageSize= forumMain.pageSize, pageCount=forumMain.pageCount, pageCountByParameter = forumMain.pageCountByParameter, forumMains= new VO.vo_redis_nf_ForumMain().getgetInitUserBlogsIdByRedisToHashValues(userNum,strlins) };
}
if (client.GetHashCount(item) != 0)//如果“ZSet集合存在数据”,指定“Hash集合”里面也存在数据,就进行“Hash过期时间”的判断
{
DateTime time = Convert.ToDateTime(client.GetItemFromList(userNum + "userBlogsDatasTimeByRedisHash", 0));
if (time == DateTime.Now.AddDays(30))//如果现在时间和过期时间一样,就删除“Hash集合”过期时间
{
client.Remove(item);//删除指定的“Hash集合”群【这里的】
}//if end
if (time != DateTime.Now.AddDays(30))//如果现在时间和过期时间不一样(Hash集合里面存在数据),将Hash集合指定区间内的数据取出来
{
if (new VO.vo_redis_nf_ForumMain().getInitUserBlogsIdByRedisToHash(userNum, strlins) == true)
return new Page.pa_ForumMain() { pageNo = forumMain.pageNo, pageSize = forumMain.pageSize, pageCount = forumMain.pageCount, pageCountByParameter = forumMain.pageCountByParameter, forumMains = new VO.vo_redis_nf_ForumMain().getgetInitUserBlogsIdByRedisToHashValues(userNum, strlins) };
}
}//if end
}//foreach end
}//if end
}//if end
};//using end
//如果出错,就直接返回“-1”
return new Page.pa_ForumMain() { pageNo=-1};
}//function end
///
/// 获得数据库中,所有的用户博文Id并存储进“ZSet集合”
///
/// 布尔类型
public bool getInitUserBlogsIdByRedisToZSet(string userNum, Page.pa_ForumMain forumMain=null)
{
DAL.DAL_nf_ForumMain forumMai = new DAL.DAL_nf_ForumMain();
try
{
//从数据库中查找数据
List<Model.nf_ForumMain> dataList = forumMai.getUserBlogsByTime(userNum);
using (var client = RedisManager.GetClient())
{
long Bignumber = 0;
foreach (Model.nf_ForumMain item in dataList)
{
Bignumber += 1;
client.AddItemToSortedSet(userNum + "userBlogsZSet", item.FMId, Bignumber);
}//foreach end
client.AddItemToList(userNum + "userBlogsZSetFirstCount", client.GetSortedSetCount(userNum + "userBlogsZSet").ToString());//存储第一次用户博文的总数
client.AddItemToList(userNum + "userBlogsZSetSecondCount", client.GetSortedSetCount(userNum + "userBlogsZSet").ToString());//除第一次外,用户的博文总数
}//using end
return true;
}
catch (Exception ex)
{
return false;
}
}//function end
///
/// 通过“第一个参数”和“第二个参数”获得ZSet集合区间内的博文Id,并进行返回
/// pageNo、pageSize
///
///
public List<string> getRedisBlogsIdByZSet(string userNum, Page.pa_ForumMain forumMain)
{
List<string> strlins = new List<string>();
using (var client = RedisManager.GetClient())
{
IDictionary<string, double> pairs = client.GetRangeWithScoresFromSortedSet(userNum + "userBlogsZSet", (forumMain.pageSize * (forumMain.pageNo - 1) + 1), (forumMain.pageSize * forumMain.pageNo));//得到ZSet区间内的
for (int i = 0; i < pairs.Count; i++)
strlins.Add(pairs.ElementAt(i).Key);
}//using end
return strlins;
}//function end
/// y
/// 将博文具体数据存储进Hash集合里面
///
///
///
///
///
public bool getInitUserBlogsIdByRedisToHash(string userNum, List<string> strlins)
{
DAL.DAL_nf_ForumMain forumMain = new DAL_nf_ForumMain();
try
{
using (var client = RedisManager.GetClient())
{
foreach (string item in strlins)
{
Model.nf_ForumMain main = forumMain.getObject(item, userNum);
//17个对象字段为一个hash集群,hashId为该对象的Id
client.SetEntryInHash(main.FMId, "FMId", main.FMId);//帖子编号
client.SetEntryInHash(main.FMId, "FMTitle", main.FMTitle);//帖子标题
client.SetEntryInHash(main.FMId, "FMArea", main.FMArea.ToString());//区域编号
client.SetEntryInHash(main.FMId, "FMClassify", main.FMClassify.ToString());//分类编号
client.SetEntryInHash(main.FMId, "LId", main.LId);//标签编号
client.SetEntryInHash(main.FMId, "messager", main.messager);//发帖人编号
client.SetEntryInHash(main.FMId, "FMTime", main.FMTime.ToString());//发帖时间
client.SetEntryInHash(main.FMId, "FMContent", main.FMContent);//帖子内容
client.SetEntryInHash(main.FMId, "FBodyContent", main.FBodyContent);//纯文本内容
client.SetEntryInHash(main.FMId, "FImagesUrl", main.FImagesUrl);//图片路径
client.SetEntryInHash(main.FMId, "FMNumber", main.FMNumber.ToString());//点赞数量
client.SetEntryInHash(main.FMId, "FMViewCount", main.FMViewCount.ToString());//浏览次数
//求价值:
int number = new DAL.DAL_nf_ForumSecond().getCommentCount(main.FMId).Count();
string price = Math.Ceiling(Convert.ToDecimal(((long.Parse(client.GetValueFromHash(main.FMId, "FMNumber")) * 2) +//点赞数量
long.Parse(client.GetValueFromHash(main.FMId, "FMViewCount")) +//浏览数量
(number * 3)) / 3.0)).ToString();//评论数量[从后台拿到]
client.SetEntryInHash(main.FMId, "jiazhi", price);//【价值数量=浏览分数+点赞分数+评论分数】
client.SetEntryInHash(main.FMId, "Commentary", number.ToString());//评论数量
client.SetEntryInHash(main.FMId, "FMRecommend", main.FMRecommend.ToString());//是否推荐
client.SetEntryInHash(main.FMId, "FMExamine", main.FMExamine.ToString());//是否审查
client.SetEntryInHash(main.FMId, "IsDelete", main.IsDelete.ToString());//是否删除
}//foreach end
client.AddItemToList(userNum + "userBlogsDatasTimeByRedisHash", DateTime.Now.AddDays(30).ToString());//获得Hash集合过期时间
client.AddItemToList(userNum + "userBlogsImportDataTimeByRedisHash", DateTime.Now.AddDays(5).ToString());//获得“点赞、评论、浏览”的过期时间
}//using end
return true;
}
catch (Exception ex)
{
return false;
}//catch end
}//function end
///
/// 在“Hash集合”中得到指定区间的数据
///
/// 获得某ZSet集合区间内的用户博文Id
///
public List<Model.nf_ForumMain> getgetInitUserBlogsIdByRedisToHashValues(string userNum, List<string> strlins)
{
List<Model.nf_ForumMain> forumMains = new List<Model.nf_ForumMain>();
try
{
using (var client = RedisManager.GetClient())
{
foreach (string item in strlins)
{
//如果“ZSet有值,Hash里面没有值。就通过博文Id到数据库里面寻找”
if (!client.HashContainsEntry(item, "FMArea") && !client.HashContainsEntry(item, "FMClassify") && !client.HashContainsEntry(item, "FMNumber") && !client.HashContainsEntry(item, "FMViewCount") && !client.HashContainsEntry(item, "jiazhi") && !client.HashContainsEntry(item, "Commentary") && !client.HashContainsEntry(item, "FMRecommend") && !client.HashContainsEntry(item, "FMExamine") && !client.HashContainsEntry(item, "IsDelete"))
{
Model.nf_ForumMain main = new DAL.DAL_nf_ForumMain().getObject(item, userNum);
client.SetEntryInHash(main.FMId, "FMId", main.FMId);//帖子编号
client.SetEntryInHash(main.FMId, "FMTitle", main.FMTitle);//帖子标题
client.SetEntryInHash(main.FMId, "FMArea", main.FMArea.ToString());//区域编号
client.SetEntryInHash(main.FMId, "FMClassify", main.FMClassify.ToString());//分类编号
client.SetEntryInHash(main.FMId, "LId", main.LId);//标签编号
client.SetEntryInHash(main.FMId, "messager", main.messager);//发帖人编号
client.SetEntryInHash(main.FMId, "FMTime", main.FMTime.ToString());//发帖时间
client.SetEntryInHash(main.FMId, "FMContent", main.FMContent);//帖子内容
client.SetEntryInHash(main.FMId, "FBodyContent", main.FBodyContent);//纯文本内容
client.SetEntryInHash(main.FMId, "FImagesUrl", main.FImagesUrl);//图片路径
client.SetEntryInHash(main.FMId, "FMNumber", main.FMNumber.ToString());//点赞数量
client.SetEntryInHash(main.FMId, "FMViewCount", main.FMViewCount.ToString());//浏览次数
client.SetEntryInHash(main.FMId, "jiazhi", Convert.ToString(0));//价值
int number = new DAL.DAL_nf_ForumSecond().getCommentCount(main.FMId).Count();
client.SetEntryInHash(main.FMId, "Commentary", number.ToString());//评论次数
client.SetEntryInHash(main.FMId, "BlogsFirstPageByPlusSign", Convert.ToString(0));//加号
client.SetEntryInHash(main.FMId, "BlogsFirstPageByMinusSign", Convert.ToString(0));//减号
client.SetEntryInHash(main.FMId, "FMRecommend", main.FMRecommend.ToString());//是否推荐
client.SetEntryInHash(main.FMId, "FMExamine", main.FMExamine.ToString());//是否审查
client.SetEntryInHash(main.FMId, "IsDelete", main.IsDelete.ToString());//是否删除
//重新为“价值赋值”
string price = Math.Ceiling(Convert.ToDecimal(((long.Parse(client.GetValueFromHash(main.FMId, "FMNumber")) * 2) +//点赞数量
long.Parse(client.GetValueFromHash(main.FMId, "FMViewCount")) +//浏览数量
(number * 3)) / 3.0)).ToString();//评论数量[从后台拿到]
client.SetEntryInHash(main.FMId, "jiazhi", price);//【价值数量=浏览分数+点赞分数+评论分数】
}//if end
forumMains.Add(new Model.nf_ForumMain()
{
FMId = client.GetValueFromHash(item, "FMId") as string,//帖子编号
FMTitle = client.GetValueFromHash(item, "FMTitle") as string,//帖子标题
FMArea = int.Parse(client.GetValueFromHash(item, "FMArea")) == 0 ? -1 : int.Parse(client.GetValueFromHash(item, "FMArea")),//区域编号
FMClassify = int.Parse(client.GetValueFromHash(item, "FMClassify")) == 0 ? -1 : int.Parse(client.GetValueFromHash(item, "FMClassify")),//分类编号
LId = client.GetValueFromHash(item, "LId") as string,//标签编号
messager = client.GetValueFromHash(item, "messager") as string,//发帖人编号
FMTime = DateTime.Parse(client.GetValueFromHash(item, "FMTime")),//发帖时间
FMContent = client.GetValueFromHash(item, "FMContent") as string,//帖子内容
FBodyContent = client.GetValueFromHash(item, "FBodyContent") as string,//纯文本内容
FImagesUrl = client.GetValueFromHash(item, "FImagesUrl") as string,//图片路径
FMNumber = int.Parse(client.GetValueFromHash(item, "FMNumber")) == 0 ? -1 : int.Parse(client.GetValueFromHash(item, "FMNumber")),//点赞数量
FMViewCount = int.Parse(client.GetValueFromHash(item, "FMViewCount")) == 0 ? -1 : int.Parse(client.GetValueFromHash(item, "FMViewCount")),//浏览次数
jiazhi = int.Parse(client.GetValueFromHash(item, "jiazhi")) == 0 ? -1 : int.Parse(client.GetValueFromHash(item, "jiazhi")),//价值
Commentary = int.Parse(client.GetValueFromHash(item, "Commentary")) == 0 ? -1 : int.Parse(client.GetValueFromHash(item, "Commentary")),//评论数量
FMRecommend = int.Parse(client.GetValueFromHash(item, "FMRecommend")) == 0 ? -1 : int.Parse(client.GetValueFromHash(item, "FMRecommend")),//是否推荐
FMExamine = int.Parse(client.GetValueFromHash(item, "FMExamine")) == 0 ? -1 : int.Parse(client.GetValueFromHash(item, "FMExamine")),//是否审查
IsDelete = int.Parse(client.GetValueFromHash(item, "IsDelete")) == 0 ? -1 : int.Parse(client.GetValueFromHash(item, "IsDelete"))//是否删除
});//listObject end
}//foreach end
}//using end
return forumMains;
}
catch (Exception ex)
{
return null;
}
}//function end
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Page
{
///
/// 博客表
/// 说明:用户个人中心时,显示用户文章
///
public class pa_ForumMain
{
///
/// 当前页
///
public int pageNo { get; set; }
///
/// 每页显示的条数
///
public int pageSize { get; set; }
///
/// 数据总数
///
public int pageCount { get; set; }
///
/// 动态分页总数
///
public int pageCountByParameter { get; set; }
///
/// 用户账号
///
public string userNum { get; set; }
///
/// 用户博文
///
public List<Model.nf_ForumMain> forumMains { get; set; }
}
}
以上就是完整代码部分,亲测是可以使用的。
补充:我的前端是使用了“layui的流显示”==>点我哦
思路:和平常的分页一样,只是期间用户请求页的时候会先去缓存里面看是否存在,不在才会从数据库里面返回指定页区间的数据
本人觉得,思路比代码实现更重要。思路是代码的灵魂,所以我的代码仅供参考哦!早点睡觉哦,宝贝们