2015-06-02 4 views
0

Я использую кэш лазурного редиса после учебника https://azure.microsoft.com/en-us/documentation/articles/cache-dotnet-how-to-use-service/. Как я могу использовать этот кеш без с помощью webconfig? Вот мой код.Azure Управляемый кеш без 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> 

ответ

0

код программно поговорить с управляемым кэша:

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("<your-named-cache>")

+0

спасибо ... это работает !!!! – Celia

Смежные вопросы