Skip to content

Commit

Permalink
Better caching
Browse files Browse the repository at this point in the history
Now saves all hashes,links to persistent csv file
Now clears temporary files from disk after uploading to imgur and saving cache
  • Loading branch information
MadaraUchiha committed Jun 16, 2015
1 parent 48a90fb commit 0ed11a8
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ build/Release
node_modules

target

cache.csv
34 changes: 34 additions & 0 deletions cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import fs from 'fs';
import {parse} from 'csv';
import {promisify} from 'bluebird';

let parseAsync = promisify(parse);

export class Cache {
constructor(file) {
this.file = file;
this.cache = new Map();
}

static fromCsvFile(file) {
let cacheInstance = new Cache(file);
fs.readFileAsync('cache.csv', {flag: 'a+'})
.then(parseAsync)
.then(parsed => {
debugger;
cacheInstance.cache = parsed.reduce((map, row) => map.set(row[0], row[1]), new Map());
return Promise.resolve();
})
.then(() => console.log('Cache is ready!'));
return cacheInstance;
}

get(hash) {
return this.cache.get(hash);
}

set(hash, link) {
this.cache.set(hash, link);
return fs.appendFileAsync(this.file, hash+","+link+"\n")
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"babel": "^5.5.8",
"bluebird": "^2.9.30",
"body-parser": "^1.13.0",
"csv": "^0.4.5",
"del": "^1.2.0",
"express": "^4.12.4",
"gm": "^1.18.1",
Expand Down
10 changes: 9 additions & 1 deletion realIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import fs from 'fs';
import url from 'url';
import crypto from 'crypto';

import {Cache} from './cache';

let delAsync = Promise.promisify(del);

let app = express();
Expand All @@ -20,12 +22,16 @@ Promise.promisifyAll(fs);
Promise.promisifyAll(gm.prototype);

let targetDir = process.env.CAPTION_MAKER_TARGET_DIR || "target";
let cache = new Map();
let cache = Cache.fromCsvFile('cache.csv');

app.use(json());
app.use(urlencoded({extended: false}));
app.post('/', async function handlePost(req, res) {
console.log(req.body);

req.body.top = req.body.top.toUpperCase();
req.body.bottom = req.body.bottom.toUpperCase();

let sha = sha1(JSON.stringify(req.body));
console.log("SHA1:", sha);
if (cache.get(sha)) {
Expand All @@ -41,6 +47,8 @@ app.post('/', async function handlePost(req, res) {
let json = await imgur.uploadFile(`${location}/output`);
console.log("Done. Imgur link", json.data.link, "Adding to cache");
cache.set(sha, json.data.link);
console.log("Removing from filesystem");
await delAsync(location);
res.end(json.data.link);
});

Expand Down

0 comments on commit 0ed11a8

Please sign in to comment.