Created
May 1, 2018 17:10
-
-
Save pubudu91/fe4c7db7e632a3b301992f477f60b7d5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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