npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-view-cache-conditional

v0.0.1

Published

Unobtrusive solution to express framework - cache rendered page on server side in redis database, without unnecessary database requests and rendering.

Downloads

1

Readme

express-view-cache

NPM version Build Status Dependency Status Bitdeli Badge

Unobtrusive solution to express 4.0.0 framework - cache response content in Redis database.

Attention!

Version 0.0.6 is abandoned. Sorry. But it is still present, you can use it, if you are brave. But i have encountered few problems with this version i was unable to solve, and i decided to rewrite it from scratch, with near the same API and better functionality.

Shameless advertisement

You can hire the author of this package by Odesk - https://www.odesk.com/users/~0120ba573d09c66c51

Why do we need this plugin and how does it work?

Let's consider we have a NodeJS application with code like this:

app.get('/getPopularPosts',function(req,res){
    req.model.posts.getPopular(function(err,posts){
        if(err) throw err;
        res.render('posts',{"posts":posts});
    });
});

The method getPopular of posts requires a call to database and executed slowly. Also rendering the template of posts requires some time. So, maybe we need to cache all this? Ideally, when visitor gets the page with url /getPopularPosts we have to give him info right from cache, without requests to database, parsing data received, rendering page and other things we need to do to give him this page. The most expressJS way to do it is to make a separate middleware, that is ran before router middleware, and returns page from cache (if it is present in cache) or pass data to other middlewares, but this caching middleware adds a listener to response, which SAVES rendered response to cache. And for future use, the response is taken from CACHE!

It is turned that it works best with node-redis with redis >v2.6.16

Example

There is a complete example of NodeJS + ExpressJS (4.x.x) application which responds with current time.


    var express = require('express'),
      morgan = require('morgan'),
      errorHandler = require('errorhandler'),
      request = require('request'),
      http = require('http'),
      EVC = require('express-view-cache'),
      app = express(),
      evc = EVC('redis://redis:someLongAuthPassword@localhost:6379');

    app.set('port', process.env.PORT || 3000);
    app.use(morgan('dev'));
    app.use('/cacheFor5sec', evc.cachingMiddleware(5000));
    app.use('/cacheFor3sec', evc.cachingMiddleware(3000));
    app.get('*', function (request, response) {
      response.json({
        'Page Created At': new Date().toLocaleTimeString()
      });
    });
    app.use(errorHandler());

    http.createServer(app).listen(app.get('port'), function () {
      console.log("Express server listening on port " + app.get('port'));

      setInterval(function(){
        request('http://localhost:'+app.get('port')+'/', function(error, response, body){
          console.log('GET /',body);
        })
      }, 1000);

      setInterval(function(){
        request('http://localhost:'+app.get('port')+'/cacheFor3sec', function(error, response, body){
          console.log('GET /cacheFor3sec',body);
        })
      }, 1000);
    });

Options

var evc = EVC(options);
app.use('/someURItoBeCachedForFiveSeconds', evc.cachingMiddleware(5000));

Options can be a redis connection string like redis://usernameTotallyIgnored:[email protected]:6379

also options can be an dictionary object with this fields:

  • host - default is localhost - the hostname of redis server
  • port - default is 6379 - the port where the redis server listens
  • pass - password for redis authorization, default is null
  • client - ready to use node-redis client - this option overrides all previous
  • appPort - port of nodejs application to use, default is process.env.PORT or 3000

Tests

$ npm test

License

The MIT License (MIT)

Copyright (c) 2013 Ostroumov Anatolij ostroumov095(at)gmail(dot)com et al.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.