Book Image

ServiceStack 4 Cookbook

Book Image

ServiceStack 4 Cookbook

Overview of this book

Table of Contents (18 chapters)
ServiceStack 4 Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Using Redis as a cache with the ServiceStack client


In the previous recipe, we focused on using low-level Redis features directly to hand roll our own caching functions. While this is useful, we can also let ServiceStack's extension methods on the Service class do some of the work for us.

Getting ready

ServiceStack's Service class provides a method named ToOptimizedResultUsingCache(), which can help. The way it works is that we'll pass in an implementation of ICacheClient, a string that represents a cache key, and a function that knows how to generate our response in the event that there isn't a value at the location in the cache at that cache key.

This functionality can improve performance for expensive queries that don't need to be performed every time the service is hit. A common example would be a service that runs a query to give back statistics that require a large number of joins across database tables. This query might be calculating yesterday's statistics—something that isn't going...