diff options
| author | s-ol <s+removethis@s-ol.nu> | 2021-03-03 23:35:14 +0000 |
|---|---|---|
| committer | s-ol <s+removethis@s-ol.nu> | 2021-03-03 23:35:14 +0000 |
| commit | 0cc0b9ddeceb4a0adde48e94ce79b4b2b492a252 (patch) | |
| tree | 2e3fa1a96003cd0d38b04ba8ea924418fd314412 | |
| parent | add luaposix dependency (diff) | |
| download | mmm-0cc0b9ddeceb4a0adde48e94ce79b4b2b492a252.tar.gz mmm-0cc0b9ddeceb4a0adde48e94ce79b4b2b492a252.zip | |
basic caching for fileder tree and conversion results
| -rw-r--r-- | Dockerfile | 3 | ||||
| -rw-r--r-- | build/server.moon | 11 | ||||
| -rw-r--r-- | mmm/mmmfs/cache.moon | 22 | ||||
| -rw-r--r-- | mmm/mmmfs/fileder.moon | 4 |
4 files changed, 37 insertions, 3 deletions
@@ -18,4 +18,5 @@ WORKDIR /code RUN tup init && tup generate build-static.sh && ./build-static.sh EXPOSE 8000 -ENTRYPOINT ["moon", "build/server.moon", "fs", "0.0.0.0", "8000"] +ENTRYPOINT ["moon", "build/server.moon"] +CMD ["fs", "0.0.0.0", "8000", "--cache", "--no-rw"] diff --git a/build/server.moon b/build/server.moon index d14b0b4..46ea737 100644 --- a/build/server.moon +++ b/build/server.moon @@ -17,6 +17,7 @@ import convert, MermaidDebugger from require 'mmm.mmmfs.conversion' import get_store from require 'mmm.mmmfs.stores' import render from require 'mmm.mmmfs.layout' import Browser from require 'mmm.mmmfs.browser' +import init_cache from require 'mmm.mmmfs.cache' import decodeURI from require 'http.util' lfs = require 'lfs' @@ -41,6 +42,11 @@ class Server if @flags.unsafe == nil @flags.unsafe = not @flags.rw or opts.host == 'localhost' or opts.host == '127.0.0.1' + if @flags.cache + assert not @flags.rw, "--rw and --cache are incompatible" + @root = Fileder @store + init_cache! + -- @TODO: fix UNSAFE! UNSAFE = @flags.unsafe @@ -55,7 +61,7 @@ class Server assert @server\loop! handle_interactive: (fileder, facet) => - root = Fileder @store + root = @root or Fileder @store browser = Browser root, fileder.path, facet.name render browser\todom!, fileder, noview: true, scripts: " @@ -93,7 +99,7 @@ class Server switch method when 'GET', 'HEAD' - root = Fileder @store + root = @root or Fileder @store export BROWSER BROWSER = :path fileder = root\walk path @@ -217,6 +223,7 @@ class Server -- * FLAGS - any of the following: -- --[no-]rw - enable/disable POST?PUT/DELETE operations (default: on if local) -- --[no-]unsafe - enable/disable server-side code execution when writable is on (default: on if local or --no-rw) +-- --[no-]cache - cache all fileder contents (default: off) -- * STORE - see mmm/mmmfs/stores/init.moon:get_store -- * host - interface to bind to (default localhost, set to 0.0.0.0 for public hosting) -- * port - port to serve from, default 8000 diff --git a/mmm/mmmfs/cache.moon b/mmm/mmmfs/cache.moon new file mode 100644 index 0000000..96c2ece --- /dev/null +++ b/mmm/mmmfs/cache.moon @@ -0,0 +1,22 @@ +class Cache + new: => + @cache = {} + + get: (fileder, key) => + key = tostring key + @cache[fileder.path] or= {} + @cache[fileder.path][key] + + set: (fileder, key, val) => + key = tostring key + @cache[fileder.path] or= {} + @cache[fileder.path][key] = val + + +init_cache = -> + export CACHE + CACHE = Cache! + +{ + :init_cache +} diff --git a/mmm/mmmfs/fileder.moon b/mmm/mmmfs/fileder.moon index 6ce8e91..cd50a40 100644 --- a/mmm/mmmfs/fileder.moon +++ b/mmm/mmmfs/fileder.moon @@ -276,11 +276,15 @@ class Fileder if val = @facets[want] return val, want + if val = CACHE and CACHE\get @, want + return val, want + -- find matching key and shortest conversion path key, conversions = @find want if key value = apply_conversions @, conversions, @facets[key], key + CACHE\set @, want, value if CACHE value, key -- like @get, throw if no value or conversion path |
