1.1.5 • Published 9 years ago

proxycache v1.1.5

Weekly downloads
3
License
MIT
Repository
github
Last release
9 years ago

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

1.1.5

9 years ago

1.1.4

9 years ago

1.1.3

9 years ago

1.1.2

9 years ago

1.1.1

9 years ago

1.1.0

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago

0.2.12

9 years ago

0.2.11

9 years ago

0.2.10

9 years ago

0.2.9

10 years ago

0.2.8

10 years ago

0.2.7

10 years ago

0.2.6

10 years ago

0.2.5

10 years ago

0.2.4

10 years ago

0.2.3

10 years ago

0.2.2

10 years ago

0.2.1

10 years ago

0.2.0

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago

0.0.0

10 years ago