servicestack,Caching,servicestack" /> servicestack,Caching,servicestack" />

Caching 使用服务堆栈缓存数据聚合以优化ResultUsingCache

Caching 使用服务堆栈缓存数据聚合以优化ResultUsingCache,caching,servicestack,Caching,servicestack,我目前正在使用服务堆栈ICacheClient在内存中缓存 注意:下面的代码是我删除客户特定名称所需的一些伪代码 假设我有以下汇总: 博客帖子 =>评论 我想做以下几点: // So I need to go get the blogPost and cache it: var blogPostExpiration = new TimeSpan(0, 0, 30); var blogPostCacheKey = GenerateUniqueCacheKey<BlogPostRequest&

我目前正在使用服务堆栈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;
}
//所以我需要去获取blogPost并缓存它:
var blogPostExpiration=新的时间跨度(0,0,30);
var blogpostcheKey=GenerateUniqueCacheKey(请求);
blogPostResponse=base.RequestContext.ToOptimizedResultUsingCache(base.CacheClient、blogPostCacheKey、blogPostExpiration,()=>
_执行((请求));
//然后,令人烦恼的是,我需要将其解压缩为json,以便将响应返回到我的域实体结构:BlogPostResponse
字符串blogJson=streamxtensions.decompression(((CompressedResult)blogpostress.Contents,CompressionTypes.Default);
response=ServiceStack.Text.StringExtensions.FromJson(blogJson);
//然后我也会这样做,以便得到评论:
var CommentsPrivation=新的时间跨度(0,0,30);
var commentsCacheKey=GenerateUniqueCacheKey(请求);
var commentsResponse=base.RequestContext.TooOptimizedResultusingCache(base.CacheClient,commentsCacheKey,CommentsPrivation,()=>
_执行((请求));
//然后像上面一样再次解压
string commentsJson=streamxtensions.decompression((CompressedResult)commentsResponse.Contents,CompressionTypes.Default);
var commentsResponse=ServiceStack.Text.StringExtensions.FromJson(commentsJson);
//解压的原因在这里变得很清楚,因为我只需要附上我的评论和我的域名。
if(commentsResponse!=null&&commentsResponse.Comments!=null)
{
response.Comments=评论response.Comments;
}
我想知道的是,有一种较短的方法可以完成以下任务:

获取我的数据并将其缓存,将其恢复为我的域实体格式,而无需编写上述所有代码行。我不想经历以下痛苦!:

域实体=>json=>解压缩=>域实体。

好像浪费了很多能量


如果有任何示例代码或指向ToOptimizedResultUsingCache的更好解释的指针,我们将不胜感激。

好的,我将回答我自己的问题。看起来像ToOptimizedResult和ToOptimizedResultUsingCache这样的方法(扩展方法)可以免费提供压缩和缓存之类的东西

但是,如果您想要更多的控制,您只需像平常一样使用缓存:

// Generate cache key
var applesCacheKey = GenerateUniqueCacheKey<ApplesRequest>(request);

// attempt to get match details from cache
applesResponse = CacheClient.Get<ApplesDetailResponse>(applesDetailCacheKey);

// if there was nothing in cache then
if (applesResponse == null)
{
    // Get data from storage
    applesResponse = _client.Execute(request);

    // Add the data to cache
    CacheClient.Add(applesCacheKey, applesResponse, applesExpiration);
}
如果您想进行全局压缩,可以阅读以下文章:

希望这是有意义的


罗斯

好的,我来回答我自己的问题。看起来像ToOptimizedResult和ToOptimizedResultUsingCache这样的方法(扩展方法)可以免费提供压缩和缓存之类的东西

但是,如果您想要更多的控制,您只需像平常一样使用缓存:

// Generate cache key
var applesCacheKey = GenerateUniqueCacheKey<ApplesRequest>(request);

// attempt to get match details from cache
applesResponse = CacheClient.Get<ApplesDetailResponse>(applesDetailCacheKey);

// if there was nothing in cache then
if (applesResponse == null)
{
    // Get data from storage
    applesResponse = _client.Execute(request);

    // Add the data to cache
    CacheClient.Add(applesCacheKey, applesResponse, applesExpiration);
}
如果您想进行全局压缩,可以阅读以下文章:

希望这是有意义的

罗斯