• 阿里云-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://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、
    2、
     
    6.返回顶部
     
    warn 作者:ylbtech
    出处:http://ylbtech.cnblogs.com/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Django中文无法转换成latin-1编码的解决方案
    JS-排序详解:冒泡排序、选择排序和快速排序
    javascript实例
    selenium webdriver 启动三大浏览器Firefox,Chrome,IE
    javascript教程5:--BOM操作
    python----图像简单处理(PIL or Pillow)
    Python模块学习之bs4
    采集淘宝美人库
    JavaScript 中的FileReader对象(实现上传图片预览)
    WPF
  • 原文地址:https://www.cnblogs.com/storebook/p/12657849.html
Copyright © 2020-2023  润新知