UNPKG

2.03 kBJavaScriptView Raw
1/*jslint node: true, nomen: true, esversion: 6 */
2"use strict";
3
4const assert = require('assert');
5
6const NodeWeakHashmap = require('../util/nodeWeakHashmap');
7const debug = require('debug')('upnpserver:db:cachedRegistry');
8
9const AbstractRegistry = require('./abstractRegistry');
10
11const CACHE_DELAY_MS = 1000 * 10;
12
13class CachedRegistry extends AbstractRegistry {
14
15 /**
16 *
17 */
18 initialize(service, callback) {
19
20 var garbage = (node, key, infos) => {
21 debug("intialize.garbage", "Garbage node #", node.id, infos.readCount);
22
23 var sem = node._isLocked();
24 if (sem !== false) {
25 debug("intialize.garbage", "Not releasable #", node.id, "locked by semaphore=", sem);
26 this._map.put(node, node);
27 return;
28 }
29
30 if (this._garbageNode) {
31 this._garbageNode(node);
32 }
33 };
34
35 this._map = new NodeWeakHashmap("nodeById", CACHE_DELAY_MS, false, garbage);
36
37 debug("intialize", "CachedRegistry initialized");
38
39 super.initialize(service, callback);
40 }
41
42 /**
43 *
44 */
45 clear(callback) {
46 assert(typeof(callback)==="function", "Invalid callback parameter");
47
48 this._map.clear();
49
50 debug("clear", "Clear all registry");
51
52 callback(null);
53 }
54
55 /**
56 *
57 */
58 saveNode(node, modifiedProperties, callback) {
59 this._map.put(node, node);
60
61 debug("saveNode", "Put in cache node #", node.id);
62
63 callback(null, node);
64 }
65
66 /**
67 *
68 */
69 getNodeById(id, callback) {
70 assert(typeof(callback)==="function", "Invalid callback parameter");
71
72 var node = this._map.get(id);
73
74 debug("getNodeById", "Find node #", id, "=>", !!node);
75
76 callback(null, node);
77 }
78
79 /**
80 *
81 */
82 unregisterNode(node, callback) {
83 assert(typeof(callback)==="function", "Invalid callback parameter");
84
85 this._map.remove(node);
86
87 debug("unregisterNode", "Unregister node #", node.id);
88
89 callback(null, node);
90 }
91}
92
93module.exports = CachedRegistry;