从托管缓存服务迁移到Azure缓存的Redis [英] Migrate from Managed Cache Service to Azure Cache for Redis
问题描述
以下是我使用的现有应用程序" Microsoft.ApplicationServer.Caching "。现在最新的Azure
虚拟机或应用服务将不支持。
ErrorCode< ERRCA0017> ;:SubStatus< ES0006>:
存在临时故障。 (一个或多个指定的缓存服务器
不可用。
因此计划迁移到 Azure Redis缓存。我可以从Azure管理缓存中获取Azure redis缓存系统的
方法或建议。
使用Core。 Common.Extensions;
命名空间Core.Common.Caching
{
使用System.Collections.Specialized;
使用Microsoft.ApplicationServer.Caching;
公共类AzureCacheProvider:ICacheProvider
{
private readonly NameValueCollection appSettings;
public AzureCacheProvider(NameValueCollection appSettings)
{
this.appSettings = appSettings;
}
#region公共方法和运算符
public T GetCacheItem< T>(string cacheKey,string configKey)
{
return this.GetCacheItem< T>(cacheKey,configKey," default");
}
public T GetCacheItem< T>(字符串cacheKey,字符串configKey,bool isSerialized)
{
抛出新的System.NotImplementedException();
}
public T GetCacheItem< T>(字符串cacheKey,字符串configKey,字符串logicalCacheName)
{
var obj = default(T);
if(this.appSettings [configKey] .ToBool())
{
using(var cacheFactory = new DataCacheFactory(new DataCacheFactoryConfiguration(logicalCacheName)))
{
var defaultCache = cacheFactory.GetDefaultCache();
return(T)defaultCache.Get(cacheKey);
}
}
return obj;
}
public void SetCacheItem< T>(string cacheKey,string configKey,T item)
{
this.SetCacheItem(cacheKey,configKey, item,"default";);
}
public void SetCacheItem< T>(string cacheKey,string configKey,T item,string logicalCacheName)
{
if(this.appSettings) [configKey] .ToBool())
{
using(var cacheFactory = new DataCacheFactory(new DataCacheFactoryConfiguration(logicalCacheName)))
{
var defaultCache = cacheFactory.GetDefaultCache();
defaultCache.Put(cacheKey,item);
}
}
}
public void RemoveCacheItem(string cacheKey,string configKey)
{
this.RemoveCacheItem( cacheKey,configKey,"default";);
}
public void RemoveCacheItem(string cacheKey,string configKey,string logicalCacheName)
{
if(!this.appSettings [configKey] .ToBool ())
{
return;
}
using(var cacheFactory = new DataCacheFactory(new DataCacheFactoryConfiguration(logicalCacheName)))
{
var defaultCache = cacheFactory.GetDefaultCache();
defaultCache.Remove(cacheKey);
}
}
#endregion
}
}
$
Hello AAzath
请查看此
文章有关将托管缓存服务迁移到Azure Cache for Redis的指导。
您的缓存大小是多少?
如果大小相当小,您的应用程序可以在启动时构建缓存,您可以考虑使用
.Net Core中的InMemory缓存
Here is my existing application used "Microsoft.ApplicationServer.Caching". Now latest Azure
Virtual Machine or App service won't support.
ErrorCode<ERRCA0017>:SubStatus<ES0006>:
There is a temp failure. (One or More specified cache servers
are unavailable.
Hence planed to migrate to Azure Redis cache. May i get some idea or suggestion for the
below methods Azure redis cache system from Azure Managed cache.
using Core.Common.Extensions; namespace Core.Common.Caching { using System.Collections.Specialized; using Microsoft.ApplicationServer.Caching; public class AzureCacheProvider : ICacheProvider { private readonly NameValueCollection appSettings; public AzureCacheProvider(NameValueCollection appSettings) { this.appSettings = appSettings; } #region Public Methods and Operators public T GetCacheItem<T>(string cacheKey, string configKey) { return this.GetCacheItem<T>(cacheKey, configKey, "default"); } public T GetCacheItem<T>(string cacheKey, string configKey, bool isSerialized) { throw new System.NotImplementedException(); } public T GetCacheItem<T>(string cacheKey, string configKey, string logicalCacheName) { var obj = default(T); if (this.appSettings[configKey].ToBool()) { using (var cacheFactory = new DataCacheFactory(new DataCacheFactoryConfiguration(logicalCacheName))) { var defaultCache = cacheFactory.GetDefaultCache(); return (T)defaultCache.Get(cacheKey); } } return obj; } public void SetCacheItem<T>(string cacheKey, string configKey, T item) { this.SetCacheItem(cacheKey, configKey, item, "default"); } public void SetCacheItem<T>(string cacheKey, string configKey, T item, string logicalCacheName) { if (this.appSettings[configKey].ToBool()) { using (var cacheFactory = new DataCacheFactory(new DataCacheFactoryConfiguration(logicalCacheName))) { var defaultCache = cacheFactory.GetDefaultCache(); defaultCache.Put(cacheKey, item); } } } public void RemoveCacheItem(string cacheKey, string configKey) { this.RemoveCacheItem(cacheKey, configKey, "default"); } public void RemoveCacheItem(string cacheKey, string configKey, string logicalCacheName) { if (!this.appSettings[configKey].ToBool()) { return; } using (var cacheFactory = new DataCacheFactory(new DataCacheFactoryConfiguration(logicalCacheName))) { var defaultCache = cacheFactory.GetDefaultCache(); defaultCache.Remove(cacheKey); } } #endregion } }
Hello AAzath
Please check this article for guidance on migrating managed cache service to Azure Cache for Redis.
What is you cache size?
If the size is considerably small and your app can build the cache on start up, you can consider using InMemory Caching in .Net Core as well.
这篇关于从托管缓存服务迁移到Azure缓存的Redis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!