This repository has been archived by the owner on Oct 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
129 lines (112 loc) · 3.43 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
var hprose = require("hprose");
var events = require("events");
var fs = require("fs");
function FeatherWeight(opt) {
if(!(this instanceof FeatherWeight)) {
return new FeatherWeight(opt);
}
// enable eventing
events.EventEmitter.call(this);
// Setting up properties
this._store = {};
this._instance = {};
this.stats = {
hits: 0,
misses: 0
};
// The time that a value stays in the object.
this._defaultTtl = opt.ttl || 60;
// The interval, in seconds, in which "dead entries" are checked.
this._interval = opt.interval || 10;
if(opt.storeFile) this.storeFile = opt.storeFile;
else throw new TypeError("You must set storeFile.");
// It might be better to use an externalized cleaner. I.e. child_process.fork()
this._interval_t = setInterval(function(){
for(var prop in this._store) {
var obj = this._store[prop];
var expiresAt = new Date(Date.now()+obj.ttl);
var now = new Date();
if(now < expiresAt) {
// This key is due.
delete this._store[prop];
this.emit("expired", prop, obj.value);
}
}
}.bind(this), this._interval);
}
FeatherWeight.prototype.createServer = function(type, on, cb) {
var server, kind;
switch(type.toLowerCase()) {
case "http":
server = hprose.HttpServer;
kind = "http";
break;
case "ws":
case "websocket":
case "websockets":
server = hprose.WebSocketServer;
kind = "ws";
break;
case "tcp":
case "socket":
server = hprose.TcpServer;
kind = "tcp";
break;
case "unix":
server = hprose.UnixServer;
kind = "unix";
break;
default: throw new Error("Unknown type: "+type);
}
this._instance[kind] = new server();
this._instance[kind].listen.call(this._instance[kind], on, cb);
}
FeatherWeight.prototype.set = function(key, data, ttl) {
this._store[key] = {
value: data,
ttl: (typeof ttl != "undefined" ? Number(ttl) : this._defaultTtl),
createdAt: new Date()
}
}
FeatherWeight.prototype.get = function(keys) {
var rt = {};
var getter = function(key){
if(typeof this.store[key] == "undefined") {
this.stats.misses++;
rt[key] = undefined;
} else {
this.stats.hits++;
rt[key] = this.store[key].value;
}
}.bind(this);
if(keys instanceof Array) {
keys.forEach(getter);
return rt;
} else if(typeof keys == "string") {
getter(keys);
return rt[keys];
}
};
FeatherWeight.prototype.purge = function(keys) {
var deleter = function(key){
if(typeof this.store[key] != "undefined") {
delete this.store[key];
}
}.bind(this);
if(keys instanceof Array) {
keys.forEach(deleter);
return rt;
} else if(typeof keys == "string") {
deleter(keys);
}
};
FeatherWeight.prototype.run = function(cb) {
// Initialize our methods on all servers, if any.
if(Object.keys(this._instance).length == 0) {
throw new Error("You have not created any server that this cache can bind to! use .createServer()");
}
for(var kind in this._instance) {
var srv = this._instance[kind];
// srv.addAsyncMethods(...);
}
}