ylbtech-阿里云-Redis-Help-连接实例-Redis客户端连接:.net客户端 |
1.返回顶部 |
1、
操作步骤如下所示:
git clone https://github.com/ServiceStack/ServiceStack.Redis
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using ServiceStack.Redis;namespace ServiceStack.Redis.Tests{ class Program { public static void RedisClientTest() { string host = "127.0.0.1";/*访问host地址*/ string password = "password";/*密码*/ RedisClient redisClient = new RedisClient(host, 6379, password); string key = "test-aliyun"; string value = "test-aliyun-value"; redisClient.Set(key, value); string listKey = "test-aliyun-list"; System.Console.WriteLine("set key " + key + " value " + value); string getValue = System.Text.Encoding.Default.GetString(redisClient.Get(key)); System.Console.WriteLine("get key " + getValue); System.Console.Read(); } public static void RedisPoolClientTest() { string[] testReadWriteHosts = new[] { "redis://password@127.0.0.1:6379"/*redis://密码@访问地址:端口*/ }; RedisConfig.VerifyMasterConnections = false;//需要设置 PooledRedisClientManager redisPoolManager = new PooledRedisClientManager(10/*连接池个数*/, 10/*连接池超时时间*/, testReadWriteHosts); for (int i = 0; i < 100; i++) { IRedisClient redisClient = redisPoolManager.GetClient();//获取连接 RedisNativeClient redisNativeClient = (RedisNativeClient)redisClient; redisNativeClient.Client = null;//ApsaraDB for Redis不支持client setname所以这里需要显示的把client对象置为null try { string key = "test-aliyun1111"; string value = "test-aliyun-value1111"; redisClient.Set(key, value); string listKey = "test-aliyun-list"; redisClient.AddItemToList(listKey, value); System.Console.WriteLine("set key " + key + " value " + value); string getValue = redisClient.GetValue(key); System.Console.WriteLine("get key " + getValue); redisClient.Dispose();// } catch (Exception e) { System.Console.WriteLine(e.Message); } } System.Console.Read(); } static void Main(string[] args) { //单链接模式 RedisClientTest(); //连接池模式 RedisPoolClientTest(); } }}
2、
2.返回顶部 |
3.返回顶部 |
4.返回顶部 |
5.返回顶部 |
1、
https://help.aliyun.com/document_detail/43848.html#section-cvv-lkc-5db 2、
6.返回顶部 |
![]() | 作者:ylbtech 出处:http://ylbtech.cnblogs.com/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 |