Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Wiping the slate, starting from scratch - initial commit of very simp…

…lified file based cache
  • Loading branch information...
commit 351d341922f527292d342afee849a54cf816ebae 1 parent b7b255c
Zachary Parmley authored
Showing with 41 additions and 132 deletions.
  1. +41 −70 BillBill.js
  2. +0 −20 StreamedAlfredModel.js
  3. +0 −19 datastore.js
  4. +0 −23 pageCache.js
View
111 BillBill.js
@@ -1,70 +1,41 @@
-var http = require('http')
- , url = require('url')
- , mapleTree = require('mapleTree')
- , request = require('request')
- , pageCache = require('./pageCache.js')
- ;
-
-
-
-
-
-
-var configs = [
- {
- 'host' : 'www.example.org',
- 'origin' : '127.0.0.1:2019',
- 'ttl' : 7200,
- 'excludes' : [
- '/wp-admin*',
- '/wp-login.php'
- ]
- }
-]
-
-var servers = {}
-var cache = new pageCache.PageCache()
-configs.forEach(function(conf) {
- var host = conf.host
- conf.router = new mapleTree.RouteTree()
-
- conf.excludes.forEach(function(exclude) {
- console.log('Excluding ' + exclude)
- conf.router.define(exclude, function() {
- return false
- })
- })
-
- conf.router.define('*', function() { return conf.ttl })
- conf.router.define('/', function() { return conf.ttl })
-
-
- servers[host] = conf
-})
-
-
-
-http.createServer(function(req, res) {
- console.log([req.headers.host, req.url])
- var host = req.headers.host
- var url = req.url
- if (servers[host] !== undefined) {
- match = servers[host].router.match(url)
- if (match !== undefined) {
- var ttl = match.fn()
- console.log(ttl)
- if (ttl !== false) {
- console.log('cacheable')
- request.get('http://www.example.org' + req.url).pipe(res)
- } else {
- console.log('skipping')
- request.get('http://www.example.org' + req.url).pipe(res)
- }
- }
- } else {
- res.writeHead(200, {"Content-Type": "text/plain"});
- res.write('nope dummy');
- res.end();
- }
-
-}).listen(80)
+var fs = require('fs')
+var filed = require('filed')
+var http = require('http')
+var request = require('request')
+
+// This needs to be config'd
+var cacheDir = 'pagecache'
+
+
+// Set up the server
+http.createServer(function(req, resp) {
+ var host = req.headers['host']
+ var url = req.url
+ var cacheFile = cacheDir + '/' + new Buffer(host + url).toString('base64')
+
+ console.log(cacheFile)
+
+ fs.lstat(cacheFile, function(err, stats) {
+ if (!err && stats.isFile()) {
+ // Cache hit
+ filed(cacheFile).pipe(resp)
+ } else {
+ // Cache miss
+
+ // Currently a server on 2020 is waitig 3 seconds and sending a simple response, for dev
+ var originReq = request('http://localhost:2020')
+ var filedFile = filed(cacheFile)
+
+ console.log(originReq)
+
+ originReq.on('close', function() {
+ filedFile.pipe(resp)
+ })
+
+ originReq.pipe(filedFile)
+ // filed(cacheFile).pipe(resp)
+ // .pipe(resp)
+ }
+ })
+
+}).listen(10001)
View
20 StreamedAlfredModel.js
@@ -1,20 +0,0 @@
-var StreamedAlfredModel = function(model, data, streamField) {
- this.model = model
- this.data = data
- this.streamField = streamField
- this.streamData = ''
- this.writeable = true
- this.readable = true
-}
-
-StreamedAlfredModel.prototype.write = function(data) {
- this.streamData = this.streamData + data
- return true
-}
-
-StreamedAlfredModel.prototype.end = function(data) {
- if (data) { this.write(data) }
- this.data[this.streamField] = this.streamData
- this.model.new(this.data).save(function(er) { console.log(er) })
- emit 'close'
-}
View
19 datastore.js
@@ -1,19 +0,0 @@
-var alfred = require('Alfred')
-
-var getCachedPageModel = function(cb) {
- alfred.open('./alfreddata/', function(er, db) {
- if (er) { throw er }
- var CachedPage = db.define('CachedPage', {
- indexes: [
- { name: 'expires',
- fn: function(CachedPage) { return CachedPage.expires }},
- { name: 'key',
- fn: function(CachedPage) { return CachedPage.cacheKey }}]
- })
-
- CachedPage.property('expires', Number)
- CachedPage.property('content', 'string')
- CachedPage.property('cacheKey', 'string')
- cb(CachedPage)
- })
-}
View
23 pageCache.js
@@ -1,23 +0,0 @@
-var PageCache = function(options) {
- this.PageCache = {}
-}
-PageCache.prototype.setData = function(key, value) {
- this.PageCache[key] = value
- return true
-}
-PageCache.prototype.getData = function(key) {
- return this.PageCache[key]
-}
-PageCache.prototype.deleteData = function(key) {
- this.PageCache[key] = null
- return true
-}
-PageCache.prototype.flushData = function() {
- this.PageCache = {}
-}
-PageCache.prototype.updateData = function(key, field, value) {
- this.PageCache[key][field] = value
-}
-
-
-exports.PageCache = PageCache
Please sign in to comment.
Something went wrong with that request. Please try again.