2014-07-22 10 views
0

SQL 데이터베이스에서 이미지 검색을 위해 AppFabric을 사용하려고합니다. 공급자 파일을 만들어 내 캐시에로드했습니다. 그러나 나는 지금 고군분투하고있다.AppFabric-ReadThrough 구현에서 데이터를 가져 오는 방법은 무엇입니까?

공급자 파일을 사용하여 캐시에서 get 함수를 호출하려면 어떻게해야합니까? 아니면 데이터를 검색하는 동안 공급자 파일을 사용해야합니까?

.Get (key.Key)를 호출하면 데이터베이스에서 오는 데이터를 볼 필요가 있습니까?

공급자의 My Read 메서드가 다음과 같습니까? 이미지를 얻을 때

public override DataCacheItem Read(DataCacheItemKey key) 
    {  
     try 
     { 
      Object retrievedValue = null; 
      DataCacheItem cacheItem; 

      retrievedValue = *Running SQL Query Retrieving Image from DB*;//Is that a correct approach? 

      if (retrievedValue == null) 
       cacheItem = null; 
      else 
       cacheItem = DataCacheItemFactory.GetCacheItem(key, cacheName, retrievedValue, null); 
      return cacheItem; 
     } 
     catch 
     { 
      return null; 
     } 
    } 
+0

예, 그건. – stuartd

+0

의견을 보내 주셔서 감사합니다. 항목이 캐시에 없다면 AppFabric은 요청 된 항목을 캐시에 어떻게 기록합니까? – merimini

+0

'DataCacheItemFactory.GetCacheItem'에 전달할 때 – stuartd

답변

0
Example:> 

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

namespace SampleProvider 
{ 
    public class Provider : DataCacheStoreProvider 
    { 
    private readonly String dataCacheName; 
    private readonly Dictionary<string, string> config; 

    public Provider(string cacheName, Dictionary<string, string> config) 
    { 
     this.dataCacheName = cacheName; //Store the cache name for future use 
     this.config = config; 
    } 

    public override DataCacheItem Read(DataCacheItemKey key) 
    { 
     Object retrievedValue = null; 
     DataCacheItem cacheItem; 

     retrievedValue = ReadFromDatabase(key.Key); //Your implemented method that searches in the backend store based 

     if (retrievedValue == null) 
     cacheItem = null; 
     else 
     cacheItem = DataCacheItemFactory.GetCacheItem(key, dataCacheName, retrievedValue, null); 
     return cacheItem; 
    } 
    public override void Read(System.Collections.ObjectModel.ReadOnlyCollection<DataCacheItemKey> keys, IDictionary<DataCacheItemKey, DataCacheItem> items) 
    { 
     foreach (var key in keys) 
     { 
     items[key] = Read(key); 
     } 
    } 

    public override void Delete(System.Collections.ObjectModel.Collection<DataCacheItemKey> keys) { } 

    public override void Delete(DataCacheItemKey key) { } 

    protected override void Dispose(bool disposing) { } 

    public override void Write(IDictionary<DataCacheItemKey, DataCacheItem> items) { } 

    public override void Write(DataCacheItem item) { } 


    private string ReadFromDatabase(string key) 
    { 
     string value = string.Empty; 
     object retrievedValue = null; 
     using (SqlConnection connection = new SqlConnection(config["DBConnection"])) 
     { 
     SqlCommand cmd = new SqlCommand(); 
     cmd.CommandText = string.Format("select Value from KeyValueStore where [Key] = '{0}'", key); 
     cmd.Connection = connection; 
     connection.Open(); 
     retrievedValue = cmd.ExecuteScalar(); 
     if (retrievedValue != null) 
     { 
      value = retrievedValue.ToString(); 
     } 
     } 

     return value; 
    } 
    } 
}