我正在使用Azure Redis缓存服务的StackExchange.Redis客户端.这是我的班级,
public class RedisCacheService : ICacheService
{
private readonly ISettings _settings;
private readonly IDatabase _cache;
public RedisCacheService(ISettings settings)
{
_settings = settings;
var connectionMultiplexer = ConnectionMultiplexer.Connect(settings.RedisConnection);
_cache = connectionMultiplexer.GetDatabase();
}
public bool Exists(string key)
{
return _cache.KeyExists(key);
}
public void Save(string key, string value)
{
var ts = TimeSpan.FromMinutes(_settings.CacheTimeout);
_cache.StringSet(key, value, ts);
}
public string Get(string key)
{
return _cache.StringGet(key);
}
public void Remove(string key)
{
// How to remove one
}
public void Clear()
{
// How to …
Run Code Online (Sandbox Code Playgroud) 我有以下代码连接到azure redis缓存.
public class CacheConnectionHelper
{
private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
{
return ConnectionMultiplexer.Connect(SettingsHelper.AzureRedisCache);
});
public static ConnectionMultiplexer Connection
{
get
{
return lazyConnection.Value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我用这种方式使用它
public static List<Models.Module> GetModules()
{
IDatabase cache = CacheConnectionHelper.Connection.GetDatabase();
List<Models.Module> listOfModules = new List<Models.Module>();
listOfModules = (List<Models.Module>)cache.Get("ApplicationModules");
if (listOfModules == null)
{
listOfModules = dbApp.Modulos.ToList();
cache.Set("ApplicationModules", listOfModules, TimeSpan.FromMinutes(SettingsHelper.CacheModuleNames));
return listOfModules;
}
else {
return listOfModules;
}
}
Run Code Online (Sandbox Code Playgroud)
但是每天1或2次我得到这个例外:
Additional information: It was not possible to connect to the redis …
Run Code Online (Sandbox Code Playgroud) 我有点失落。我正在阅读有关使用 Redis 进行 ASP.NET Core 缓存的 Microsoft 文档。并且文档建议使用 Microsoft.Extensions.Caching.StackExchangeRedis,这是一个开源第三方库。
但是我看到其他一些教程正在使用 Microsoft.Extensions.Caching.Redis,这是一个更原生的 asp.net 核心。
最后他们都使用相同的接口 IDistributedCache
为什么我需要 StackExchangeRedis 以及它比 Microsoft.Extensions.Caching.Redis 有什么优势?
我是redis的新手,所以我做错了,我敢肯定:
我在Azure Redis中存储了大约16,000个键/值.
我使用以下代码来写入键/值
foreach (var worksheet in wksList)
{
var wksYYMM = string.Format("{0}{1:00}", worksheet.ReviewDt.Year, worksheet.ReviewDt.Month);
var wksKey = string.Format("{0}:{1}:{2}", provCode, wksYYMM, worksheet.AcctNbr);
string jsonStr = JsonConvert.SerializeObject( MakeWsListDto(worksheet, provCoderList, rvrList));
cache.StringSet(wksKey, jsonStr);
}
Run Code Online (Sandbox Code Playgroud)
所以我的键看起来像这样:"AP:201401:AZ5798BK"
当我尝试查找时:
var keys = server.Keys(pattern: "AP:201401:*"); // returns in milliseconds
var keyAry = keys.ToArray(); // returns in over one minute
(note: this returns 12 keys)
Run Code Online (Sandbox Code Playgroud)
返回键需要1分12秒.一旦我有了密钥,它需要几毫秒来检索那些值.如果我迭代键的值并返回值,我得到类似的结果.我做了ToArray()只是为了隔离问题.
如果我在redis-cli.exe中尝试相同的查询,它会在几毫秒内回来.
我错误地使用此命令了吗?
我在查询缓存的单个计算机的高负载情况下使用Azure Redis缓存.这台机器每秒大约可以获得约20件物品.在白天,这种情况会增加,在夜间这种情况会减少.
到目前为止,事情一直很好.今天我意识到"连接客户端"的指标非常高,尽管我只有一个客户端只是经常获取和设置项目.以下是我所指的指标的屏幕截图:
我的代码看起来像这样:
public class RedisCache<TValue> : ICache<TValue>
{
private IDatabase cache;
private ConnectionMultiplexer connectionMultiplexer;
public RedisCache()
{
ConfigurationOptions config = new ConfigurationOptions();
config.EndPoints.Add(GlobalConfig.Instance.GetConfig("RedisCacheUrl"));
config.Password = GlobalConfig.Instance.GetConfig("RedisCachePassword");
config.ConnectRetry = int.MaxValue; // retry connection if broken
config.KeepAlive = 60; // keep connection alive (ping every minute)
config.Ssl = true;
config.SyncTimeout = 8000; // 8 seconds timeout for each get/set/remove operation
config.ConnectTimeout = 20000; // 20 seconds to connect to the cache
connectionMultiplexer = ConnectionMultiplexer.Connect(config);
cache = connectionMultiplexer.GetDatabase();
}
public virtual …
Run Code Online (Sandbox Code Playgroud) 我正在尝试在Azure WebApp上使用StackExchange.Redis并需要运行一些Lua脚本.
建议的方法是将脚本加载到服务器,但我很难理解正确的模式.
我认为应该做的方式是在WebApp初创公司:
LuaScript luaScript = LuaScript.Prepare(scriptString);
var endpoints = redis.GetEndPoints();
var endpoint = endpoints[0];
IServer server = redis.GetServer(endpoint);
LoadedLuaScript loadedScript = luaScript.Load(server);
Run Code Online (Sandbox Code Playgroud)
然后保留LoadedLuaScript供以后使用(与ConnectionMultiplexer一起使用).然后当我想运行Lua脚本时:
IDatabase db = redis.GetDatabase();
db.ScriptEvaluate(loadedScript);
Run Code Online (Sandbox Code Playgroud)
这是正确的方法吗?
由于Azure可以有多个Redis节点,我应该为每个端点运行luaScript.Load并只保留其中一个以供以后使用吗?即:
LuaScript luaScript = LuaScript.Prepare(script);
var endpoints = redis.GetEndPoints();
LoadedLuaScript loadedScript;
foreach (var endpoint in endpoints)
{
IServer server = redis.GetServer(endpoint);
loadedScript = luaScript.Load(server);
}
return loadedScript;
Run Code Online (Sandbox Code Playgroud) 我们目前使用Redis作为我们Web应用程序的持久缓存,但由于内存和成本有限,我开始考虑Table存储是否可行.
我们存储的数据是相当基本的json数据,带有明确的2部分密钥,我们将其用于表存储中的分区和行密钥,因此我希望这意味着快速查询.
我很欣赏一个在内存中,一个在外面,所以表存储会慢一点但是随着我们的扩展,我相信只有一个CPU提供来自Redis缓存的数据,而对于Table存储,我们不会有这个问题,因为它会是减少到我们运行的Web服务器的数量.
有没有人有这种方式使用表存储的经验或2之间的比较.
我应该补充一点,我们以极简主义的方式使用Redis来获取/设置,仅此而已,我们逐出我们自己的数据,并且当它耗尽空间时将其驱逐到Redis.
我们在我们的应用程序中使用C1 Azure Redis Cache.最近我们在GET操作上遇到了很多时间.
根据这篇文章,可能的解决方案之一是实现ConnectionMultiplexer对象池.
另一种可能的解决方案是在客户端中使用ConnectionMultiplexer对象池,并在发送新请求时选择"负载最小"的ConnectionMultiplexer.这应该可以防止单个超时导致其他请求也超时.
如何使用C#实现一个ConnectionMultiplexer对象池?
编辑:
我有以下代码用于从缓存中获取信息.我不知道我的应用程序是否打开了太多连接,或者只是这个错误是由于天蓝色redis缓存的瞬态故障造成的.
这是堆栈跟踪
[RedisConnectionException:没有可用于此操作的连接:GET UserProfileInformation|globaladmin@xx.onmicrosoft.com]
1 processor, ServerEndPoint server) in c:\TeamCity\buildAgent\work\3ae0647004edff78\StackExchange.Redis\StackExchange\Redis\ConnectionMultiplexer.cs:1922 StackExchange.Redis.RedisBase.ExecuteSync(Message message, ResultProcessor
c:\ TeamCity\buildAgent\work中的StackExchange.Redis.ConnectionMultiplexer.ExecuteSyncImpl(消息消息,ResultProcessor 1处理器,ServerEndPoint服务器)\3ae0647004edff78\StackExchange.Redis\StackExchange\Redis\RedisBase.cs:80 StackExchange.Redis.RedisDatabase.StringGet(RedisKey key,CommandFlags标志)位于c:\ TeamCity\buildAgent\work\3ae0647004edff78\StackExchange.Redis\StackExchange\Redis\RedisDatabase.cs:1431 xx.Utils.SampleStackExchangeRedisExtensions.Get(IDatabase cache,String key)位于C:\ Proyectos\xx\xx\Utils\SampleStackExchangeRedisExtensions.cs:20
xx.Cache.UserProfile.GetUserProfile(String identityname)in C:\Proyectos\xx\xx\Cache\UserProfile.cs:22
x.Controllers.UserProfileController.GetPropertiesForUser()在C:\ Proyectos\xx\xx\Controllers\UserProfileController.cs:16
lambda_method(Closure,ControllerBase,Object []) +61
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase控制器,Object []参数)+14
这是代码
public static Models.UserProfile GetUserProfile(string identityname)
{
/// It needs to be cached for every user because every user can have different modules enabled.
var cachekeyname = "UserProfileInformation|" + identityname;
IDatabase cache = CacheConnectionHelper.Connection.GetDatabase();
Models.UserProfile userProfile = new Models.UserProfile();
object obj = cache.Get(cachekeyname);
string userProfileString;
if (obj …
Run Code Online (Sandbox Code Playgroud) 我需要使用 Azure Cache for Reddis 在我的应用程序中实现缓存,但我访问了一些博客,其中我可以选择使用 Azure CDN 存储我的响应或数据。
有人可以建议我它们之间有什么区别吗?
据我了解,Reddis 用于存储缓存数据,而 CDN 用于缓存数据以及附近服务器的更快响应