【问题标题】:Azure Managed Cache without webconfig没有 webconfig 的 Azure 托管缓存
【发布时间】:2015-06-20 07:40:13
【问题描述】:

我正在按照教程 https://azure.microsoft.com/en-us/documentation/articles/cache-dotnet-how-to-use-service/ 使用 Azure Redis 缓存。我如何不使用使用 webconfig 来使用这个缓存?这是我的代码。

c#代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.ApplicationServer.Caching;
using System.Data;
using System.Data.SqlClient;

namespace MoviesDB.CacheWrappers
{

    public class AzureManagedCache 
    {


        DataCache AzureCache = new DataCache("default");
        public void Add(string sqlQuery, DataTable records)
        {
            List<Movie> movies = new List<Movie>();
            movies = MovieDBContext.ConvertToMovies(records);
            AzureCache.Add("sqlQuery", movies);
        }

        public void Delete(string sqlQuery)
        {
            AzureCache.Remove("sqlQuery");
        }

        public object Read(string sqlQuery)
        {
            var movie = AzureCache.Get("sqlQuery");
            return movie;
        }



    }
}

和 webconfig.xml

<dataCacheClients>
    <dataCacheClient name="default">
      <!--To use the in-role flavor of Windows Azure Cache, set identifier to be the cache cluster role name 
      To use the Windows Azure Cache Service, set identifier to be the endpoint of the cache cluster--> 
      <autoDiscover isEnabled="true" identifier="******.cache.windows.net" />

      <localCache isEnabled="true" sync="TimeoutBased" objectCount="100000" ttlValue="300" />

      <!--Use this section to specify security settings for connecting to your cache. This section is not required if your cache is hosted on a role that is a part of your cloud service.--> 
      <securityProperties mode="Message" sslEnabled="true">
        <messageSecurity authorizationInfo="******" />
      </securityProperties>
    </dataCacheClient>
  </dataCacheClients>

【问题讨论】:

    标签: c# azure caching


    【解决方案1】:

    以编程方式与托管缓存对话的代码:

    var cacheSvcUri = "<your-cache-name>.cache.windows.net";
    
    // Setup DataCacheSecurity configuration
    string cacheSvcAcsKey = "<your-acs-key>";
    var secureAcsKey = new SecureString();
    foreach (char c in cacheSvcAcsKey)
    {
    secureAcsKey.AppendChar(c);
    }
    secureAcsKey.MakeReadOnly();
    
    // Setup the DataCacheFactory configuration
    DataCacheFactoryConfiguration dcfConfig = new DataCacheFactoryConfiguration();
    
    // Set autodiscover for in-role
    
    dcfConfig.AutoDiscoverProperty = new DataCacheAutoDiscoverProperty(true, cacheSvcUri);
    
    DataCacheSecurity factorySecurity = new DataCacheSecurity(secureAcsKey);
    dcfConfig.SecurityProperties = factorySecurity;
    
    // Create a configured DataCacheFactory object.
    DataCacheFactory dcf= new DataCacheFactory(dcfConfig); // make the DataCacheFactory static as it connects to the cache cluster.
    
    // Get a cache client for the default cache.
    DataCache defaultCache = dcf.GetDefaultCache();
    
    defaultCache.Put("One", 1);
    defaultCache.Put("Two", 2);
    

    您可以在缓存租户中定义要与哪个命名缓存通信后添加缓存操作。在这里,它是“默认”(DataCache defaultCache = dcf.GetDefaultCache())。如果您想与任何其他命名缓存通信,则应为DataCache defaultCache = dcf.GetCache("&lt;your-named-cache&gt;")

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-25
      • 2018-05-07
      • 2020-06-06
      • 2012-12-09
      • 2010-09-06
      相关资源
      最近更新 更多