阿里云-Redis-Help-连接实例-Redis客户端连接:.net客户端

ylbtech-阿里云-Redis-Help-连接实例-Redis客户端连接:.net客户端
1.
1、

.net客户端

操作步骤如下所示:

  1. 下载并使用.net客户端。 
    git clone https://github.com/ServiceStack/ServiceStack.Redis
  2. 在.net 客户端中新建.net项目。
  3. 添加客户端引用,引用文件在库文件的ServiceStack.Redis/lib/tests中。
  4. 在新建的.net项目中输入如下代码来连接云数据库Redis。详细的接口用法请参见接口说明 。(注意:ServiceStack.Redis.dll 使用v-4.0版本) 
    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://: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、
2、
 
6.
 
阿里云-Redis-Help-连接实例-Redis客户端连接:.net客户端作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

相关推荐