1 # Build Cache wrapper for minifying SVG files
7 The `min_svg_cache` package exports a single constructor
9 which must be called with the `new` operator. The resulting cache object stores
10 the `utf-8`-encoded text of SVG files loaded from disk and minified.
12 See the `build_cache`, `disk_build`, and `svgo` packages for more information.
13 The `MinSVGCache` object is essentially a wrapper object which routes the
14 request between these packages, to ensure that the minified SVG text is either
15 retrieved from RAM or minified from a source file as required.
19 Suppose one has a `MinSVGCache` instance named `msc`. It behaves somewhat like
20 an ES6 `Map` object, except that it only has the `msc.get()` function, because
21 new objects are added to the cache by attempting to `get` them.
23 The interface for the `MinSVGCache`-provided instance function `msc.get()` is:
25 `await msc.get(key)` — retrieves the object stored under `key`, where
26 `key` is the on-disk pathname to a SVG file. A `Buffer` object is returned,
27 containing the `utf-8`-encoded text resulting from running `svgo` on the
28 source file. The pathname and the returned result are cached for future reuse.
30 Before returning the cached copy, the existence and modification time of the
31 SVG file on disk is checked to make sure that the cache is up-to-date.
32 Otherwise, if the file doesn't exist an `ENOENT` exception is thrown, or if the
33 file exists it is loaded and minified and the cache updated for next time. The
34 minification is via disk, and skipped if an up-to-date disk result is present.
38 The `diag` argument to the constructor is a `bool`, which if `true` causes
39 messages to be printed via `console.log()` for all activities except for the
40 common case of retrieval when the object is already up-to-date. A `diag` value
41 of `undefined` is treated as `false`, thus it can be omitted in the usual case.
45 The `svgo` package is called with standard NDCODE options. This is part of the
46 reason to have `min_svg_cache`, so as to provide a consistent interface to
47 minifiers for JavaScript, CSS, SVG and so on. However, if some control of the
48 underlying minifier is needed in future, we could add an `options` object to
49 pass through, in which we'd inject our NDCODE options where not overridden.
51 It is intended that we will shortly add a timer function (or possibly just a
52 function that the user should call periodically) to flush built templates from
53 the cache after a stale time. There is otherwise no way to delete an object
54 from the cache, except by first deleting it on disk, then trying to `get` it.
58 The development version can be cloned, downloaded, or browsed with `gitweb` at:
59 https://git.ndcode.org/public/min_svg_cache.git
63 All of our NPM packages are MIT licensed, please see LICENSE in the repository.
67 The caching system is under active development (and is part of a larger project
68 that is also under development) and thus the API is tentative. Please go ahead
69 and incorporate the system into your project, or try out our example webserver
70 built on the system, subject to the caution that the API could change. Please
71 send us your experience and feedback, and let us know of improvements you make.
73 Contact: Nick Downing <nick@ndcode.org>