UNPKG

2.41 kBJavaScriptView Raw
1// Copyright 2012 Timothy J Fontaine <tjfontaine@gmail.com>
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
20'use strict';
21
22var MemoryStore = exports.MemoryStore = function (opts) {
23 this._store = {};
24};
25
26MemoryStore.prototype.get = function (domain, key, cb) {
27 var d = domain.toLowerCase();
28 var k = key.toLowerCase();
29 var result = this._store[d];
30
31 if (result)
32 result = result[k];
33
34 process.nextTick(function () {
35 cb(null, result);
36 });
37};
38
39MemoryStore.prototype.set = function (domain, key, data, cb) {
40 var d = domain.toLowerCase();
41 var k = key.toLowerCase();
42 var result_domain = this._store[d];
43
44 if (!result_domain)
45 result_domain = this._store[d] = {};
46
47 result_domain[k] = data;
48
49 if (cb) {
50 process.nextTick(function () {
51 cb(null, data);
52 });
53 }
54};
55
56MemoryStore.prototype.delete = function (domain, key, type, cb) {
57 var d, k;
58
59 if (!cb) {
60 cb = type;
61 type = undefined;
62 }
63
64 if (!cb) {
65 cb = key;
66 type = undefined;
67 }
68
69 d = this._store[domain.toLowerCase()];
70
71 if (d && key)
72 k = d[key.toLowerCase()];
73
74 if (domain && key && type) {
75 if (d && k) {
76 delete k[type];
77 }
78 } else if (domain && key) {
79 if (d) {
80 delete d[k];
81 }
82 } else if (domain) {
83 if (d) {
84 delete this._store[domain.toLowerCase()];
85 }
86 }
87
88 if (cb) {
89 process.nextTick(function () {
90 cb(null, domain);
91 });
92 }
93};