Giter VIP home page Giter VIP logo

proxycache's Introduction

proxycache

Build Status NPM Version Coverage Status

A simple, configurable, Redis-powered caching proxy.

Use it when you want to

  • deliver a URL to a static file
  • cache the file somewhere
  • provide a URL to the cached version for subsequent requests

Install

$ npm install --save proxycache

Example

const express = require('express')
const request = require('request')
const proxycache = require('proxycache')

// Create Redis/Cloud Storage proxycache client
const config = {
  store: {
    client: 'redis',
    connection: {}
  },
  cache: {
    client: 'gcloud',
    connection: {
      keyFilename: './gcloud-key.json',
      projectId: 'my-project-id'
    },
    options: {
      bucket: 'images'
    }
  }
}
let cache
proxycache(config).then(client => {
  cache = client
})

// Image server to proxy for
const imgsrv = express()
imgsrv.get('/images/:id', (req, res) => {
  const id = req.params.id
  request.get(`http://www.fillmurray.com/g/${id}/${id}`).pipe(res)
})
imgsrv.listen(3888)
console.log('Image server listening on 3888')

const render = src => `
<html>
  <head>
   <title>bfm</title>
  </head>
  <body>
   <img src='${src}' height=500 width=500/>
  </body>
</html>
`

// API Server
const app = express()
app.get('/images/:id', (req, res) => {
  const id = req.params.id
  // query the cache
  cache.get(id).then(result => {
   // cache hit
    if (result) {
      return res.end(render(result))
    }
    const uri = `http://localhost:3888/images/${id}`
   // cache miss; send the default url
    res.end(render(uri))
   // cache the file; TTL in seconds
    cache.set(id, uri, 60)
  })
})
app.listen(8000)

API

proxycache(options)

options

Type: object

License

MIT © Forrest Desjardins

proxycache's People

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

morristech

proxycache's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.