Skip to content

Commit

Permalink
Add simple lru cache of URLs into authenticate action
Browse files Browse the repository at this point in the history
  • Loading branch information
jezhiggins committed Sep 10, 2019
1 parent 7cbd87e commit 3befe91
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
12 changes: 11 additions & 1 deletion web-server/actions/authenticate-action.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
import fs from 'fs';
import axios from 'axios';
import path from 'path';
import os from 'os';
import os from 'os';
import LRU from 'lru-cache';
import { getTempFilename as tempFilename } from 'express-fileupload/lib/utilities';
import { authenticatePhoto } from '../imagehash/authenticate-photo'

const cache = new LRU(500);

async function authenticate(req, res) {
const imageUrl = req.query.url;

if (!imageUrl)
return res.status(400).send('No file uploaded');

const imageInfo = cache.get(imageUrl);
if (imageInfo)
return res.json(imageInfo);

let imagePath = null;
try {
imagePath = await downloadImage(imageUrl);

const authInfo = await authenticatePhoto(imagePath);

cache.set(imageUrl, authInfo);

res.json(authInfo);
} catch (e) {
res.status(500).send(`Authenticating failed: ${e.message}`);
Expand Down
5 changes: 4 additions & 1 deletion web-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"debug": "~2.6.9",
"express": "~4.16.1",
"express-fileupload": "^1.1.5",
"lru-cache": "^5.1.1",
"lz-string": "^1.4.4",
"morgan": "~1.9.1",
"web3": "0.20.6"
Expand All @@ -24,7 +25,9 @@
"start": "babel-node ./bin/www"
},
"nodemonConfig": {
"ignore": ["ethereum/fingerprints/*"],
"ignore": [
"ethereum/fingerprints/*"
],
"delay": "2500"
}
}

0 comments on commit 3befe91

Please sign in to comment.