Add caching with LRU cache under mutex

Caches responses of each GET handler in a separate capacity-limited cache (as a
custom clone-able `CachedResponse` struct). Subsequent requests will build a
`Response` from the cached bytes instead of re-querying the database and
re-serializing the JSON. This greatly speeds up the list endpoints and
`get_interior_ref_list`.

Also caches the api-key-to-id mapping for `Owner`s in order to speed up frequent
authentications.

Each create handler clears the entire list response cache. Each delete handler
also clears the entire list response cache and deletes the cached response for
that key. Deleting an owner also deletes their entry in the
`owner_ids_by_api_key` cache.
This commit is contained in:
2020-08-01 00:25:04 -04:00
parent 68b04b4f4c
commit 519fcb4c5a
14 changed files with 470 additions and 123 deletions

View File

@@ -45,7 +45,8 @@ pub fn get_shop(env: Environment) -> impl Filter<Extract = impl Reply, Error = R
pub fn create_shop(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::post()
warp::path::end()
.and(warp::post())
.and(json_body::<Shop>())
.and(with_env(env))
.and_then(handlers::create_shop)
@@ -64,7 +65,8 @@ pub fn delete_shop(
pub fn list_shops(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::get()
warp::path::end()
.and(warp::get())
.and(warp::query::<ListParams>())
.and(with_env(env))
.and_then(handlers::list_shops)
@@ -80,7 +82,8 @@ pub fn get_owner(env: Environment) -> impl Filter<Extract = impl Reply, Error =
pub fn create_owner(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::post()
warp::path::end()
.and(warp::post())
.and(json_body::<Owner>())
.and(warp::addr::remote())
.and(with_env(env))
@@ -100,7 +103,8 @@ pub fn delete_owner(
pub fn list_owners(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::get()
warp::path::end()
.and(warp::get())
.and(warp::query::<ListParams>())
.and(with_env(env))
.and_then(handlers::list_owners)
@@ -118,7 +122,8 @@ pub fn get_interior_ref_list(
pub fn create_interior_ref_list(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::post()
warp::path::end()
.and(warp::post())
.and(json_body::<InteriorRefList>())
.and(with_env(env))
.and_then(handlers::create_interior_ref_list)
@@ -137,7 +142,8 @@ pub fn delete_interior_ref_list(
pub fn list_interior_ref_lists(
env: Environment,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::get()
warp::path::end()
.and(warp::get())
.and(warp::query::<ListParams>())
.and(with_env(env))
.and_then(handlers::list_interior_ref_lists)