redis在windows下配置和在.net中的使用
2021-04-10 04:27
下边给出了在.net中使用的方法
1 static void Main(string[] args) 2 { 3 //---------获取redisClient对象 4 IRedisClientsManager clientManager = new PooledRedisClientManager(new string[]
{ "127.0.0.1:6379", "10.122.0.1" }); 5 IRedisClient client = clientManager.GetClient(); 6 7 //简单测试数据 8 UserInfo user1 = new UserInfo() { UserName = "zs", Age = 18 }; 9 UserInfo user2 = new UserInfo() { UserName = "ls", Age = 20 }; 10 11 12 //---------------------------------string类型-------------------------------------------// 13 //string存一个对象 14 client.Set("user1", user1); 15 UserInfo userGot = client.Get ("user1"); 16 Console.WriteLine(userGot.UserName); 17 18 //string存一个list 19 List userlist = new List () { user1, user2 }; 20 client.Set >("userlist", userlist);//存入list 21 List
listGot = client.Get >("userlist");//取出list 22 foreach (UserInfo user in listGot) 23 { 24 Console.WriteLine(user.UserName); 25 } 26 27 28 //---------------------------------hash类型---------------------------------------// 29 30 client.SetEntryInHash("usert", "UserName", "zs"); 31 client.SetEntryInHash("usert", "Age", "18"); 32 33 Liststring> listKeys = client.GetHashKeys("usert");//获取key 结果:UserName,Age 34 Liststring> listValues = client.GetHashValues("usert");//获取值 结果:zs,18 35 Liststring> listAllK = client.GetAllKeys();//获取所有的key。 36 37 38 //-------------------------list类型(只支持
)------------------------ // 39 40 //队列使用 41 client.EnqueueItemOnList("listQueue", "item1"); 42 client.EnqueueItemOnList("listQueue", "item2"); 43 int count = client.GetListCount("listQueue"); 44 for (int i = 0; i ) 45 { 46 Console.WriteLine(client.DequeueItemFromList("listQueue"));//结果:item1,item2 先进先出 47 } 48 49 //栈使用 50 client.PushItemToList("listStack", "item1"); 51 client.PushItemToList("listStack", "item2"); 52 int count = client.GetListCount("listStack"); 53 for (int i = 0; i ) 54 { 55 Console.WriteLine(client.PopItemFromList("listStack"));//结果:item2,item1 先进后出 56 } 57 58 //-------------------------Set类型只支持-------------------------- // 59 //对Set类型进行操作 60 client.AddItemToSet("set1", "111"); 61 client.AddItemToSet("set1", "222"); 62 client.AddItemToSet("set1", "333"); 63 client.AddItemToSet("set1", "444"); 64 client.AddItemToSet("set1","555"); 65 66 //client.RemoveItemFromSet("set1", "111");//删除111 67 //获取set中的数据 68 HashSetstring> hashset = client.GetAllItemsFromSet("set1"); 69 foreach (string str in hashset) 70 { 71 Console.WriteLine(str);//每次abcde的顺序都不一样 72 } 73 74 client.AddItemToSet("set2", "444"); 75 client.AddItemToSet("set2", "555"); 76 client.AddItemToSet("set2", "666"); 77 client.AddItemToSet("set2", "777"); 78 79 //求并集 结果:1~7 80 HashSetstring> hashset1 = client.GetUnionFromSets(new string[]{"set1", "set2"});81 82 //求交集 结果:444 555 83 HashSetstring> hashset2 = client.GetIntersectFromSets(new string[] { "set1", "set2" });84 85 //求差集(第一个set有,第二个set没有) 结果:111 222 333 86 HashSetstring> hashset3 = client.GetDifferencesFromSet("set1",new string[]{"set2"}); 87 88 }
三、集群和密码
1、redis设置密码
redis没有实现访问控制这个功能,但是它提供了一个轻量级的认证方式,可以编辑redis.conf配置来启用认证。
加密方法1:初始化Redis密码:
配置文件中: requirepass 123321;(Ps:需重启Redis才能生效)
加密方法2:不重启Redis设置密码(shell中进行):
配置文件中: requirepass 123321;
shell执行 : redis 127.0.0.1:6379> config set requirepass 123321
登陆有密码的Redis:
redis-cli -p 6379
redis 127.0.0.1:6379> auth 123321
查询密码:
redis 127.0.0.1:6379> config get requirepass
(error) ERR operation not permitted
密码验证:
redis 127.0.0.1:6379> auth 123321
OK
再次查询:
redis 127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "123321"
2、 搭建redis集群
redis主从复制机制中一个master可以有多个slave,而一个slave也可以有多个slave,如此可是使用redis搭建多级的服务器集群。
一般写操作在master服务器中写,在通过主从复制写入slave服务器,查操作通过slave服务器中获取(因为写的操作一般比较少,而读的操作多)
怎么搭建集群:
1、三台电脑为例,一主二从,建立三个redis服务器MasterRedis,SlaveRedis1,SlavaRedis2
2、在Master的配置文件修改:
port 6379 ---- bind 192.168.2.153 ------ reqirepass 123321
3、在Slave1中
port 6379----- bind 192.168.2.154 ----- slaveof 192.168.2.153 6379 --- masterauth 123321
在Slave2中
port 6379 ----- bind 192.168.2.155 ----- slaveof 192.168.2.153 6379 --- masterauth 123321
4、推荐:Master关闭save和appendonly
Slave开启save和appendonly,bgrewriteaof
(Master只向内存写,不考虑持久化,让Slave来进行持久化和Aof日志记录,这样做的优势是Master的性能达到最好)
1 static void Main(string[] args) 2 { 3 //获取master服务器client,用于写操作 4 IRedisClientsManager masterClientManager = new PooledRedisClientManager(new string[]
{ "192.168.2.153:6379" }); 5 IRedisClient writeClient = masterClientManager.GetClient(); 6 writeClient.Password = "123321"; 7 8 //获取salve服务器client,用于读操作 9 IRedisClientsManager slaveClientManager = new PooledRedisClientManager(new string[]
{ "192.168.2.154:6379", "192.168.2.154:6379" }); 10 IRedisClient readClient = slaveClientManager.GetClient(); 11 readClient.Password = "123321"; 12 13 writeClient.Setstring>("testkey", "can you get me?"); 14 string getStr = readClient.Getstring>("testkey"); 15 Console.ReadKey(); 16 }
文章标题:redis在windows下配置和在.net中的使用
文章链接:http://soscw.com/index.php/essay/73635.html