Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ############################################## Variant 1 ##############################################
- ICacheService.cs
- namespace TicketingSystem.Web.Infrastructure.Caching
- {
- using System;
- public interface ICacheService
- {
- T GetOrSet<T>(string cacheKey, Func<T> getItemCallback) where T : class;
- void Clear(string cacheKey);
- }
- }
- InMemoryCache.cs
- namespace TicketingSystem.Web.Infrastructure.Caching
- {
- using System;
- using System.Runtime.Caching;
- public class InMemoryCache : ICacheService
- {
- public T GetOrSet<T>(string cacheKey, Func<T> getItemCallback) where T : class
- {
- T item = MemoryCache.Default.Get(cacheKey) as T;
- if (item == null)
- {
- item = getItemCallback();
- MemoryCache.Default.Add(cacheKey, item, DateTime.Now.AddMinutes(30));
- }
- return item;
- }
- public void Clear(string cacheKey)
- {
- MemoryCache.Default.Remove(cacheKey);
- }
- }
- }
- ############################################## Variant 2 ##############################################
- ICacheService.cs
- namespace Bookmarks.Web.Infrastructure.CacheService
- {
- using System.Collections.Generic;
- using Bookmarks.Web.ViewModels;
- public interface ICacheService
- {
- IList<BookmarkViewModel> Bookmarks { get; }
- }
- }
- BaseCacheService.cs
- namespace Bookmarks.Web.Infrastructure.CacheService
- {
- using System.Web;
- using Antlr.Runtime.Misc;
- public class BaseCacheService
- {
- protected T Get<T>(string cacheId, Func<T> getItemcallback) where T : class
- {
- var item = HttpRuntime.Cache.Get(cacheId) as T;
- if (item == null)
- {
- item = getItemcallback();
- HttpContext.Current.Cache.Insert(cacheId, item);
- return item;
- }
- return item;
- }
- }
- }
- MemoryCacheService.cs
- namespace Bookmarks.Web.Infrastructure.CacheService
- {
- using System.Collections.Generic;
- using System.Linq;
- using AutoMapper.QueryableExtensions;
- using Bookmarks.Data;
- using Bookmarks.Web.ViewModels;
- public class MemoryCacheService : BaseCacheService, ICacheService
- {
- private readonly IBookmarksData data;
- public MemoryCacheService(IBookmarksData data)
- {
- this.data = data;
- }
- public IList<BookmarkViewModel> Bookmarks
- {
- get
- {
- return this.Get<IList<BookmarkViewModel>>("Bookmarks", () =>
- this.data.Bookmarks
- .All()
- .OrderByDescending(x => x.Votes.Sum(v => v.Value))
- .Take(6)
- .Project()
- .To<BookmarkViewModel>()
- .ToList());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement