Elegancy challenge: Cacheable Batches
Join the DZone community and get the full member experience.
Join For FreeLet us say that we have the following server code:
public JsonDocument[] GetDocument(string[] ids) { var results = new List<JsonDocument>(); foreach(var id in ids) { results.Add(GetDocument(id)); } return result.ToArray(); }
This method is a classic example of batching requests to the server. For the purpose of our discussion, we have a client proxy that looks like this:
public class ClientProxy : IDisposable { MemoryCache cache = new MemoryCache(); public JsonDocument[] GetDocument(params string[] ids) { // make the request to the server, for example var request = WebRequest.Create("http://server/get?id=" + string.Join("&id=", ids)); using(var stream = request.GetResponse().GetResposeStream()) { return GetResults(stream); } } public void Dispose() { cache.Dispose(); } }
Now, as you can probably guess from the title and from the code above, the question relates to caching. We need to make the following pass:
using(var proxy = new ClientProxy()) { proxy.GetPopularity("ayende", "oren"); // nothing in the cahce, make request to server proxy.GetPopulairty("rhino", "hippo"); // nothing in the cahce, make request to server proxy.GetPopulairty("rhino", "aligator"); // only request aligator, 'rhino' is in the cache proxy.GetPopulairty("rhino", "hippo"); // don't make any request, serve from cache proxy.GetPopulairty("rhino", "oren"); // don't make any request, serve from cache }
The tricky part, of course, is to make this elegant. You can modify both server and client code.
I simplified the problem drastically, but one of the major benefits in the real issue was reducing the size of data we have to fetch over the network even for partial cached queries.
Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
What Is Test Pyramid: Getting Started With Test Automation Pyramid
-
Build a Simple Chat Server With gRPC in .Net Core
-
Competing Consumers With Spring Boot and Hazelcast
-
Auto-Scaling Kinesis Data Streams Applications on Kubernetes
Comments