阿里云推荐的Redis服务.NET客户端是ServiceStack.Redis,但ServiceStack.Redis不支持异步,不支持.NET Core,于是尝试使用StackExchange.Redis。Microsoft.Extensions.Caching.Redis用的也是StackExchange.Redis,于是直接使用Microsoft.Extensions.Caching.Redis。
测试代码如下:
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());
代码运行出错:
> 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支持了)。