Skip to content

Instantly share code, notes, and snippets.

@pubudu91
Created May 1, 2018 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pubudu91/fe4c7db7e632a3b301992f477f60b7d5 to your computer and use it in GitHub Desktop.
Save pubudu91/fe4c7db7e632a3b301992f477f60b7d5 to your computer and use it in GitHub Desktop.
import ballerina/http;
import ballerina/log;
endpoint http:Client cachingEP {
url: "http://localhost:8080",
cache: { isShared: true }
};
@http:ServiceConfig { basePath: "/cache" }
service<http:Service> cachingService bind { port: 9090 } {
@http:ResourceConfig {
path: "/"
}
cacheableResource(endpoint caller, http:Request req) {
var response = cachingEP->forward("/hello", req);
match response {
http:Response res => {
_ = caller->respond(res);
}
error err => {
http:Response res = new;
res.statusCode = 500;
res.setPayload(err.message);
_ = caller->respond(res);
}
}
}
}
// Sample backend service which serves cacheable responses.
@http:ServiceConfig { basePath: "/hello" }
service<http:Service> helloWorld bind { port: 8080 } {
json payload = { "message": "Hello, World!" };
@http:ResourceConfig { path: "/" }
sayHello(endpoint caller, http:Request req) {
http:Response res = new;
http:ResponseCacheControl resCC = new;
resCC.maxAge = 15;
resCC.mustRevalidate = true;
resCC.isPrivate = false;
res.cacheControl = resCC;
res.setETag(payload);
res.setLastModified();
res.setPayload(payload);
_ = caller->respond(res);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment