-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:LaurentZuijdwijk/streaming-cache
Conflicts: package.json
- Loading branch information
Showing
10 changed files
with
871 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,34 @@ | ||
|
||
/** | ||
In this example we create a simple server that serves the first file from disk and subsequent | ||
requests from cache | ||
**/ | ||
'use strict'; | ||
var http = require('http'); | ||
const PORT = 8080; | ||
var PORT = 8080; | ||
|
||
var fs = require('fs'); | ||
var Cache = require('../index.js'); | ||
var cache = new Cache(); | ||
|
||
function xhandleRequest(request, response) { | ||
var _cache = cache.get('a'); | ||
var meta; | ||
if (_cache) { | ||
// meta = cache.getMetadata('a'); | ||
// if (meta && meta.length) { | ||
// response.setHeader('Content-Length', meta.length) | ||
// response.setHeader('Content-Type', 'image/jpeg'); | ||
// } | ||
_cache.pipe(response); | ||
} | ||
else { | ||
var readstream = fs.createReadStream('./stream.jpg'); | ||
readstream.pipe(cache.set('a')).pipe(response) | ||
// fs.readFile('./stream.jpg', function (err, data) { | ||
// // cache.setData('a', data); | ||
// response.end(data) | ||
} | ||
} | ||
|
||
//Create a server | ||
var server = http.createServer(handleRequest); | ||
var FILENAME = './stream.jpg'; | ||
|
||
//Lets start our server | ||
server.listen(PORT, function () { | ||
//Callback triggered when server is successfully listening. Hurray! | ||
console.log('Server listening on: http://localhost:%s', PORT); | ||
console.log('Server listening on: http://localhost:%s', PORT); | ||
}); | ||
|
||
function handleRequest(request, response) { | ||
cache.getData('a', function (err, data) { | ||
if (data) { | ||
response.end(data); | ||
} | ||
else { | ||
fs.readFile('./stream.jpg', function (err, data) { | ||
cache.setData('a', data); | ||
response.end(data) | ||
}); | ||
} | ||
}); | ||
if (cache.exists(FILENAME)) { | ||
response.setHeader('From-Cache', 'true'); | ||
cache.get(FILENAME).pipe(response); | ||
} | ||
else { | ||
response.setHeader('From-Cache', 'false'); | ||
fs.createReadStream(FILENAME) | ||
.pipe(cache.set(FILENAME)) | ||
.pipe(response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. | ||
# yarn lockfile v1 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0123456789 |
Oops, something went wrong.