From 1418e5d760a093c525c78cdd8dfc74d59825210f Mon Sep 17 00:00:00 2001 From: Rouven Hi Date: Sun, 13 Feb 2022 20:11:00 +0100 Subject: [PATCH] feat: added request cache --- Cargo.lock | 501 ++++++++++++++++++++++++++++++++++-- Cargo.toml | 1 + src/image_processor_test.rs | 2 +- src/main.rs | 5 +- src/resource_endpoint.rs | 46 +++- src/scheduler.rs | 2 +- 6 files changed, 528 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d1e483e..a83ef31 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -69,7 +69,7 @@ dependencies = [ "actix-service", "actix-threadpool", "actix-utils", - "base64", + "base64 0.13.0", "bitflags", "brotli2", "bytes 0.5.6", @@ -99,7 +99,7 @@ dependencies = [ "serde", "serde_json", "serde_urlencoded", - "sha-1", + "sha-1 0.9.8", "slab", "time 0.2.27", ] @@ -322,6 +322,135 @@ dependencies = [ "num-traits", ] +[[package]] +name = "async-channel" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2114d64672151c0c5eaa5e131ec84a74f06e1e559830dabba01ca30605d66319" +dependencies = [ + "concurrent-queue", + "event-listener", + "futures-core", +] + +[[package]] +name = "async-executor" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "871f9bb5e0a22eeb7e8cf16641feb87c9dc67032ccf8ff49e772eb9941d3a965" +dependencies = [ + "async-task", + "concurrent-queue", + "fastrand", + "futures-lite", + "once_cell", + "slab", +] + +[[package]] +name = "async-global-executor" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9586ec52317f36de58453159d48351bc244bc24ced3effc1fce22f3d48664af6" +dependencies = [ + "async-channel", + "async-executor", + "async-io", + "async-mutex", + "blocking", + "futures-lite", + "num_cpus", + "once_cell", +] + +[[package]] +name = "async-io" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a811e6a479f2439f0c04038796b5cfb3d2ad56c230e0f2d3f7b04d68cfee607b" +dependencies = [ + "concurrent-queue", + "futures-lite", + "libc", + "log", + "once_cell", + "parking", + "polling", + "slab", + "socket2 0.4.2", + "waker-fn", + "winapi 0.3.9", +] + +[[package]] +name = "async-lock" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6a8ea61bf9947a1007c5cada31e647dbc77b103c679858150003ba697ea798b" +dependencies = [ + "event-listener", +] + +[[package]] +name = "async-mutex" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "479db852db25d9dbf6204e6cb6253698f175c15726470f78af0d918e99d6156e" +dependencies = [ + "event-listener", +] + +[[package]] +name = "async-process" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83137067e3a2a6a06d67168e49e68a0957d215410473a740cea95a2425c0b7c6" +dependencies = [ + "async-io", + "blocking", + "cfg-if 1.0.0", + "event-listener", + "futures-lite", + "libc", + "once_cell", + "signal-hook", + "winapi 0.3.9", +] + +[[package]] +name = "async-std" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8056f1455169ab86dd47b47391e4ab0cbd25410a70e9fe675544f49bafaf952" +dependencies = [ + "async-channel", + "async-global-executor", + "async-io", + "async-lock", + "async-process", + "crossbeam-utils", + "futures-channel", + "futures-core", + "futures-io", + "futures-lite", + "gloo-timers", + "kv-log-macro", + "log", + "memchr", + "num_cpus", + "once_cell", + "pin-project-lite 0.2.8", + "pin-utils", + "slab", + "wasm-bindgen-futures", +] + +[[package]] +name = "async-task" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d306121baf53310a3fd342d88dc0824f6bbeace68347593658525565abee8" + [[package]] name = "async-trait" version = "0.1.52" @@ -333,6 +462,12 @@ dependencies = [ "syn", ] +[[package]] +name = "atomic-waker" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "065374052e7df7ee4047b1160cca5e1467a12351a40b3da123c870ba0b8eda2a" + [[package]] name = "autocfg" version = "1.0.1" @@ -349,7 +484,7 @@ dependencies = [ "actix-http", "actix-rt", "actix-service", - "base64", + "base64 0.13.0", "bytes 0.5.6", "cfg-if 1.0.0", "derive_more", @@ -369,6 +504,15 @@ version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4521f3e3d031370679b3b140beb36dfe4801b09ac77e30c61941f97df3ef28b" +[[package]] +name = "base64" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b25d992356d2eb0ed82172f5248873db5560c4721f564b13cb5193bda5e668e" +dependencies = [ + "byteorder", +] + [[package]] name = "base64" version = "0.13.0" @@ -381,13 +525,48 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "block-buffer" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0940dc441f31689269e10ac70eb1002a3a1d3ad1390e030043662eb7fe4688b" +dependencies = [ + "block-padding", + "byte-tools", + "byteorder", + "generic-array 0.12.4", +] + [[package]] name = "block-buffer" version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" dependencies = [ - "generic-array", + "generic-array 0.14.5", +] + +[[package]] +name = "block-padding" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa79dedbb091f449f1f39e53edf88d5dbe95f895dae6135a8d7b881fb5af73f5" +dependencies = [ + "byte-tools", +] + +[[package]] +name = "blocking" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "046e47d4b2d391b1f6f8b407b1deb8dee56c1852ccd868becf2710f601b5f427" +dependencies = [ + "async-channel", + "async-task", + "atomic-waker", + "fastrand", + "futures-lite", + "once_cell", ] [[package]] @@ -425,6 +604,12 @@ version = "3.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4a45a46ab1f2412e53d3a0ade76ffad2025804294569aae387231a0cd6e0899" +[[package]] +name = "byte-tools" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" + [[package]] name = "bytemuck" version = "1.7.3" @@ -458,6 +643,35 @@ dependencies = [ "bytes 1.1.0", ] +[[package]] +name = "cacache" +version = "10.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a4ae3b2af64ab1dc8c05c412bd5e900f0536c1ae6caa7a48171d4726d85dda0" +dependencies = [ + "async-std", + "digest 0.9.0", + "either", + "futures", + "hex 0.4.3", + "memmap2", + "serde", + "serde_derive", + "serde_json", + "sha-1 0.9.8", + "sha2 0.9.9", + "ssri", + "tempfile", + "thiserror", + "walkdir", +] + +[[package]] +name = "cache-padded" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1db59621ec70f09c5e9b597b220c7a2b43611f4710dc03ceb8748637775692c" + [[package]] name = "cc" version = "1.0.72" @@ -505,6 +719,15 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" +[[package]] +name = "concurrent-queue" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30ed07550be01594c6026cff2a1d7fe9c8f683caa798e12b68694ac9e88286a3" +dependencies = [ + "cache-padded", +] + [[package]] name = "const_fn" version = "0.4.9" @@ -651,13 +874,22 @@ version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0e25ea47919b1560c4e3b7fe0aaab9becf5b84a10325ddf7db0f0ba5e1026499" +[[package]] +name = "digest" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3d0c8c8752312f9713efd397ff63acb9f85585afbf179282e720e7704954dd5" +dependencies = [ + "generic-array 0.12.4", +] + [[package]] name = "digest" version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" dependencies = [ - "generic-array", + "generic-array 0.14.5", ] [[package]] @@ -693,6 +925,12 @@ dependencies = [ "syn", ] +[[package]] +name = "event-listener" +version = "2.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77f3309417938f28bf8228fcff79a4a37103981e3e186d2ccd19c74b38f4eb71" + [[package]] name = "evmap" version = "10.0.2" @@ -712,6 +950,12 @@ dependencies = [ "mutate_once", ] +[[package]] +name = "fake-simd" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" + [[package]] name = "fastrand" version = "1.6.0" @@ -788,6 +1032,7 @@ checksum = "28560757fe2bb34e79f907794bb6b22ae8b0e5c669b638a1132f2592b19035b4" dependencies = [ "futures-channel", "futures-core", + "futures-executor", "futures-io", "futures-sink", "futures-task", @@ -810,12 +1055,38 @@ version = "0.3.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0c8ff0461b82559810cdccfde3215c3f373807f5e5232b71479bff7bb2583d7" +[[package]] +name = "futures-executor" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29d6d2ff5bb10fb95c85b8ce46538a2e5f5e7fdc755623a7d4529ab8a4ed9d2a" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + [[package]] name = "futures-io" version = "0.3.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1f9d34af5a1aac6fb380f735fe510746c38067c5bf16c7fd250280503c971b2" +[[package]] +name = "futures-lite" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7694489acd39452c77daa48516b894c153f192c3578d5a839b62c58099fcbf48" +dependencies = [ + "fastrand", + "futures-core", + "futures-io", + "memchr", + "parking", + "pin-project-lite 0.2.8", + "waker-fn", +] + [[package]] name = "futures-macro" version = "0.3.19" @@ -866,6 +1137,15 @@ dependencies = [ "byteorder", ] +[[package]] +name = "generic-array" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffdf9f34f1447443d37393cc6c2b8313aebddcd96906caf34e54c68d8e57d7bd" +dependencies = [ + "typenum", +] + [[package]] name = "generic-array" version = "0.14.5" @@ -897,6 +1177,18 @@ dependencies = [ "weezl", ] +[[package]] +name = "gloo-timers" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d12a7f4e95cfe710f1d624fb1210b7d961a5fb05c4fd942f4feab06e61f590e" +dependencies = [ + "futures-channel", + "futures-core", + "js-sys", + "wasm-bindgen", +] + [[package]] name = "h2" version = "0.2.7" @@ -931,7 +1223,7 @@ dependencies = [ "http", "indexmap", "slab", - "tokio 1.15.0", + "tokio 1.16.1", "tokio-util 0.6.9", "tracing", ] @@ -966,6 +1258,18 @@ dependencies = [ "libc", ] +[[package]] +name = "hex" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "805026a5d0141ffc30abb3be3173848ad46a1b1664fe632428479619a3644d77" + +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + [[package]] name = "hostname" version = "0.3.1" @@ -1029,7 +1333,7 @@ dependencies = [ "itoa 0.4.8", "pin-project-lite 0.2.8", "socket2 0.4.2", - "tokio 1.15.0", + "tokio 1.16.1", "tower-service", "tracing", "want", @@ -1044,7 +1348,7 @@ dependencies = [ "bytes 1.1.0", "hyper", "native-tls", - "tokio 1.15.0", + "tokio 1.16.1", "tokio-native-tls", ] @@ -1164,6 +1468,15 @@ dependencies = [ "winapi-build", ] +[[package]] +name = "kv-log-macro" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0de8b303297635ad57c9f5059fd9cee7a47f8e8daa09df0fcd07dd39fb22977f" +dependencies = [ + "log", +] + [[package]] name = "language-tags" version = "0.2.2" @@ -1204,6 +1517,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" dependencies = [ "cfg-if 1.0.0", + "value-bag", ] [[package]] @@ -1239,6 +1553,15 @@ version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" +[[package]] +name = "memmap2" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "057a3db23999c867821a7a59feb06a578fcb03685e983dff90daf9e7d24ac08f" +dependencies = [ + "libc", +] + [[package]] name = "memoffset" version = "0.6.5" @@ -1467,6 +1790,12 @@ version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da32515d9f6e6e489d7bc9d84c71b060db7247dc035bbe44eac88cf87486d8d5" +[[package]] +name = "opaque-debug" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c" + [[package]] name = "opaque-debug" version = "0.3.0" @@ -1515,6 +1844,12 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "parking" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "427c3892f9e783d91cc128285287e70a59e206ca452770ece88a76f7a3eddd72" + [[package]] name = "parking_lot" version = "0.11.2" @@ -1622,6 +1957,19 @@ dependencies = [ "miniz_oxide 0.3.7", ] +[[package]] +name = "polling" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "685404d509889fade3e86fe3a5803bca2ec09b0c0778d5ada6ec8bf7a8de5259" +dependencies = [ + "cfg-if 1.0.0", + "libc", + "log", + "wepoll-ffi", + "winapi 0.3.9", +] + [[package]] name = "ppv-lite86" version = "0.2.16" @@ -1777,7 +2125,7 @@ version = "0.11.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7c4e0a76dc12a116108933f6301b95e83634e0c47b0afbed6abbaa0601e99258" dependencies = [ - "base64", + "base64 0.13.0", "bytes 1.1.0", "encoding_rs", "futures-core", @@ -1797,7 +2145,7 @@ dependencies = [ "serde", "serde_json", "serde_urlencoded", - "tokio 1.15.0", + "tokio 1.16.1", "tokio-native-tls", "url", "wasm-bindgen", @@ -1849,6 +2197,15 @@ version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f" +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + [[package]] name = "schannel" version = "0.1.19" @@ -1958,17 +2315,29 @@ dependencies = [ "serde", ] +[[package]] +name = "sha-1" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7d94d0bede923b3cea61f3f1ff57ff8cdfd77b400fb8f9998949e0cf04163df" +dependencies = [ + "block-buffer 0.7.3", + "digest 0.8.1", + "fake-simd", + "opaque-debug 0.2.3", +] + [[package]] name = "sha-1" version = "0.9.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "99cd6713db3cf16b6c84e06321e049a9b9f699826e16096d23bbcc44d15d51a6" dependencies = [ - "block-buffer", + "block-buffer 0.9.0", "cfg-if 1.0.0", "cpufeatures", - "digest", - "opaque-debug", + "digest 0.9.0", + "opaque-debug 0.3.0", ] [[package]] @@ -1977,6 +2346,41 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2579985fda508104f7587689507983eadd6a6e84dd35d6d115361f530916fa0d" +[[package]] +name = "sha2" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a256f46ea78a0c0d9ff00077504903ac881a1dafdc20da66545699e7776b3e69" +dependencies = [ + "block-buffer 0.7.3", + "digest 0.8.1", + "fake-simd", + "opaque-debug 0.2.3", +] + +[[package]] +name = "sha2" +version = "0.9.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d58a1e1bf39749807d89cf2d98ac2dfa0ff1cb3faa38fbb64dd88ac8013d800" +dependencies = [ + "block-buffer 0.9.0", + "cfg-if 1.0.0", + "cpufeatures", + "digest 0.9.0", + "opaque-debug 0.3.0", +] + +[[package]] +name = "signal-hook" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "647c97df271007dcea485bb74ffdb57f2e683f1306c854f468a0c244badabf2d" +dependencies = [ + "libc", + "signal-hook-registry", +] + [[package]] name = "signal-hook-registry" version = "1.4.0" @@ -2019,6 +2423,21 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "ssri" +version = "7.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9cec0d388f39fbe79d7aa600e8d38053bf97b1bc8d350da7c0ba800d0f423f2" +dependencies = [ + "base64 0.10.1", + "digest 0.8.1", + "hex 0.3.2", + "serde", + "sha-1 0.8.2", + "sha2 0.8.2", + "thiserror", +] + [[package]] name = "standback" version = "0.2.17" @@ -2110,7 +2529,8 @@ dependencies = [ "actix-http", "actix-web", "assertor", - "base64", + "base64 0.13.0", + "cacache", "chrono", "clokwerk", "evmap", @@ -2254,9 +2674,9 @@ dependencies = [ [[package]] name = "tokio" -version = "1.15.0" +version = "1.16.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbbf1c778ec206785635ce8ad57fe52b3009ae9e0c9f574a728f3049d3e55838" +checksum = "0c27a64b625de6d309e8c57716ba93021dccf1b3b5c97edd6d3dd2d2135afc0a" dependencies = [ "bytes 1.1.0", "libc", @@ -2274,7 +2694,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7d995660bd2b7f8c1568414c1126076c13fbb725c40112dc0120b78eb9b717b" dependencies = [ "native-tls", - "tokio 1.15.0", + "tokio 1.16.1", ] [[package]] @@ -2302,7 +2722,7 @@ dependencies = [ "futures-sink", "log", "pin-project-lite 0.2.8", - "tokio 1.15.0", + "tokio 1.16.1", ] [[package]] @@ -2473,6 +2893,16 @@ dependencies = [ "v_escape", ] +[[package]] +name = "value-bag" +version = "1.0.0-alpha.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79923f7731dc61ebfba3633098bf3ac533bbd35ccd8c57e7088d9a5eebe0263f" +dependencies = [ + "ctor", + "version_check 0.9.4", +] + [[package]] name = "vcpkg" version = "0.2.15" @@ -2491,6 +2921,23 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +[[package]] +name = "waker-fn" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d5b2c62b4012a3e1eca5a7e077d13b3bf498c4073e33ccd58626607748ceeca" + +[[package]] +name = "walkdir" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56" +dependencies = [ + "same-file", + "winapi 0.3.9", + "winapi-util", +] + [[package]] name = "want" version = "0.3.0" @@ -2595,6 +3042,15 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d8b77fdfd5a253be4ab714e4ffa3c49caf146b4de743e97510c0656cf90f1e8e" +[[package]] +name = "wepoll-ffi" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d743fdedc5c64377b5fc2bc036b01c7fd642205a0d96356034ae3404d49eb7fb" +dependencies = [ + "cc", +] + [[package]] name = "widestring" version = "0.4.3" @@ -2629,6 +3085,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi 0.3.9", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 65a1136..0f08f06 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ rayon = "1.5" regex = "1.5" base64 = "0.13" image = "0.23" +cacache = "10" [dev-dependencies] assertor = "0.0" diff --git a/src/image_processor_test.rs b/src/image_processor_test.rs index c0d777e..3e345c0 100644 --- a/src/image_processor_test.rs +++ b/src/image_processor_test.rs @@ -18,7 +18,7 @@ fn scale_image() { image_data, 1024, 786, - None + None, ); // THEN the resolved city name should be Koblenz diff --git a/src/main.rs b/src/main.rs index c076139..ed80468 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,7 +22,6 @@ mod image_processor_test; #[actix_web::main] async fn main() -> std::io::Result<()> { // Build webdav client - let web_dav_client = web_dav_client::new( env::var("TWIP_WEBDAV_BASE_URL").expect("TWIP_WEBDAV_BASE_URL is missing").as_str(), env::var("TWIP_USERNAME").expect("TWIP_USERNAME is missing").as_str(), @@ -31,11 +30,11 @@ async fn main() -> std::io::Result<()> { // Initialize kv_store reader and writer let (kv_reader, kv_writer) = evmap::new::(); - // Build arc mutex of kv_store writer (we have multiple writer) + // Build arc mutex of kv_store writer, we need this exact instance (cause, we have multiple writer) let kv_writer_mutex = Arc::new(Mutex::new(kv_writer)); // Start scheduler to run at midnight - let scheduler_handle = scheduler::initialize( + let scheduler_handle = scheduler::run_webdav_indexer( web_dav_client.clone(), kv_writer_mutex.clone(), ); diff --git a/src/resource_endpoint.rs b/src/resource_endpoint.rs index 69fb8f2..1ebf936 100644 --- a/src/resource_endpoint.rs +++ b/src/resource_endpoint.rs @@ -4,6 +4,8 @@ use evmap::ReadHandle; use crate::{get, image_processor, resource_processor, WebDavClient, WebDavResource}; +const CACHE_DIR: &'static str = "./cache"; + #[get("")] pub async fn list_resources(kv_reader: web::Data>) -> HttpResponse { let keys: Vec = resource_processor::get_all(kv_reader.as_ref()); @@ -23,7 +25,11 @@ pub async fn list_this_week_resources(kv_reader: web::Data, kv_reader: web::Data>, web_dav_client: web::Data) -> HttpResponse { +pub async fn get_resource( + resources_id: web::Path<(String, u32, u32)>, + kv_reader: web::Data>, + web_dav_client: web::Data, +) -> HttpResponse { let path_params = resources_id.0; let resource_id = path_params.0.as_str(); let display_width = path_params.1; @@ -54,14 +60,30 @@ pub async fn get_resource(resources_id: web::Path<(String, u32, u32)>, kv_reader } #[get("{resource_id}/{display_width}/{display_height}/base64")] -pub async fn get_resource_base64(resources_id: web::Path<(String, u32, u32)>, kv_reader: web::Data>, web_dav_client: web::Data) -> HttpResponse { - // TODO request caching - +pub async fn get_resource_base64( + resources_id: web::Path<(String, u32, u32)>, + kv_reader: web::Data>, + web_dav_client: web::Data, +) -> HttpResponse { let path_params = resources_id.0; let resource_id = path_params.0.as_str(); let display_width = path_params.1; let display_height = path_params.2; + // check cache + let cached_data = cacache::read( + CACHE_DIR, + format!("{resource_id}_{display_width}_{display_height}"), + ); + if let Ok(cached_data) = cached_data.await { + println!(" #### Cache hit! {}", format!("{resource_id}_{display_width}_{display_height}")); + return HttpResponse::Ok() + .content_type("plain/text") + .body(cached_data); + } + println!(" #### Cache miss! {}", format!("{resource_id}_{display_width}_{display_height}")); + + // Read image from webdav let web_dav_resource = kv_reader.get_one(resource_id) .map(|value| value.to_string()) .and_then(|resource_json_string| serde_json::from_str(resource_json_string.as_str()).ok()); @@ -75,6 +97,12 @@ pub async fn get_resource_base64(resources_id: web::Path<(String, u32, u32)>, kv .map(|base64_string| format!("data:image/png;base64,{}", base64_string)); if let Some(base64_image) = base64_image { + cacache::write( + CACHE_DIR, + format!("{resource_id}_{display_width}_{display_height}"), + base64_image.as_bytes(), + ).await.unwrap(); + HttpResponse::Ok() .content_type("plain/text") .body(base64_image) @@ -84,7 +112,10 @@ pub async fn get_resource_base64(resources_id: web::Path<(String, u32, u32)>, kv } #[get("{resource_id}/metadata")] -pub async fn get_resource_metadata(resources_id: web::Path, kv_reader: web::Data>) -> HttpResponse { +pub async fn get_resource_metadata( + resources_id: web::Path, + kv_reader: web::Data>, +) -> HttpResponse { let metadata = kv_reader.get_one(resources_id.as_str()) .map(|value| value.to_string()); @@ -98,7 +129,10 @@ pub async fn get_resource_metadata(resources_id: web::Path, kv_reader: w } #[get("{resource_id}/description")] -pub async fn get_resource_metadata_description(resources_id: web::Path, kv_reader: web::Data>) -> HttpResponse { +pub async fn get_resource_metadata_description( + resources_id: web::Path, + kv_reader: web::Data>, +) -> HttpResponse { let display_value = kv_reader.get_one(resources_id.as_str()) .map(|value| value.to_string()) .and_then(|resource_json_string| serde_json::from_str(resource_json_string.as_str()).ok()) diff --git a/src/scheduler.rs b/src/scheduler.rs index 7378bd4..398401c 100644 --- a/src/scheduler.rs +++ b/src/scheduler.rs @@ -6,7 +6,7 @@ use evmap::WriteHandle; use crate::web_dav_client::WebDavClient; -pub fn initialize(web_dav_client: WebDavClient, kv_writer_mutex: Arc>>) -> ScheduleHandle { +pub fn run_webdav_indexer(web_dav_client: WebDavClient, kv_writer_mutex: Arc>>) -> ScheduleHandle { let mut scheduler = Scheduler::new(); // Fetch webdav resources at midnight