阿里雲KVStore兼容Redis。由於KVStore就至關於Redis的服務器端,咱們代碼只是看成客戶端,連接上服務器端就好了,阿里雲的KVStore詳情文檔見,https://docs.aliyun.com/#/pub/kvstore/key-value-store/kvstore-introduction。git
1.阿里雲文檔上介紹的是用ServiceStack去連接KVStore。那咱們項目中就用nuget去下載ServiceStack包。nuget搜索ServiceStack,搜索到的結果以下圖,安裝圖中標識的就能夠了。這個應該是最新的V4版本,當提醒你須要用商業版的時候,能夠去還原以前的V3版本,具體還原方法見,https://github.com/ServiceStackV3/ServiceStackV3。github
nuget搜索結果以下:redis
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 return redisClient.Add<T>(key, value);