Search code examples
c#asp.netsql-serverappfabricappfabric-cache

AppFabric-ReadThrough implementation how to get the data?


I am trying to use AppFabric for fasten my image retrieval from SQL Database. I created my provider file and load it to my cache. However, I am struggling right now.

How can I call get function from cache using my provider file or do I need to use my provider file while retrieving data?

When I call .Get(key.Key), do I need to see the data coming from my database?

My Read method from provider is as follows, is this correct?

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;
        }
    }

Solution

  • 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;
        }
      }
    }