/// <summary> /// 获取消息 /// </summary> /// <param name="userid"></param> public List <ChatMsg> GetMessage(int userid) { List <ChatMsg> list = new List <ChatMsg>(); string redisKey = "userinfo" + userid; int count = 0; string jsonStr = ""; using (var client = RedisClientFactory.Instance.CreateRedisClient(redisIp, redisPort)) { lock (syncHelper) { count = client.GetListCount(redisKey); for (int i = 0; i < count; i++) { //出队一个元素并且将此元素删除 jsonStr = client.DequeueItemFromList(redisKey); //将json字符串反序列化程ChatMsg var chatmsg = Kits.Deserialize <ChatMsg>(jsonStr); //将ChatMsg对象实例追加到list集合中 list.Add(chatmsg); } } } return(list); }
public void SetMessage(Chat.Model.ChatMsg msg) { string cacheKey = "History"; using (var client = RedisClientFactory.Instance.CreateRedisClient("127.0.0.1", 6379)) { lock (syncHelper) { //入队成功 client.EnqueueItemOnList(cacheKey, Kits.ToJsonString(msg)); } } }
/// <summary> /// 保存消息到服务器队列中(Redis -> userinfo8->ChatMsg) /// </summary> /// <param name="msg"></param> public void SetMessage(ChatMsg msg) { string redisKey = "userinfo" + msg.ToUserId; //创建redis的客户端连接 using (var client = RedisClientFactory.Instance.CreateRedisClient(redisIp, redisPort)) { lock (syncHelper) { //入队成功 client.EnqueueItemOnList(redisKey, Kits.ToJsonString(msg)); } } }
/// <summary> /// 负责去redis中根据 string cacheKey = "History"; 获取历史消息数据集合 /// </summary> /// <returns></returns> List <ChatMsg> GetHistoryMsgList() { List <ChatMsg> list = new List <ChatMsg>(); string cacheKey = "History"; using (var client = RedisClientFactory.Instance.CreateRedisClient("127.0.0.1", 6379)) { int count = client.GetListCount(cacheKey); for (int i = 0; i < count; i++) { list.Add(Kits.Deserialize <ChatMsg>(client.DequeueItemFromList(cacheKey))); } } return(list); }
/// <summary> /// 给子线程每隔一段时间进行请求后,将数据保存到db中 /// </summary> /// <returns></returns> public List <Chat.Model.ChatMsg> GetMessage() { List <Chat.Model.ChatMsg> list = new List <Model.ChatMsg>(); string cacheKey = "History"; using (var client = RedisClientFactory.Instance.CreateRedisClient("127.0.0.1", 6379)) { lock (syncHelper) { int count = client.GetListCount(cacheKey); //出队成功 for (int i = 0; i < count; i++) { var chatmsg = Kits.Deserialize <Chat.Model.ChatMsg>(client.DequeueItemFromList(cacheKey)); list.Add(chatmsg); } } } return(list); }