(conf)
| 100173 | */ |
| 100174 | |
| 100175 | var CacheObject = function (conf) { |
| 100176 | conf = conf || {}; |
| 100177 | conf.ttl = parseInt(conf.ttl, 10) || 300; //0 is not permissible |
| 100178 | conf.cachesize = parseInt(conf.cachesize, 10) || 1000; //0 is not permissible |
| 100179 | |
| 100180 | this.ttl = conf.ttl * 1000; |
| 100181 | this.max = conf.cachesize; |
| 100182 | |
| 100183 | this.count = 0; |
| 100184 | this.data = {}; |
| 100185 | var next = __webpack_require__(480); |
| 100186 | |
| 100187 | this.set = function (key, value, callback) { |
| 100188 | var self = this; |
| 100189 | next(function () { |
| 100190 | if (self.data[key]) { |
| 100191 | if (self.data[key].newer) { |
| 100192 | if (self.data[key].older) { |
| 100193 | self.data[key].newer.older = self.data[key].older; |
| 100194 | self.data[key].older.newer = self.data[key].newer; |
| 100195 | } else { |
| 100196 | self.tail = self.data[key].newer; |
| 100197 | delete self.tail.older; |
| 100198 | } |
| 100199 | |
| 100200 | self.data[key].older = self.head; |
| 100201 | self.head.newer = self.data[key]; |
| 100202 | delete self.data[key].newer; |
| 100203 | self.head = self.data[key]; |
| 100204 | } |
| 100205 | |
| 100206 | self.head.val = value; |
| 100207 | self.head.hit = 0; |
| 100208 | self.head.ts = Date.now(); |
| 100209 | } else { |
| 100210 | // key is not exist |
| 100211 | self.data[key] = { |
| 100212 | "key" : key, |
| 100213 | "val" : value, |
| 100214 | "hit" : 0, |
| 100215 | "ts" : Date.now() |
| 100216 | }; |
| 100217 | |
| 100218 | if (!self.head) { |
| 100219 | // cache is empty |
| 100220 | self.head = self.data[key]; |
| 100221 | self.tail = self.data[key]; |
| 100222 | } else { |
| 100223 | // insert the new entry to the front |
| 100224 | self.head.newer = self.data[key]; |
| 100225 | self.data[key].older = self.head; |
| 100226 | self.head = self.data[key]; |
| 100227 | } |
| 100228 | |
| 100229 | if (self.count >= self.max) { |
| 100230 | // remove the tail |
| 100231 | var temp = self.tail; |
| 100232 | self.tail = self.tail.newer; |
nothing calls this directly
no test coverage detected