我目前正在使用服务堆栈 ICacheClient 缓存在内存中。
注意:下面的代码是我需要删除客户特定名称的一些伪代码。
可以说我有以下聚合:
博文 => 评论
我想这样做:
// So I need to go get the blogPost and cache it:
var blogPostExpiration = new TimeSpan(0, 0, 30);
var blogPostCacheKey = GenerateUniqueCacheKey<BlogPostRequest>(request);
blogPostResponse = base.RequestContext.ToOptimizedResultUsingCache<BlogPostResponse>(base.CacheClient, blogPostCacheKey, blogPostExpiration, () =>
_client.Execute((request)));
// Then, annoyingly I need to decompress it to json to get the response back into my domain entity structure: BlogPostResponse
string blogJson = StreamExtensions.Decompress(((CompressedResult)blogPostResponse).Contents, CompressionTypes.Default);
response = ServiceStack.Text.StringExtensions.FromJson<BlogPostResponse>(blogJson);
// Then I do the same so get the comments:
var commentsExpiration = new TimeSpan(0, 0, 30);
var commentsCacheKey = GenerateUniqueCacheKey<CommentsRequest>(request);
var commentsResponse = base.RequestContext.ToOptimizedResultUsingCache<CommentsResponse>(base.CacheClient, commentsCacheKey, commentsExpiration, () =>
_client.Execute((request)));
// And decompress again as above
string commentsJson = StreamExtensions.Decompress(((CompressedResult)commentsResponse).Contents, CompressionTypes.Default);
var commentsResponse = ServiceStack.Text.StringExtensions.FromJson<CommentsResponse>(commentsJson);
// The reason for the decompression becomes clear here as I need to attach my Comments only my domain emtity.
if (commentsResponse != null && commentsResponse.Comments != null)
{
response.Comments = commentsResponse.Comments;
}
我想知道的是有一种更短的方法来执行以下操作:
获取我的数据并将其缓存,将其恢复为我的域实体格式,而无需编写上述所有代码行。我不想经历以下痛苦!:
域实体 => json => 解压缩 => 域实体。
似乎浪费了很多精力。
任何示例代码或指向 ToOptimizedResultUsingCache 更好解释的指针将不胜感激。