Name: cache-pug-templates
Owner: Lad
Description: Cache Pug templates for Lad/Koa/Express/Connect with Redis
Created: 2017-09-12 07:10:48.0
Updated: 2018-05-20 00:45:26.0
Pushed: 2018-03-06 20:35:50.0
Homepage: https://lad.js.org
Size: 95
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
npm:
install cache-pug-templates
yarn:
add cache-pug-templates
t path = require('path');
t redis = require('redis');
t cachePugTemplates = require('cache-pug-templates');
t views = path.join(__dirname, 'views');
ePugTemplates(redisClient, views);
t path = require('path');
t redis = require('redis');
t Koa = require('koa');
t cachePugTemplates = require('cache-pug-templates');
t app = new Koa();
t redisClient = redis.createClient();
ptional (e.g. if you want to cache in non-production)
pp.cache = true;
ote that koa requires us to specify a
ath name for the views directory
t views = path.join(__dirname, 'views');
listen(3000, () => {
chePugTemplates(app, redisClient, views);
t path = require('path');
t redis = require('redis');
t express = require('express');
t cachePugTemplates = require('cache-pug-templates');
t app = express();
t redisClient = redis.createClient();
ptional (by default express defaults to `./views`)
pp.set('views', path.join(__dirname, 'views'));
set('view engine', 'pug');
listen(3000, () => {
chePugTemplates(app, redisClient, views);
You can also pass an optional callback function with arguments err
and cached
.
The argument cached
is an Array of filenames that have been cached (from Object.keys(pug.cache)
).
..
listen(3000, () => {
chePugTemplates(app, redisClient, (err, cached) => {
if (err) throw err;
console.log(`successfully cached (${cached.length}) files`);
;
Note that with Koa you'll need to pass the views
argument as well:
..
t views = path.join(__dirname, 'views');
listen(3000, () => {
chePugTemplates(app, redisClient, views, (err, cached) => {
if (err) throw err;
console.log(`successfully cached (${cached.length}) files`);
;
By default this callback simply console.error
the err
(if any).
If you want to check what the cache state is at anytime:
t pug = require('pug');
..
et everything:
ole.log('pug.cache', pug.cache);
ust get the file names:
ole.log('pug cached files', Object.keys(pug.cache));
| Name | Website | | ————– | ————————– | | Nick Baugh | http://niftylettuce.com/ |