Permalink
Browse files

feat: add Redis cache tests back after a long time in exile

  • Loading branch information...
jimlambie committed Dec 20, 2016
1 parent 4b741e3 commit 5f3618e0ebd0b38d5a7648f7a871daac813bdcd0
Showing with 550 additions and 498 deletions.
  1. +1 −1 README.md
  2. +9 −4 dadi/lib/cache/index.js
  3. +1 −1 dadi/lib/controller/index.js
  4. +12 −1 dadi/lib/help.js
  5. +1 −1 scripts/coverage.svg
  6. +64 −66 test/acceptance/cache.js
  7. +47 −38 test/acceptance/flush.js
  8. +402 −373 test/unit/controller.js
  9. +13 −13 test/unit/model/hooks.js
@@ -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/)

@@ -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
@@ -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()

@@ -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')
@@ -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) => {
@@ -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
@@ -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')
@@ -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) {
@@ -1 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="94" height="20"><linearGradient id="b" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><mask id="a"><rect width="94" height="20" rx="3" fill="#fff"/></mask><g mask="url(#a)"><path fill="#555" d="M0 0h59v20H0z"/><path fill="#dfb317" d="M59 0h35v20H59z"/><path fill="url(#b)" d="M0 0h94v20H0z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="29.5" y="15" fill="#010101" fill-opacity=".3">coverage</text><text x="29.5" y="14">coverage</text><text x="75.5" y="15" fill="#010101" fill-opacity=".3">85%</text><text x="75.5" y="14">85%</text></g></svg>
<svg xmlns="http://www.w3.org/2000/svg" width="94" height="20"><linearGradient id="b" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="a"><rect width="94" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#a)"><path fill="#555" d="M0 0h59v20H0z"/><path fill="#dfb317" d="M59 0h35v20H59z"/><path fill="url(#b)" d="M0 0h94v20H0z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="29.5" y="15" fill="#010101" fill-opacity=".3">coverage</text><text x="29.5" y="14">coverage</text><text x="75.5" y="15" fill="#010101" fill-opacity=".3">89%</text><text x="75.5" y="14">89%</text></g></svg>
Oops, something went wrong.

0 comments on commit 5f3618e

Please sign in to comment.