这次改动挺大的。
1.服务端和客户端改为Winform窗口。好多细节未处理,只是实现了功能。
2.使用Redis做为缓存,版本redis-3.0.1,和RedisDesktopManager做管理。增加二个类库,一个用于Redis数据实体,一个Redis操作和测试。
数据实体部份:
用户:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MessagePack;
namespace CommonModel
{
[MessagePackObject]
public class Users
{
public static string cacheKey = "Users";
[Key(0)]
public int ID { get; set; }
[Key(1)]
public string name { get; set; }
[Key(2)]
public string password { get; set; }
private DateTime? registered;
[Key(3)]
public DateTime? createtime
{
get
{
if (registered == null)
{
registered = DateTime.Now;
}
return registered.Value;
}
set { registered = value; }
}
public List<Users> ListDatas()
{
List<Users> users = new List<Users>
{
new Users{ ID=0,name="zs",password="zs" },
new Users{ ID=1,name="ls",password="ls" },
new Users{ ID=2,name="ww",password="ww" },
new Users{ ID=3,name="zl",password="zl" },
new Users{ ID=4,name="wb",password="wb" }
};
return users;
}
}
public class userFriends
{
/// <summary>
/// 好友ID
/// </summary>
public int friend_id { set; get; }
/// <summary>
/// 好友名称
/// </summary>
public string name { set; get; }
/// <summary>
/// 是否在线
/// </summary>
public bool isOnline { set; get; }
}
public class userGroups
{
public string group_id { set; get; }
public string group_name { set; get; }
public int user_id { set; get; }
public string user_name { set; get; }
/// <summary>
/// 是否在线
/// </summary>
public bool isOnline { set; get; }
}
}
好友:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CommonModel
{
public class Friends
{
public static string cacheKey = "Friends";
public int ID { get; set; }
public int user_id { get; set; }
public int friend_id { get; set; }
private DateTime? registered;
public DateTime? createtime
{
get
{
if (registered == null)
{
registered = DateTime.Now;
}
return registered.Value;
}
set { registered = value; }
}
public List<Friends> ListDatas()
{
List<Friends> friends = new List<Friends>
{
new Friends{ ID=0,user_id=0,friend_id=2 },
new Friends{ ID=1,user_id=0,friend_id=4 },
new Friends{ ID=3,user_id=1,friend_id=3 },
new Friends{ ID=4,user_id=1,friend_id=4 },
new Friends{ ID=5,user_id=2,friend_id=3 },
new Friends{ ID=6,user_id=2,friend_id=4 },
new Friends{ ID=7,user_id=2,friend_id=0 },
new Friends{ ID=8,user_id=3,friend_id=1 },
new Friends{ ID=9,user_id=3,friend_id=2 },
new Friends{ ID=10,user_id=3,friend_id=4 },
new Friends{ ID=11,user_id=4,friend_id=0 },
new Friends{ ID=12,user_id=4,friend_id=1 },
new Friends{ ID=13,user_id=4,friend_id=2 },
new Friends{ ID=14,user_id=4,friend_id=3 }
};
return friends;
}
}
}
群组:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CommonModel
{
public class Groups
{
public static string cacheKey = "Groups";
public int ID { get; set; }
public string group_id { get; set; }
public string group_name { get; set; }
public List<int> lUser { set; get; }
private DateTime? registered;
public DateTime? createtime
{
get
{
if (registered == null)
{
registered = DateTime.Now;
}
return registered.Value;
}
set { registered = value; }
}
public List<Groups> ListDatas()
{
List<Groups> groups = new List<Groups>
{
new Groups{ ID=0, group_id="gOne", group_name="一组", lUser=new List<int>{0,1,2 } },
new Groups{ ID=0, group_id="gTwo", group_name="二组", lUser=new List<int>{4,1,2 } },
new Groups{ ID=1, group_id="gThree", group_name="三组", lUser=new List<int>{3,4 } }
};
return groups;
}
}
}
Redis测试项目:用于将数据写入缓存,直接读取使用,直接运行测试,就将数据写入。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Cache.Redis;
using CommonModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace RedisUnitTest
{
[TestClass]
public class RedisTest
{
/// <summary>
/// 写入用户数据
/// </summary>
[TestMethod]
public void TestWriteUsersObject()
{
ICache cache = CacheFactory.Cache();
List<Users> u = new Users().ListDatas();
cache.Write(Users.cacheKey, u);
List<Users> temp = cache.Read<List<Users>>(Users.cacheKey);
var us = temp.Find(t => t.name.Equals("张三")&&t.password.Equals("zs"));
}
/// <summary>
/// 写入好友数据
/// </summary>
[TestMethod]
public void TestWriteFriendsObject()
{
ICache cache = CacheFactory.Cache();
List<Friends> u = new Friends().ListDatas();
cache.Write(Friends.cacheKey, u);
List<Friends> temp = cache.Read<List<Friends>>(Friends.cacheKey);
var us = temp.FindAll(t => t.user_id.Equals(4));
}
/// <summary>
/// 写入群组数据
/// </summary>
[TestMethod]
public void TestWriteGroupsObject()
{
ICache cache = CacheFactory.Cache();
List<Groups> u = new Groups().ListDatas();
cache.Write(Groups.cacheKey, u);
List<Groups> temp = cache.Read<List<Groups>>(Groups.cacheKey);
var us = temp.FindAll(t => t.ID.Equals(0));
}
/// <summary>
/// 测试删除数据
/// </summary>
[TestMethod]
public void TestRemove()
{
ICache cache = CacheFactory.Cache();
cache.Remove(Groups.cacheKey);
cache.RemoveAll();
}
}
}
。。。。Redis类库,代码太多,这里省略。
3.用户好友,群组,直接写入缓存读取使用。
using Cache.Redis;
using CommonModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace WinServer.UserInfo
{
public class GetUser
{
ICache cache = CacheFactory.Cache();
public Users GetUserById(int id) => cache.Read<List<Users>>(Users.cacheKey).Find(t => t.ID == id);
public Users UserLogin(Users temp)
{
List<Users> users = cache.Read<List<Users>>(Users.cacheKey);
Users self = users.Find(t => t.name.Equals(temp.name) && t.password.Equals(temp.password));
return self;
}
public IEnumerable<userFriends> GetFriendsByID(int id)
{
List<Users> users = cache.Read<List<Users>>(Users.cacheKey);
var lf = cache.Read<List<Friends>>(Friends.cacheKey).FindAll(t => t.user_id.Equals(id)).Select(
s => new userFriends
{
friend_id = s.friend_id,