Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cache option #31

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const env = vento({
useWith: true,
includes: Deno.cwd(),
autoescape: false,
cache: true,
});
```

Expand Down Expand Up @@ -84,6 +85,16 @@ const result = env.runString("{{ title |> safe }}", {

The path of the directory that Vento will use to look for includes templates.

### cache

Set `false` to ignore the cache, and re-compile the template when loading.

```js
const env = vento({
cache: false, // default is true
});
```

## Filters

Filters are custom functions to transform the content.
Expand Down
2 changes: 2 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface Options {
useWith?: boolean;
dataVarname?: string;
autoescape?: boolean;
cache?: boolean;
}

export default function (options: Options = {}) {
Expand All @@ -30,6 +31,7 @@ export default function (options: Options = {}) {
dataVarname: options.dataVarname || "it",
autoescape: options.autoescape ?? false,
useWith: options.useWith ?? true,
cache: options.cache ?? true,
});

// Register basic plugins
Expand Down
13 changes: 9 additions & 4 deletions src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export interface Options {
dataVarname: string;
autoescape: boolean;
useWith: boolean;
cache: boolean;
}

export class Environment {
Expand Down Expand Up @@ -75,10 +76,12 @@ export class Environment {
file?: string,
): Promise<TemplateResult> {
if (file) {
const cached = this.cache.get(file);
if (this.options.cache) {
const cached = this.cache.get(file);

if (cached) {
return await cached(data);
if (cached) {
return await cached(data);
}
}

const template = this.compile(source, file);
Expand Down Expand Up @@ -154,11 +157,13 @@ export class Environment {
async load(file: string, from?: string): Promise<Template> {
const path = from ? this.options.loader.resolve(from, file) : file;

if (!this.cache.has(path)) {
if (!this.cache.has(path) || !this.options.cache) {
const { source, data } = await this.options.loader.load(path);
const template = this.compile(source, path, data);

this.cache.set(path, template);

return template;
}

return this.cache.get(path)!;
Expand Down