Cachier

Cachier

The default persistence cache manager that uses a simple object mapping for Cachier.read/Cachier.write operations. All caching is maintained in-memory. Template, partial and context Cachier.read operations use either window.fetch (browsers) or the https module (Node.js) to capture partial template content. Compiled template Cachier.read operations are handled via dynamic import/require (if used). Cachier.write operations are typically written to memory, but can also be configured to POST template, partials and/or context over HTTP/S.

Constructor

new Cachier(optsopt, readFormatteropt, writeFormatteropt, logopt)

Constructor

Source:
Parameters:
Name Type Attributes Description
opts TemplateOpts <optional>

the TemplateOpts

readFormatter function <optional>

The function(string, readFormatOptions) that will return a formatted string for reading data using the options.readFormatOptions from TemplateOpts as the formatting options. Typically reads are for HTML minification and/or beautifying. NOTE: Use with caution as syntax errors may result depending on the formatter used and the complexity of the data being formatted!

writeFormatter function <optional>

The function(string, writeFormatOptions) that will return a formatted string for writting data using the options.writeFormatOptions from TemplateOpts as the formatting options. Typically reads are for JS minification and/or beautifying. NOTE: Use with caution as syntax errors may result depending on the formatter used and the complexity of the data being formatted!

log Object <optional>

The log for handling logging output

Name Type Attributes Description
debug function <optional>

A function that will accept debug level logging messages (i.e. debug('some message to log'))

info function <optional>

A function that will accept info level logging messages (i.e. info('some message to log'))

warn function <optional>

A function that will accept warning level logging messages (i.e. warn('some message to log'))

error function <optional>

A function that will accept error level logging messages (i.e. error('some message to log'))

Members

log

Source:

(protected) metadata

Source:

operations

Source:

options

Source:

readFormatter

Source:

readWriteNames

Source:
See:
  • Cachier.readWriteName for parameter details

writeFormatter

Source:

Methods

(static) contentURL(name, opts) → {Object}

Extracts the proper URL and option name used for a particular template, partial or context

Source:
Parameters:
Name Type Description
name String

The template, partial or context name

opts TemplateOpts

The template options

Returns:
Type:
Object

The extracted content that contains a url property and a optionName that describes the option used for the URL

(async, static) waiter(proms, errMsgopt, captureopt) → {Array|undefined}

Waits for promises to finish and throws commulative errors into a single meaningful stack while continuing to process subsequent promises in the array. When an error is thrown and capture === true, it will also contain a results property that contains either each result from an awaited promise or an error thrown when waiting for the promise to complete.

Source:
Parameters:
Name Type Attributes Default Description
proms Array.<Promise>

The promises to wait for.

errMsg String <optional>
'One or more pomises failed'

The message to use when any errors occur.

capture Boolean <optional>
true

When true each promise results/errors will be captured and returned. Otherwise, nothing will be captured

Returns:
Type:
Array | undefined

When capture === true, the awaited promise results are retuend. When an error occurs, the value at the given index will contain the error instead. When capture !== true nothing is captured/returned.

(async) clear(allopt)

Clears the cache

Source:
Parameters:
Name Type Attributes Default Description
all Boolean <optional>
false

true to clear all unassociated cache instances when possible

(async) compile(name, templateopt, paramsopt, extensionopt) → {function}

Compiles a locally sandboxed async template rendering function and when applicable, stores the function in cache

Source:
Parameters:
Name Type Attributes Description
name String

The template name that uniquely identifies the primary template content

template String | Boolean <optional>

The raw template content, true to read from cache before compilation. Omit to load the template content from cache when the returned rendering function is called.

params URLSearchParams <optional>

Any URL search parmeters that will be passed when capturing the primary template and/or context when needed

extension String <optional>

The file extension designation for the template

Returns:
Type:
function

The return function from Sandbox.compile

(async) getRegistered(name, paramsopt, extensionopt) → {Object}

Retrieves a template, partial or context that resides in-memory.

Source:
Parameters:
Name Type Attributes Default Description
name String

The raw template name (i.e. not from Cachier.readWriteName)

params URLSearchParams <optional>

Any parameters designated during Cachier.registerPartial

extension String <optional>
options.defaultExtension

Optional override for a file extension designation for the template, partial or context designated during Cachier.registerPartial

Returns:
Type:
Object

A copy of the generated data from Cachier.registerPartial

(async) modules(optional) → {Object}

Source:
Parameters:
Name Type Description
optional TemplateOpts | function

Either the TemplateOpts or a function that takes a single name argument and returns the option value

Returns:
Type:
Object

The object that contains the modules used by the Cachier implementation

(async) read(name, forContentopt, extensionopt, paramsopt) → {Object}

Reads either template content or template code from internal cache. When options.partialsURL is set to an HTTPS URL and the read is for partial content, A GET call to window.fetch is made when running within browsers or to the https module when running on the server. The partial name will be appended to options.partialsURL (e.g. https://example.com/some/id.html where some/id.html is the the partial's Cachier.readWriteName and options.partialsURL is set to https://example.com). When options.partialsURL is omitted reading of template partial content will be limited to reading from memory. Compiled template source code is always read from memory only.

Source:
Parameters:
Name Type Attributes Description
name String

The template name that uniquely identifies the template content

forContent Boolean <optional>

true to read a template content, false to read the template source code

extension String <optional>

The file extension designation (only used when forContent is truthy)

params URLSearchParams <optional>

The URLSearchParams to pass for the read (only used when forContent is truthy)

Returns:
Type:
Object

An object read from cache that contains either the template content or module.

Returned template content properties:

  • name The template name that uniquely identifies the template content
  • content The template content
  • extension The template file extension designation

Returned module properties:

  • name The template name that uniquely identifies the template content
  • func The module function generated from the code

(async) readWriteName(name, optional, paramsopt, store, forContent, extensionopt, forContext) → {String}

Converts template names into a full path name consumable by read/write oprtations. Each function from Cachier.readWriteNames will be executed in order using the same arguments as Cachier.readWriteName as well as an additional last agument being the return value from the previous function invocation.

Source:
Parameters:
Name Type Attributes Description
name String

The name of the template, partial or context that will be converted into a name suitable for a read operation

optional TemplateOpts | function

Either the TemplateOpts or a function that takes a single name argument and returns the option value

params URLSearchParams <optional>

The parameters that should be used in the converted name

store Object

The storage object that can contain metadata used by naming operations

forContent Boolean

The flag indicating if the converted name is being used to capture partials

extension String <optional>

The file extension override for the converted name (omit to use the default extension set in the options)

forContext Boolean

The flag indicating if the converted name is being used to capture context

Returns:
Type:
String

The full template name

(async) register(dataopt, readopt, writeopt) → {Object}

Registers and caches the template, one or more partial templates and/or context JSON.

Source:
Parameters:
Name Type Attributes Description
data Array.<Object> <optional>

The template, partials and/or context to register.

Name Type Attributes Description
name String

The name that uniquely identifies the template, partial or context

content String <optional>

The raw content that will be registered. Omit when read === true to read content from cache.

params URLSearchParams <optional>

The URLSearchParams that will be passed during the content read (ignored when content is specified).

extension String <optional>

Optional override for a file extension designated for a template, partial or context.

read Boolean <optional>

When true, an attempt will be made to also Cachier.read the template, partials and context that do not have a content property set.

write Boolean <optional>

When true, an attempt will be made to also Cachier.write the template, partials and context that have a content property set.

Returns:
Type:
Object

An object that contains the registration results:

  • data The object that contains the template, partial fragments and/or context that have been registered
    • name The name that uniquely identifies the template, partial or context
    • content The raw content of the template, partial or context
    • extension The template file extension designation
    • params The URLSearchParams passed during the initial content read
    • fromRead A flag that indicates that the data was set from a read operation

registerHelper(func)

Registers a directive function that can be used within template interpolations

Source:
Parameters:
Name Type Description
func function

A named function that has no external scope dependencies/closures other than those exposed via templates during rendering

(async) registerPartial(name, contentOrParams, extensionopt) → {String}

Registers and stores a partial template in-memory. Use Cachier.register to write partials to cache (Cachier)

Source:
Parameters:
Name Type Attributes Default Description
name String

The raw template name (i.e. not from Cachier.readWriteName)

contentOrParams String | URLSearchParams

Either the partial template content string to register or the URLSearchParams that will be passed during the content read

extension String <optional>
options.defaultExtension

Optional override for a file extension designation for the partial

Returns:
Type:
String

The partial content

(async) unregister(name)

Unregisters a partial template from cache

Source:
Parameters:
Name Type Description
name String

The template name that uniquely identifies the template content

(async) write(name, data, forContentopt, extensionopt, paramsopt) → {function|undefined}

Writes either template content or template code from internal cache. When options.partialsURL is set to an HTTPS URL and the read is for partial content, A POST call to window.fetch is made when running within browsers or to the https module when running on the server. The partial name will be appended to options.partialsURL (e.g. https://example.com/some/id.html where some/id.html is the the partial's Cachier.readWriteName and options.partialsURL is set to https://example.com). When options.partialsURL is omitted writting of template partial content will be limited to writes to memory. Compiled template source code is always written to memory only.

Source:
Parameters:
Name Type Attributes Description
name String

The template name that uniquely identifies the template content

data String | function

The data to write. For content, data should be a string. Otherwise data should be a function or a function string.

forContent Boolean <optional>

true to read a template content, false to read the template source code

extension String <optional>

The file extension designation (only used when forContent is truthy)

params URLSearchParams <optional>

The URLSearchParams to pass for the write (only used when forContent is truthy)

Returns:
Type:
function | undefined

Optionally return the compiled/written module function


1.0.0 (2019-12-16)

Full Changelog