Created
November 17, 2012 00:46
-
-
Save puleos/4092233 to your computer and use it in GitHub Desktop.
Service Stack Service Implementation w/ Redis Caching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using NBTY.Core.DTO.Puritan; | |
using ServiceStack.CacheAccess; | |
using ServiceStack.ServiceHost; | |
using ServiceStack.ServiceInterface; | |
using ServiceStack.ServiceInterface.ServiceModel; | |
namespace Puritan.Web.Services.Catalog.ServiceStack | |
{ | |
[RestService("/category/{LegacyId}")] | |
public class CategoryDetailRequest | |
{ | |
public Guid CatalogId { get; set; } | |
public int WebStoreId { get; set; } | |
public string Locale { get; set; } | |
public string Slug { get; set; } | |
public Guid Id { get; set; } | |
public int LegacyId { get; set; } | |
public bool IncludeAssociatedItems { get; set; } | |
public CategoryDetailRequest() | |
{ | |
IncludeAssociatedItems = false; | |
CatalogId = default(Guid); | |
WebStoreId = 1; | |
Locale = "en"; | |
} | |
} | |
[RestService("/cached/category")] | |
[RestService("/cached/category/{LegacyId}")] | |
public class CachedCategoryDetailRequest : CategoryDetailRequest | |
{ | |
} | |
class CategoryDetailService : RestServiceBase<CategoryDetailRequest> | |
{ | |
private readonly ICatalogService _catalogService; | |
public CategoryDetailService(ICatalogService catalogService) | |
{ | |
_catalogService = catalogService; | |
} | |
public override object OnGet(CategoryDetailRequest request) | |
{ | |
var category = _catalogService.GetCategory(request.LegacyId, request.IncludeAssociatedItems); | |
return new CategoryDetailRequestResponse { Category = category, CatalogId = request.CatalogId, | |
Locale = request.Locale, WebStoreId = request.WebStoreId }; | |
} | |
} | |
public class CachedCategoryDetailService : RestServiceBase<CachedCategoryDetailRequest> | |
{ | |
public ICacheClient CacheClient { get; set; } | |
public override object OnGet(CachedCategoryDetailRequest request) | |
{ | |
var expireInTimespan = new TimeSpan(1, 0, 0); | |
var cacheKey = "urn:categoryDetail:" + request.Locale + ":" + request.WebStoreId + ":" + | |
request.LegacyId; | |
return RequestContext.ToOptimizedResultUsingCache( | |
CacheClient, cacheKey, expireInTimespan, () => | |
{ | |
var service = ResolveService<CategoryDetailService>(); | |
return (CategoryDetailRequestResponse)service.Get(request); | |
}); | |
} | |
} | |
public class CategoryDetailRequestResponse : IHasResponseStatus | |
{ | |
public Guid CatalogId { get; set; } | |
public int WebStoreId { get; set; } | |
public string Locale { get; set; } | |
public ResponseStatus ResponseStatus { get; set; } | |
public Category Category { get; set; } | |
public CategoryDetailRequestResponse() | |
{ | |
ResponseStatus = new ResponseStatus(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment