Advertisement
Venciity

cache

Jun 12th, 2015
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.97 KB | None | 0 0
  1. ############################################## Variant 1 ##############################################
  2. ICacheService.cs
  3. namespace TicketingSystem.Web.Infrastructure.Caching
  4. {
  5.     using System;
  6.  
  7.     public interface ICacheService
  8.     {
  9.         T GetOrSet<T>(string cacheKey, Func<T> getItemCallback) where T : class;
  10.  
  11.         void Clear(string cacheKey);
  12.     }
  13. }
  14.  
  15. InMemoryCache.cs
  16. namespace TicketingSystem.Web.Infrastructure.Caching
  17. {
  18.     using System;
  19.     using System.Runtime.Caching;
  20.  
  21.     public class InMemoryCache : ICacheService
  22.     {
  23.         public T GetOrSet<T>(string cacheKey, Func<T> getItemCallback) where T : class
  24.         {
  25.             T item = MemoryCache.Default.Get(cacheKey) as T;
  26.             if (item == null)
  27.             {
  28.                 item = getItemCallback();
  29.                 MemoryCache.Default.Add(cacheKey, item, DateTime.Now.AddMinutes(30));
  30.             }
  31.  
  32.             return item;
  33.         }
  34.  
  35.         public void Clear(string cacheKey)
  36.         {
  37.             MemoryCache.Default.Remove(cacheKey);
  38.         }
  39.     }
  40. }
  41.  
  42. ############################################## Variant 2 ##############################################
  43. ICacheService.cs
  44. namespace Bookmarks.Web.Infrastructure.CacheService
  45. {
  46.     using System.Collections.Generic;
  47.  
  48.     using Bookmarks.Web.ViewModels;
  49.  
  50.     public interface ICacheService
  51.     {
  52.         IList<BookmarkViewModel> Bookmarks { get; }
  53.     }
  54. }
  55.  
  56. BaseCacheService.cs
  57. namespace Bookmarks.Web.Infrastructure.CacheService
  58. {
  59.     using System.Web;
  60.  
  61.     using Antlr.Runtime.Misc;
  62.  
  63.     public class BaseCacheService
  64.     {
  65.         protected T Get<T>(string cacheId, Func<T> getItemcallback) where T : class
  66.         {
  67.             var item = HttpRuntime.Cache.Get(cacheId) as T;
  68.             if (item == null)
  69.             {
  70.                 item = getItemcallback();
  71.                 HttpContext.Current.Cache.Insert(cacheId, item);
  72.                 return item;
  73.             }
  74.  
  75.             return item;
  76.         }
  77.     }
  78. }
  79.  
  80. MemoryCacheService.cs
  81. namespace Bookmarks.Web.Infrastructure.CacheService
  82. {
  83.     using System.Collections.Generic;
  84.     using System.Linq;
  85.  
  86.     using AutoMapper.QueryableExtensions;
  87.  
  88.     using Bookmarks.Data;
  89.     using Bookmarks.Web.ViewModels;
  90.  
  91.     public class MemoryCacheService : BaseCacheService, ICacheService
  92.     {
  93.         private readonly IBookmarksData data;
  94.  
  95.         public MemoryCacheService(IBookmarksData data)
  96.         {
  97.             this.data = data;
  98.         }
  99.  
  100.         public IList<BookmarkViewModel> Bookmarks
  101.         {
  102.             get
  103.             {
  104.                 return this.Get<IList<BookmarkViewModel>>("Bookmarks", () =>
  105.                     this.data.Bookmarks
  106.                         .All()
  107.                         .OrderByDescending(x => x.Votes.Sum(v => v.Value))
  108.                         .Take(6)
  109.                         .Project()
  110.                         .To<BookmarkViewModel>()
  111.                         .ToList());
  112.             }
  113.         }
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement