tencent cloud

云数据库 Tendis

产品动态
产品简介
产品概述
产品优势
应用场景
产品系列
产品性能
命令兼容性
地域和可用区
相关概念
相关产品
购买指南
计费概述
产品定价
欠费说明
快速入门
创建 Tendis 实例
连接 Tendis 实例
iptable 转发
操作指南
多语言连接
维护管理实例
监控功能
配置安全组
禁用命令
常见问题
使用常见问题
购买相关问题
连接登录问题
联系我们
Tendis 政策
隐私政策
数据处理和安全协议
词汇表

.Net 连接示例

PDF
聚焦模式
字号
最后更新时间: 2024-12-19 20:23:34
运行前必备: 下载并安装 ServiceStack.Redis
示例代码
不使用连接池
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.Redis;
using System;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string host = "10.xx.xx.46";//实例访问 host 地址
int port = 6379;// 端口信息
string instanceId = "bd87dadc-8xx1-4xx1-86dd-021xxxcde96";//实例 ID
string pass = "1234567q";//密码

RedisClient redisClient = new RedisClient(host, port, instanceId + ":" + pass);
string key = "name";
string value = "QcloudV5!";
redisClient.Set(key, value); //设置值
System.Console.WriteLine("set key:[" + key + "]value:[" + value + "]");
string getValue = System.Text.Encoding.Default.GetString(redisClient.Get(key)); //读取值
System.Console.WriteLine("value:" + getValue);
System.Console.Read();
}
}
}
使用 ServiceStack 4.0 连接池
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.Redis;
using System;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string[] testReadWriteHosts = new[] {
"redis://:fb92bxxxabf11e5:1234xx8a1A@10.x.x.1:6379"/*redis://:实例ID:密码@访问地址:端口*/
};
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;//需要设置
try
{
string key = "test1111";
string value = "test1111";
redisClient.Set(key, value);
redisClient.Dispose();//
}
catch (Exception e)
{
System.Console.WriteLine(e.Message);
}
}
System.Console.Read();
}
}
}
使用 ServiceStack 3.0 连接池
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.Redis;
using System;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string[] testReadWriteHosts = new[] {
"fb92bfxxbf11e5:123456xx1A@10.x.x.1:6379" /*实例ID:密码@访问地址:端口*/
};
PooledRedisClientManager redisPoolManager = new PooledRedisClientManager(10/*连接池个
数*/, 10/*连接池超时时间*/, testReadWriteHosts);
for (int i = 0; i < 100; i++)
{
IRedisClient redisClient = redisPoolManager.GetClient();//获取连接
try
{
string key = "test1111";
string value = "test1111";
redisClient.Set(key, value);
redisClient.Dispose();//
}
catch (Exception e)
{
System.Console.WriteLine(e.Message);
}
}
System.Console.Read();
}
}
}
运行结果



帮助和支持

本页内容是否解决了您的问题?

填写满意度调查问卷,共创更好文档体验。

文档反馈