DotNet 笔记

东方欲晓,莫道君起早。

.Net Redis 基本用法。

准备工作:

1:环境,下载redis环境的安装包。官网上有,这里也提供一份windows下64位的

Redis-x64-3.2.100.msi (5.80 mb)

 

2:C#开发用的dll,Github上的地址:https://github.com/ServiceStack/ServiceStack.Redis

Redisdll.rar (175.58 kb)

第一步:安装redis环境,然后进入安装目录,在当前目录打开命令行工具,执行以下代码:

redis-server.exe redis.windows.conf

启动成功后,如图:

 第二步,新建一个项目,引用事先下载好的dll文件

 第三步,代码中调用:

        static void Main(string[] args)
        {
            //创建一个redis客户端对象,所有的操作都是通过这个对象
            RedisClient redisClient = new RedisClient("127.0.0.1", 6379);
            #region 简单使用
       
            Console.WriteLine("Redis写入缓存:KeyTest");
            redisClient.Add("KeyTest", "哈哈哈哈哈哈", DateTime.Now.AddDays(1));
            Console.WriteLine("Redis获取缓存:KeyTest");
            string str3 = redisClient.Get<string>("KeyTest");
            Console.WriteLine(str3);


            redisClient.Remove("KeyTest");
            str3 = redisClient.Get<string>("KeyTest");
            Console.WriteLine(str3);


            Console.WriteLine("Redis获取缓存:nihao");
            string str = redisClient.Get<string>("nihao");
            Console.WriteLine(str);


            Console.WriteLine("Redis获取缓存:wei");
            string str1 = redisClient.Get<string>("wei");
            Console.WriteLine(str1);
            #endregion


            #region 集合运算
          
            //对Set类型(普通的set为无序集合,sortSet是有序集合)进行操作  
            redisClient.Remove("set");
            redisClient.AddItemToSet("set", "aaa");
            redisClient.AddItemToSet("set", "bbb");
            redisClient.AddItemToSet("set", "ccc");
            redisClient.AddItemToSet("set", "ddd");
            redisClient.AddItemToSet("set", "eee");
            HashSet<string> hashset = redisClient.GetAllItemsFromSet("set");

            Console.WriteLine("遍历key为set的集合\r\n");
            foreach (string s in hashset)
            {
                Console.WriteLine(s);
            }

          
            //求并集        
            Console.WriteLine("求并集set1和set2集合\r\n");
            redisClient.RemoveAll(new List<string> { "set1","set2" });
            redisClient.AddItemToSet("set1", "aa");
            redisClient.AddItemToSet("set1", "bb");
            redisClient.AddItemToSet("set2", "bb");
            redisClient.AddItemToSet("set2", "cc");
            hashset = redisClient.GetUnionFromSets(new string[] { "set1", "set2" });
            foreach (string s in hashset)
            {
                Console.WriteLine(s);
            }


            //求交集 
            Console.WriteLine("求交集set1和set2集合\r\n");
            hashset = redisClient.GetIntersectFromSets(new string[] { "set1", "set2" });

            foreach (string s in hashset)
            {
                Console.WriteLine(s);
            }

            //求差集.  
            Console.WriteLine("求差集set1和set2集合\r\n");
            hashset = redisClient.GetDifferencesFromSet("set1", new string[] { "set2" });
            foreach (string s in hashset)
            {
                Console.WriteLine(s);
            }

            //SortedSet类型(有序集合)
            Console.WriteLine("Sorted Set类型排序\r\n");
            redisClient.RemoveAll(new List<string> { "sortList" });
            redisClient.AddItemToSortedSet("sortList", "1");
            redisClient.AddItemToSortedSet("sortList", "9");
            redisClient.AddItemToSortedSet("sortList", "3");
            redisClient.AddItemToSortedSet("sortList", "8");
            List<string> sortList = redisClient.GetAllItemsFromSortedSet("sortList");
            foreach (string s in sortList)
            {
                Console.WriteLine(s);
            }

            //清空所有数据
            redisClient.FlushAll();
          
            #endregion

            #region 集合排序存储
          
            //对Set类型进行操作  
            redisClient.Remove("set");
            redisClient.AddItemToSortedSet("set", "aaa");
            redisClient.AddItemToSortedSet("set", "bbb");
            redisClient.AddItemToSortedSet("set", "ccc");
            redisClient.AddItemToSortedSet("set", "ddd");
            redisClient.AddItemToSortedSet("set", "eee");
            //读取出来,用于遍历验证排序
            List<string> sList = redisClient.GetAllItemsFromSortedSet("set");
            Console.WriteLine("遍历key为set的集合\r\n");
            foreach (string s in sList)
            {
                Console.WriteLine(s);
            }
           
            #endregion

        }

 OK!

 其他注意的是:

1:如果windows下安装Redis第一次启动报错:

[2368] 21 Apr 02:57:05.611 # Creating Server TCP listening socket 127.0.0.1:6379: bind: No error

解决方法:在命令行中运行

redis-cli.exe

127.0.0.1:6379>shutdown

not connected>exit

然后重新运行redis-server.exe redis.windows.conf,启动成功!

 

2:由于上面虽然启动了redis,但是只要一关闭cmd窗口,redis就会消失。所以要把redis设置成windows下的服务。

Redis安装成windows服务的,开机自启动,命令如下:

redis-server --service-install redis.windows.conf

安装完之后,就可看到Redis已经作为windows服务了:

但是安装好之后,Redis并没有启动,启动命令如下:

redis-server --service-start

停止命令:

redis-server --service-stop

 3:redis多线程读写出现脏读问题。

大神们写的很好:http://www.cnblogs.com/tdws/p/5712835.html

 

 

Loading