Skip to content

Commit

Permalink
feat: add Redis cache tests back after a long time in exile
Browse files Browse the repository at this point in the history
  • Loading branch information
jimlambie committed Dec 20, 2016
1 parent 4b741e3 commit 5f3618e
Show file tree
Hide file tree
Showing 9 changed files with 550 additions and 498 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<img src="http://52.209.207.148/assets/products/dadi-api-full.png" alt="DADI API" height="65"/>

[![npm (scoped)](https://img.shields.io/npm/v/@dadi/api.svg?maxAge=10800&style=flat-square)](https://www.npmjs.com/package/@dadi/api)
[![coverage](https://img.shields.io/badge/coverage-85%25-yellow.svg?style=flat-square)](https://github.com/dadi/api)
[![coverage](https://img.shields.io/badge/coverage-89%25-yellow.svg?style=flat-square)](https://github.com/dadi/api)
[![Build Status](https://travis-ci.org/dadi/api.svg?branch=master)](https://travis-ci.org/dadi/api)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](http://standardjs.com/)

Expand Down
13 changes: 9 additions & 4 deletions dadi/lib/cache/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ var _ = require('underscore')

var config = require(path.join(__dirname, '/../../../config'))
var log = require('@dadi/logger')

var DadiCache = require('@dadi/cache')
var cache = new DadiCache(config.get('caching'))
var cache

var Cache = function (server) {
log.info({module: 'cache'}, 'Cache logging started.')
this.cache = cache = new DadiCache(config.get('caching'))

this.server = server
this.enabled = config.get('caching.directory.enabled') || config.get('caching.redis.enabled')
this.encoding = 'utf8'
this.options = {}

log.info({module: 'cache'}, 'Cache logging started.')
}

var instance
Expand Down Expand Up @@ -55,7 +56,7 @@ Cache.prototype.getEndpointContentType = function (req) {
Cache.prototype.init = function () {
var self = this

this.server.app.use(function (req, res, next) {
this.server.app.use((req, res, next) => {
var enabled = self.cachingEnabled(req)
if (!enabled) return next()

Expand All @@ -80,6 +81,8 @@ Cache.prototype.init = function () {
// get contentType that current endpoint requires
var contentType = self.getEndpointContentType(req)

console.log(cacheKey)

// attempt to get from the cache
cache.get(cacheKey).then((stream) => {
log.info({module: 'cache'}, 'Serving ' + req.url + ' from cache')
Expand Down Expand Up @@ -149,6 +152,8 @@ module.exports.reset = function () {
*
*/
module.exports.delete = function (pattern, callback) {
if (!cache) return callback(null)

cache.flush(pattern).then(() => {
return callback(null)
}).catch((err) => {
Expand Down
2 changes: 1 addition & 1 deletion dadi/lib/controller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ Controller.prototype.post = function (req, res, next) {
pathname = pathname.replace('/' + req.params.id, '')

// flush cache for POST requests
help.clearCache(pathname, function (err) {
help.clearCache(pathname, (err) => {
if (err) return next(err)

// if id is present in the url, then this is an update
Expand Down
13 changes: 12 additions & 1 deletion dadi/lib/help.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var _ = require('underscore')
var crypto = require('crypto')
var formatError = require('@dadi/format-error')
var fs = require('fs')
var Moment = require('moment')
Expand Down Expand Up @@ -265,8 +266,18 @@ module.exports.validateCollectionSchema = function (obj) {
module.exports.clearCache = function (pathname, callback) {
var pattern = ''

pattern = crypto.createHash('sha1').update(pathname).digest('hex')

if (config.get('caching.redis.enabled')) {
pattern = pathname
pattern = pattern + '*'
}

if (pathname === '*' || pathname === '') {
if (config.get('caching.redis.enabled')) {
pattern = '*'
} else {
pattern = ''
}
}

cache.delete(pattern, function (err) {
Expand Down
2 changes: 1 addition & 1 deletion scripts/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 5f3618e

Please sign in to comment.