2

I am currently using Service stack ICacheClient to cache in memory.

Note: the code below is some what pseudo code as I needed to remove customer specific names.

Lets say I have the following aggregate:

BlogPost => Comments

I want to do this following:

// 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;
}

What I want to know is there a shorter way to do the follow:

Get my data and cache it, get it back into my domain entity format without having to write all the above lines of code. I dont want to go through the following pain!:

Domain entity => json => decompress => domain entity.

Seems like a lot of wasted energy.

Any sample code or pointers to a better explanation of ToOptimizedResultUsingCache would be much appreciated.

RuSs
  • 1,725
  • 1
  • 29
  • 47

1 Answers1

4

Ok so im going to answer my own question. It seems that methods (extension methods) like ToOptimizedResult and ToOptimizedResultUsingCache are there to give you stuff like compression and caching for free.

But, if you want more control you just use the cache as you would normally:

// 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);
}

After you build up you aggregate and put it into cache you can compress the whole thing:

return base.RequestContext.ToOptimizedResult(applesResponse);

If you want to compress globally you can follow this post: Enable gzip/deflate compression

Hope this makes sense.

RuSs

Community
  • 1
  • 1
RuSs
  • 1,725
  • 1
  • 29
  • 47