我有一个适用于 .Net 4.6.2 框架的 asp.net mvc 应用程序。此应用程序具有使用 SimpleInjector 的控制反转技术的依赖注入和使用 PostSharp 的面向方面的编程技术。
当我开始在 Visual Studio 2015 Ent 中调试我的解决方案时,StackExchange.Redis 库在我的本地机器上运行良好。在 Windows 10 专业版上。当我在远程服务器上部署/发布我的应用程序 IIS 服务器时,我可以在本地的 redis 服务器上写入和读取,我的应用程序也可以在本地的 redis 服务器上写入和读取。
但是我不能在远程服务器上写redis服务器。我检查了端口和防火墙,但它无法以任何方式写入或读取。此外,当我跟踪我的应用程序时,它可以成功连接到同一服务器上的 redis 服务器,也可以向它发送命令,但是当查找 redis 监视器时,它不会显示该命令。
这可能是什么原因?
代码示例如下
Redis 缓存管理器
using System;
using System.Collections.Generic;
using System.Configuration;
using Newtonsoft.Json;
using StackExchange.Redis;
namespace Cits.Portal.Core.CrossCuttingConcern.Caching.Redis
{
public class RedisCacheManager : ICacheManager
{
private static readonly Lazy<ConnectionMultiplexer> LazyConnection = new Lazy<ConnectionMultiplexer>(() =>
{
var configurationOptions = new ConfigurationOptions();
#if DEBUG
configurationOptions.EndPoints.Add("localhost", 6379);
#else
configurationOptions.EndPoints.Add("141.11.11.212", 6379);
#endif
configurationOptions.AllowAdmin = true;
configurationOptions.AbortOnConnectFail = false;
return ConnectionMultiplexer.Connect(configurationOptions);
});
public static ConnectionMultiplexer Connection => LazyConnection.Value;
public static IDatabase RedisCache => Connection.GetDatabase();
public void Add(string key, object data, int cacheTime)
{
if (data == null || IsAdd(key))
return;
var value = TimeSpan.FromMinutes(cacheTime);
RedisCache.StringSet(key, Serialize(data), value);
}
public T Get<T>(string key)
{
var value = RedisCache.StringGet(key);
if (!value.HasValue)
return default(T);
return Deserialize<T>(value);
}
public bool IsAdd(string key)
{
return RedisCache.KeyExists(key);
}
public void Remove(string key)
{
RedisCache.KeyDelete(key);
}
public void RemoveByPattern(string pattern)
{
var endPoints = Connection.GetEndPoints();
foreach (var endpoint in endPoints)
{
var server = Connection.GetServer(endpoint);
var enumerable = server.Keys(RedisCache.Database, pattern);
foreach (var current in enumerable)
Remove(current);
}
}
public void Clear()
{
var endPoints = Connection.GetEndPoints();
foreach (var endpoint in endPoints)
{
var server = Connection.GetServer(endpoint);
var enumerable = server.Keys(RedisCache.Database);
foreach (var current in enumerable)
Remove(current);
}
}
public List<string> GetKeyList()
{
var list = new List<string>();
var endPoints = Connection.GetEndPoints();
foreach (var endpoint in endPoints)
{
var server = Connection.GetServer(endpoint);
var enumerable = server.Keys(RedisCache.Database);
foreach (var redisKey in enumerable)
list.Add(redisKey);
}
return list;
}
protected virtual string Serialize(object serializableObject)
{
var jsonSerializerSettings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
};
return JsonConvert.SerializeObject(serializableObject, jsonSerializerSettings);
}
protected virtual T Deserialize<T>(string serializedObject)
{
if (serializedObject == null)
return default(T);
var jsonSerializerSettings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
};
return JsonConvert.DeserializeObject<T>(serializedObject, jsonSerializerSettings);
}
}
}
Redis 缓存方面
using System;
using System.Linq;
using Cits.Portal.Core.CrossCuttingConcern.Caching.Redis;
using PostSharp.Aspects;
namespace Cits.Portal.Core.Aspects.Caching
{
[Serializable]
public class CacheAspectAttribute : MethodInterceptionAspect
{
private readonly int _cacheTimeOut;
public CacheAspectAttribute(int cacheTimeOut = 540)
{
_cacheTimeOut = cacheTimeOut;
}
public override void OnInvoke(MethodInterceptionArgs args)
{
var cacheManager = new RedisCacheManager();
if (args.Method.ReflectedType != null)
{
var methodFullName = $"{args.Method.ReflectedType.Namespace}.{args.Method.ReflectedType.Name}.{args.Method.Name}";
var arguments = args.Arguments.ToList();
var key = $"{methodFullName}({string.Join(",", arguments.Select(x => x?.ToString() ?? "<null>"))})";
if (cacheManager.IsAdd(key))
{
args.ReturnValue = cacheManager.Get<object>(key);
return;
}
base.OnInvoke(args);
cacheManager.Add(key, args.ReturnValue, _cacheTimeOut);
}
}
}
}
我们缓存的模块列表方法
[CacheAspect]
public List<ModuleViewModel> GetListAsList()
{
var rowLogQuery = _rowLogService.GetListQueryable("Module");
var moduleQuery =
_moduleDal.GetQueryable(p => p.RowStateId != _rowState)
.Select(p => new ModuleViewModel
{
Id = p.Id,
Code = p.Code,
Name = p.Name,
IsActive = p.IsActive,
RowLogViewModel = rowLogQuery.FirstOrDefault(q => q.RowId.Equals(p.Id)),
RowStateId = p.RowStateId
}).ToList();
return moduleQuery;
}
这些也是我的 redis.windows.configs
bind 127.0.0.1
bind 141.11.11.212
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 0
databases 16
这些也是我的 redis.windows.service.configs
bind 127.0.0.1
bind 141.11.11.212
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 0
databases 16
我也没有 redis auth 密码。我正在使用远程服务器浏览器在远程服务器上测试我的应用程序,但结果相同。
你能给我一些关于我可以找到问题的步骤的建议吗?
这就是 redis.server.log
[2252] 04 Aug 15:05:42.664 # Creating Server TCP listening socket 141.11.11.212:6379: bind: No error
[7504] 07 Aug 10:03:01.666 * Redis 3.2.100 (00000000/0) 64 bit, standalone mode, port 6379, pid 7504 ready to start.
[7504] 07 Aug 10:03:01.666 # Server started, Redis version 3.2.100
[7504] 07 Aug 10:03:01.666 * DB loaded from disk: 0.002 seconds
[7504] 07 Aug 10:03:01.666 * The server is now ready to accept connections on port 6379