阿里雲推薦的Redis服務.NET客戶端是ServiceStack.Redis,但ServiceStack.Redis不支持異步,不支持.NET Core,因而嘗試使用StackExchange.Redis。Microsoft.Extensions.Caching.Redis用的也是StackExchange.Redis,因而直接使用Microsoft.Extensions.Caching.Redis。git
測試代碼以下:github
var options = new RedisCacheOptions { Configuration = "實例ID.redis.rds.aliyuncs.com:6379,password=實例ID:密碼" }; var cache = new RedisCache(options); var cacheKey = "redis-test"; var cacheValue = Encoding.UTF8.GetBytes("Hello, World!"); await cache.SetAsync(cacheKey, cacheValue, new DistributedCacheEntryOptions());
代碼運行出錯:redis
> StackExchange.Redis.RedisServerException: ERR unknown command eval at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot ification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at Microsoft.Extensions.Caching.Redis.RedisCache.<SetAsync>d__13.MoveNext()
而訪問本身搭建的redis服務器則沒這個問題。服務器
向阿里雲提交工單後獲知阿里雲redis服務目前不支持eval命令(更新:後來阿里雲redis支持了)。異步