KVStore的简单介绍
阿里云KVStore兼容Redis。因为KVStore就相当于Redis的服务器端,我们代码只是当作客户端,链接上服务器端就行了,阿里云的KVStore详情文档见,https://docs.aliyun.com/#/pub/kvstore/key-value-store/kvstore-introduction。
C#客户端链接OCS
1.阿里云文档上介绍的是用ServiceStack去链接KVStore。那我们项目中就用nuget去下载ServiceStack包。nuget搜索ServiceStack,搜索到的结果如下图,安装图中标识的就可以了。这个应该是最新的V4版本,当提醒你需要用商业版的时候,可以去还原之前的V3版本,具体还原方法见,https://github.com/ServiceStackV3/ServiceStackV3。
nuget搜索结果如下:
2.安装好以后,写链接和调用kvstore的代码。其中_setting.AccessId, _setting.AccessKey, _setting.HostAddress,分别是KVStore的实例ID,链接密码和链接地址。
1 using ServiceStack.Redis;
2 //using ServiceStack.Text;
3 using ServiceStack.Common;
4 using System;
5 using System.Collections.Generic;
6 using System.Linq;
7 using System.Text;
8 using System.Threading.Tasks;
9 using System.Web.Script.Serialization;
10 using Zupo.Core.Caching;
11 using System.Text.RegularExpressions;
12
13
14 namespace KVStore
15 {
16 public class KVStoreService
17 {
18
19 IKVStoreSetting _setting;
20 private IRedisClient redisClient;
21 private bool linkServer = true;
22
23 public KVStoreService(IKVStoreSetting setting)
24 {
25 try
26 {
27 this._setting = setting;
28 //连接池模式
29 //string[] testReadWriteHosts = new[] {
30 //string.Format("redis://:{0}:{1}@{2}:6379",_setting.AccessId,_setting.AccessKey,_setting.HostAddress)/*redis://:实例id:密码@访问地址:端口*/
31 //};
32 //RedisClientManagerConfig RedisConfig = new RedisClientManagerConfig();
33 //RedisConfig.AutoStart = true;
34 //RedisConfig.MaxReadPoolSize = 60;
35 //RedisConfig.MaxWritePoolSize = 60;
36 ////RedisConfig.VerifyMasterConnections = false;//需要设置
37 ////PooledRedisClientManager redisPoolManager = new PooledRedisClientManager(10/*连接池个数*/, 10/*连接池超时时间*/, testReadWriteHosts);
38 //PooledRedisClientManager redisPoolManager = new PooledRedisClientManager(10/*连接池个数*/, 10/*连接池超时时间*/, testReadWriteHosts);
39 //redisClient = redisPoolManager.GetClient();//获取连接
40 ////RedisNativeClient redisNativeClient = (RedisNativeClient)redisClient;
41 ////redisNativeClient.Client = null;//KVStore不支持client setname所以这里需要显示的把client对象置为null
42 //var dbSize = redisClient.DbSize;
43
44 //单链接模式
45 //string host = _setting.HostAddress;/*访问host地址*/
46 //string password = string.Format("{0}:{1}", _setting.AccessId, _setting.AccessKey);/*实例id:密码*/
47 //redisClient = new RedisClient(host, 6379, password);
48 //var dbSize = redisClient.DbSize;
49
50 RedisClientManagerConfig RedisConfig = new RedisClientManagerConfig();
51 RedisConfig.AutoStart = true;
52 RedisConfig.MaxReadPoolSize = 60;
53 RedisConfig.MaxWritePoolSize = 60;
54 RedisConfig.DefaultDb = 1; //默认第一个db
55
56 PooledRedisClientManager prcm = new PooledRedisClientManager(new List<string>() { string.Format("{0}:{1}@{2}:6379", _setting.AccessId, _setting.AccessKey, _setting.HostAddress) },
57 new List<string>() { string.Format("{0}:{1}@{2}:6379", _setting.AccessId, _setting.AccessKey, _setting.HostAddress) }, RedisConfig);
58 redisClient = prcm.GetClient();
59 }
60 catch (Exception)
61 {
62 linkServer = false;
63 }
64 }
65
66 /// <summary>
67 /// 是否处于链接状态
68 /// </summary>
69 protected bool LinkServer
70 {
71 get
72 {
73 return linkServer;
74 }
75 }
76
77 /// <summary>
78 /// 根据传入的key-value添加一条记录,当key已存在返回false
79 /// </summary>
80 /// <typeparam name="T"></typeparam>
81 /// <param name="key"></param>
82 /// <param name="value"></param>
83 /// <returns></returns>
84 protected bool Add<T>(string key, T value)
85 {
86