static void GetValue() { var members = redisClient.GetAllItemsFromList("additemtolist"); foreach (var item in members) { Console.WriteLine(item); } string s = redisClient.GetItemFromList("additemtolist", 0); long length = redisClient.GetListCount("additemtolist"); Console.WriteLine(length); Console.WriteLine(s); }
static void Main(string[] args) { //1.0开启Redis的客户端,引入 using ServiceStack.Redis;命名空间 (连接服务器) using (RedisClient client = new RedisClient("127.0.0.1", 6379)) { //client.Password = "******"; //如果连接Redis的redis.windows.conf文件中的配置了访问密码,则在这设置将Password设为它的访问密码 //1.0---------------------------------- //将数据存储到了Redis服务器中 client.Set <string>("name", "zhangshang"); //获取数据 string name = client.Get <string>("name"); //删除key为蜀国的数据 client.Remove("蜀国"); //client.RemoveAllFromList("蜀国");或者移除key为蜀国的集合数据。 //2.0--------------------------------- //将数据存入到同一个key中,以list集合的方式存储。(看它如何在同一个key中添加不同的数据) client.AddItemToList("蜀国", "刘备"); client.AddItemToList("蜀国", "关羽"); client.AddItemToList("蜀国", "张飞"); client.AddItemToList("蜀国", "张飞"); //第一种获取数据的方式 (这里是获取key=“蜀国”的数据; 注意:这个key有两个值为“张飞”的数据) List <string> list = client.GetAllItemsFromList("蜀国"); list.ForEach(r => Console.WriteLine(r)); //输出:刘备 关羽 张飞 张飞 //第二种获取数据的方式 int listCount = (int)client.GetListCount("蜀国"); //获取key=“蜀国”的数据总条数 4 for (int i = 0; i < listCount; i++) { Console.WriteLine(client.GetItemFromList("蜀国", i)); } //3.0----------------------------------Set(消重) //用消重的方式,将数据存储到Redis服务器中 client.AddItemToSet("魏国", "曹操"); client.AddItemToSet("魏国", "曹操"); client.AddItemToSet("魏国", "曹植"); //获取数据 HashSet <string> ha = client.GetAllItemsFromSet("魏国"); //它返回的是一个HashSet的集合 List <string> list1 = ha.ToList(); //转成List list1.ForEach(r => Console.WriteLine(r)); //输出:曹操 曹植 (注意:因为我们写入了两个曹操,但是这里使用了Set去重,数以只输出了一个曹操) //4.0----------------------------------队列(队列的特点:先进先出) client.EnqueueItemOnList("吴国", "孙坚"); client.EnqueueItemOnList("吴国", "孙策"); client.EnqueueItemOnList("吴国", "周瑜"); int clistCount = (int)client.GetListCount("吴国"); //获取key=“吴国”的数据总条数 3 for (int i = 0; i < clistCount; i++) { //出队列(出了对列的数据项,都会被删除,所以如果一个数据项出了对列后,那么Redis里面就会被删除) Console.WriteLine(client.DequeueItemFromList("吴国")); } //为了测试出队列的数据项是否被删除,我们来做一个检测 if (client.GetAllItemsFromList("吴国").Any() == false) { Console.WriteLine("已经全部出队了,没有数据了"); } List <UserInfo> u = new List <UserInfo>() { new UserInfo() { Name = "1", Age = 1 }, new UserInfo() { Name = "2", Age = 11 }, new UserInfo() { Name = "3", Age = 12 }, }; //存储 client.Set <List <UserInfo> >("test", u); //获取 var q = client.Get <List <UserInfo> >("test"); foreach (var item in q) { Console.WriteLine(item.Name); } Console.ReadLine(); Console.ReadKey(); } }
static void Main(string[] args) { //初始化RedisClient:IP地址 端口 密码 数据库下标 RedisClient client = new RedisClient(IP, PORT); //删除所有数据库 //client.FlushAll(); //删除当前数据库 //client.FlushDb(); //选择数据库 //client.ChangeDb(1); #region String Console.WriteLine("=====value为普通字符串的存储======="); //AddMilliseconds:返回一个新的 System.DateTime,它将指定的毫秒数加到此实例的值上 时间: //过期时间 //添加key-value client.Add <string>("timeout", "30秒之后消失", DateTime.Now.AddMilliseconds(30000)); Console.WriteLine("消失时间:{0}", client.Get <string>("timeout")); Console.WriteLine("是否存在:{0}", client.ContainsKey("timeout")); //移除key client.Remove("timeout"); //是否包含key Console.WriteLine("是否存在:{0}", client.ContainsKey("timeout")); Console.WriteLine("======value为自定义对象的储存====="); Student stu = new Student() { Name = "zhangsan", Class = "class1" }; client.Add <Student>("stu", stu, DateTime.MaxValue); Student s = client.Get <Student>("stu"); Console.WriteLine("是否存在学生对象:{0}", client.ContainsKey("stu")); Console.WriteLine("Name={0}, Class={1}", s.Name, s.Class); #endregion client.FlushDb(); #region Hash Console.WriteLine("=====value为哈希值的储存====="); //保存一条信息 client.SetEntryInHash("czy", "Name", "ethan"); client.SetEntryInHash("czy", "Location", "ChangSha"); //获取所有keys List <string> keys = client.GetHashKeys("czy"); foreach (string key in keys) { //Name Location Console.WriteLine("key = {0}", key); } List <string> allKeys = client.GetAllKeys(); foreach (string kk in allKeys) { //czy Console.WriteLine("kk = {0}", kk); } //获取所有values List <string> values = client.GetHashValues("czy"); foreach (string value in values) { Console.WriteLine("values = {0}", value); } #endregion client.FlushDb(); #region List //入栈 client.EnqueueItemOnList("czy", "ethan"); client.EnqueueItemOnList("czy", "changsha"); client.EnqueueItemOnList("czy", "iOS"); client.EnqueueItemOnList("czy", "c#"); long count = client.GetListCount("czy"); long count1 = client.LLen("czy"); Console.WriteLine("数量:{0}{1}", count, count1); //根据下标取值 for (int i = 0; i < count; i++) { Console.WriteLine("list根据下标取值:{0}", client.GetItemFromList("czy", i)); } //插入新数据 byte[] ii = System.Text.Encoding.Default.GetBytes("iOS"); byte[] nn = System.Text.Encoding.Default.GetBytes("web"); //true:在前面插入 false:在后面插入 client.LInsert("czy", true, ii, nn); //获取所有value List <string> listValues = client.GetAllItemsFromList("czy"); foreach (string ss in listValues) { Console.WriteLine("ss = {0}", ss); } //挨个出栈 先进后出 for (int i = 0; i < count; i++) { //string dequeueString = client.DequeueItemFromList("czy"); string dequeueString = client.PopItemFromList("czy"); Console.WriteLine("当前出栈:{0}", dequeueString); } #endregion //清空数据库 client.FlushDb(); #region Set Console.WriteLine("=====set====="); client.AddItemToSet("czy1", "ethan"); client.AddItemToSet("czy1", "iOS"); client.AddItemToSet("czy1", "c#"); //数量 Console.WriteLine("set数量:{0}", client.GetSetCount("czy1")); //所有值 HashSet <string> sset1 = client.GetAllItemsFromSet("czy1"); Console.WriteLine(); Console.WriteLine("====sset1===="); foreach (string sss1 in sset1) { Console.WriteLine("sss1 = {0}", sss1); } client.AddItemToSet("czy2", "ethan"); client.AddItemToSet("czy2", "changsha"); client.AddItemToSet("czy2", "pingpong"); HashSet <string> sset2 = client.GetAllItemsFromSet("czy2"); Console.WriteLine(); Console.WriteLine("====sset2===="); foreach (string sss2 in sset2) { Console.WriteLine("sss2 = {0}", sss2); } //并集 HashSet <string> unionSets = client.GetUnionFromSets(new string[] { "czy1", "czy2" }); Console.WriteLine("集合并集:"); foreach (string unions in unionSets) { Console.WriteLine(unions); } //交集 HashSet <string> intersectSets = client.GetIntersectFromSets(new string[] { "czy1", "czy2" }); Console.WriteLine("集合交集:"); foreach (string intersects in intersectSets) { Console.WriteLine(intersects); } //差集://[返回存在于第一个集合,但是不存在于其他集合的数据。差集] HashSet <string> differenceSets = client.GetDifferencesFromSet("czy1", new string[] { "czy2" }); Console.WriteLine("集合差集:"); foreach (string difference in differenceSets) { Console.WriteLine(difference); } Console.WriteLine(); HashSet <string> differenceSets2 = client.GetDifferencesFromSet("czy2", new string[] { "czy1" }); foreach (string diff in differenceSets2) { Console.WriteLine(diff); } #endregion client.FlushDb(); #region Sorted Set client.AddItemToSortedSet("czy", "ethan"); client.AddItemToSortedSet("czy", "changsha"); client.AddItemToSortedSet("czy", "iOS"); client.AddItemToSortedSet("czy", "c#"); List <string> hset = client.GetAllItemsFromSortedSet("czy"); foreach (string ssss in hset) { Console.WriteLine("Sorted Set = {0}", ssss); } #endregion Console.ReadKey(); }