Server-Side Caching in Genie
Web apps often access the server to get new data. This is expensive tasks and loads server since it requires more power to produce continous results
After first request and sending response, servers can save response and next time you hit server with same request the saved copy of response is shared
Important: PUT, DELETE and POST methods should never be cached.
julia> all(Package)
7411-element Vector{Package}:
Package
| KEY | VALUE |
|----------------------|--------------------------------------|
| id::SearchLight.DbId | 1 |
| name::String | COSMA_jll |
| uuid::String | 0efae8bf-39e6-5d65-b05d-c8947f4cee2a |
Package
| KEY | VALUE |
|----------------------|--------------------------------------|
| id::SearchLight.DbId | 2 |
| name::String | CitableImage |
| uuid::String | 17ccb2e5-db19-44b3-b354-4fd16d92c74e |
⋮
Package
| KEY | VALUE |
|----------------------|--------------------------------------|
| id::SearchLight.DbId | 7402 |
| name::String | TreeSitterHighlight |
| uuid::String | d012cbc0-9be0-4aad-9a84-80c4f029fdfc |
# prev code ...
using Genie.Cache
using Packages
function packagenames()
withcache(:packagenames, 24 * 60 * 60) do # 24 hours
pkgnames = [pkgname.name for pkgname in all(Package)]
"const packageList = $pkgnames" |> js
end
end
# further code ...
# routes.js
# prev code ...
route("/assets/js/packages.js", PackagesController.packagenames, method = GET)
packages.js
now contains and is cached for 24 hrs
const packageList = ["TreeSitterHighlight", "Genie", .... , "Stipple"]
Now you can serve the endpoint in app.jl.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Package download stats for Julia</title>
<script src="/assets/js/packages.js"></script> <!-- cached for 24 hours -->
<link rel="stylesheet" href="/css/app.css" />
<!-- more code -->
</html>
Consecutive calls will get the cached response body for the next 24 hrs and, of course, it will not have to wait those longer seconds anymore and stress server for data that might not possibly have changed