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

ogre-cache

v0.0.1

Published

Simple browser headers cache control for Express

Downloads

4

Readme

========== Ogre Cache

|npmversion| |license| |downloads|

.. |npmversion| image:: https://img.shields.io/npm/v/ogre-cache.svg?style=flat-square :alt: NPM Version :target: https://npmjs.org/package/ogre-cache

.. |license| image:: http://img.shields.io/npm/l/pgre-cache.svg?style=flat-square :alt: License :target: LICENSE

.. |downloads| image:: http://img.shields.io/npm/dm/ogre-cache.svg?style=flat-square :alt: Downloads :target: https://npmjs.org/package/ogre-cache

.. contents::

Introduction

Add to Express <http://expressjs.com/>_ a simple and rude support to leverage browser cache.

The framework already support caching with the maxAge option in the express.static middleware <http://expressjs.com/api.html#express.static> but this kind of cache is the weakest one. Relying onto the HTTP 304 status code will not avoid the roundtrip of asking to the server if the resource is still "fresh". Also, this is only be available for static resources.

The best (while more difficult to be controlled) type of cache you can use is by sending special headers to browser, driving them to keep the resource in cache for an amount of time. This is done by sending the Cache-Control header.

How to use

Quick start

.. code-block:: javascript

var app = express();
var ogre = require('ogre-cache');
app.use(ogre.cache());

This will register Ogre default cache configuration: a strong, one week, cache headers for all resources like:

  • JavaScript
  • Images
  • CSS
  • CSS related contents (font, icon-font stuff)

Advanced use

The cache function returns a middleware configured as you wish. If no parameters are provided, the only named configuration available is used: This configuration is named "default" and it's stored inside the internal registry of the product.

You can easily use different named configurations:

.. code-block:: javascript

router.use(ogre.cache("default", "bar"));

Order matter: latter provided configuration "wins" on formers ones.

Configuration

Ogre Cache is using the simplest possible approach: caching configuration are activated by inspecting the calling URL: you can register a cache behavior by testing regular expressions on URLs and provinding an expire age (in seconds).

.. code-block:: javascript

app.use(new ogre.configuration.CacheConfiguration()
    .add('/settings', 600));

By simply looking at URL you can cache not only static contents but also every other registered routes.

There's facilitation while defining regex for file extensions:

.. code-block:: javascript

app.use(new ogre.configuration.CacheConfiguration()
    .add('*.avi', 600));

Named configurations are only configurations you'll save for be reused:

.. code-block:: javascript

ogre.register('files',
    new ogre.configuration.CacheConfiguration())
    .add(/.*\.(pdf|doc|odt)/gi, 86400);
app.use(ogre.cache("files"));

Limitations and troubleshooting

First rule is to be sure on what you are chaching: never not put in the user's cache a resource you are not sure will not change, or do not use long-term expire period.

The cache registration must precede the route it's applied to.

Using URL to investigate if a resouce must be cached is a weak approach. A badly configured regex could leave to unexpected results (that mean cache resources you don't want to cache). Be aware.