Skip to content

Commit

Permalink
#110 early tags prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
krazyjakee committed Aug 10, 2018
1 parent 9f6971f commit cc02a24
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/ExpressRedisCache/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ function add(name, body, options, callback) {
+entry.expire
);
self.emit("message", message);

if (options.tag) {
self.emit("message", `SADD ${prefix}:${options.tag} "${redisKey}"`);
}
callback(null, name, entry, res);
});
} else {
Expand Down
63 changes: 63 additions & 0 deletions lib/ExpressRedisCache/delTag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import util from "util";
import async from "async";

/** Delete cache entries by tag
*
* @method ExpressRedisCache.delTag
* @description Delete entries by tag
* @return null
* @arg {String} name - The tag name
* @arg {Function} callback
*/

function delTag(name, callback) {
let self = this;

if (typeof name !== "string") {
return this.emit(
"error",
new Error("ExpressRedisCache.delTag: missing first argument String")
);
}

if (typeof callback !== "function") {
return this.emit(
"error",
new Error("ExpressRedisCache.delTag: missing second argument Function")
);
}

/** Get prefix */

let prefix = self.prefix.match(/:$/)
? self.prefix.replace(/:$/, "")
: self.prefix;

/** Tell Redis to delete hash */

let redisKey = `${prefix}:${name}`;

self.client.smembers(redisKey, (err, deletions) => {
deletions.push(redisKey);
async.each(
deletions,

(key, callback) => {
self.client.del(key, () => {
self.emit("message", util.format("DEL %s", key));
callback();
});
},

error => {
if (error) {
throw error;
}

callback(null, deletions.length);
}
);
});
}

export default delTag;
23 changes: 22 additions & 1 deletion lib/ExpressRedisCache/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ function route() {
name = res.express_redis_cache_name;
}

// If a tag has been explicitly attached to `res` then use it as tag

if (res.express_redis_cache_tag) {
tag = res.express_redis_cache_tag;
}

// If route() was called with a string as its first argument, use this string as name

if (typeof options[0] === "string") {
Expand All @@ -104,7 +110,17 @@ function route() {
* @type Number
* @default this.expire
*/

let expire = self.expire;

/** The object representing the tag for the cache key
*
* @type String
* @default this.tag
*/

let tag = self.tag;

if (typeof options[0] === "object") {
if (
typeof options[0].expire === "number" ||
Expand All @@ -113,6 +129,10 @@ function route() {
) {
expire = options[0].expire;
}

if (typeof options[0].tag === "string") {
tag = options[0].tag;
}
}

if (typeof options[0] === "number") {
Expand Down Expand Up @@ -181,7 +201,8 @@ function route() {
body,
{
type: this._headers["content-type"],
expire: expirationPolicy(req, res)
expire: expirationPolicy(req, res),
tag
},
function (name, cache) {}); // eslint-disable-line

Expand Down
11 changes: 11 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import redis from "redis";
import Get from "./ExpressRedisCache/get";
import Add from "./ExpressRedisCache/add";
import Del from "./ExpressRedisCache/del";
import DelTag from "./ExpressRedisCache/delTag";
import Route from "./ExpressRedisCache/route";
import Size from "./ExpressRedisCache/size";
import { config } from "../package.json";
Expand Down Expand Up @@ -140,6 +141,16 @@ export default class ExpressRedisCache extends EventEmitter {

this.del = Del;

/** js-comment
*
* @method
* @description This is a method
* @return void{Object}
* @arg {Object} arg - About arg
*/

this.delTag = DelTag;

/** js-comment
*
* @method
Expand Down
37 changes: 37 additions & 0 deletions test/delTag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import should from "should";
import async from "async";
import ERC from "../dist";

let prefix = process.env.EX_RE_CA_PREFIX || "erct:";
let host = process.env.EX_RE_CA_HOST || "localhost";
let port = process.env.EX_RE_CA_PORT || 6379;

const cache = new ERC({
prefix: prefix,
host: host,
port: port
});

describe("delTag", () => {
before(done => {
cache.add(
"tag-to-del",
"-",
{
tag: "testtag"
},
done
);
});

it("should be a function", () => {
cache.delTag.should.be.a.Function;
});

it("should callback", done => {
cache.delTag("testtag", ($error, $deletions) => {
debugger;
done();
});
});
});

0 comments on commit cc02a24

Please sign in to comment.