CACHE MANAGER:
using System;
using System.Runtime.Caching;
namespace Quasar.Bll
{
public class CacheManager : ICacheProvider
{
private ObjectCache Cache { get { return MemoryCache.Default; } }
public object Get(string key)
{
return Cache[key];
}
public void Set(string key, object data, int cacheTime)
{
var policy = new CacheItemPolicy {
AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime)
};
Cache.Add(new CacheItem(key, data), policy);
}
public bool IsSet(string key)
{
return (Cache[key] != null);
}
public void Invalidate(string key)
{
Cache.Remove(key);
}
}
}
CACHE USAGE in BLL:
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Quasar.Bll.Models;
namespace Quasar.Bll
{
public class ProductsBusinessObject
{
public List<ProductSearchResult> GetProductData(string searchText)
{
var cache = new CacheManager();
var products = cache.Get(searchText) as List<ProductSearchResult>;
if (products != null)
{
return products;
}
var rawData = new Dal.ProductsDao().GetProductsData(searchText);
var searchedProducts = from prod in rawData
select
new ProductSearchResult
{
Breadcrumb = prod.Categories,
LastUpdated = prod.LastUpdated,
Price = float.Parse(prod.Price.ToString()),
ProductImage = prod.ProductMainImageUrl,
ProductLink = prod.ProductLink,
ProductTitle = prod.ProductTitle,
Provider = prod.SiteNameToCrawle,
ProviderLogo = ProvidersLogoManager.GetProviderLogo(prod.SiteNameToCrawle)
};
var result = searchedProducts.ToList();
cache.Set(searchText, result, 1);
return result;
}
}
}