|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var assert = require('assert'); |
| 4 | +var Connector = require('loopback-connector').Connector; |
| 5 | +var debug = require('debug')('loopback:connector:kv-memory'); |
| 6 | +var util = require('util'); |
| 7 | + |
| 8 | +exports.initialize = function initializeDataSource(dataSource, cb) { |
| 9 | + var settings = dataSource.settings; |
| 10 | + dataSource.connector = new KeyValueMemoryConnector(settings, dataSource); |
| 11 | + if (cb) process.nextTick(cb); |
| 12 | +}; |
| 13 | + |
| 14 | +function KeyValueMemoryConnector(settings, dataSource) { |
| 15 | + Connector.call(this, 'kv-memory', settings); |
| 16 | + |
| 17 | + debug('Connector settings', settings); |
| 18 | + |
| 19 | + this.dataSource = dataSource; |
| 20 | + this.DataAccessObject = dataSource.juggler.KeyValueAccessObject; |
| 21 | + |
| 22 | + this._store = Object.create(null); |
| 23 | + |
| 24 | + this._setupRegularCleanup(); |
| 25 | +}; |
| 26 | +util.inherits(KeyValueMemoryConnector, Connector); |
| 27 | + |
| 28 | +KeyValueMemoryConnector.prototype._setupRegularCleanup = function() { |
| 29 | + // Scan the database for expired keys at a regular interval |
| 30 | + // in order to release memory. Note that GET operation checks |
| 31 | + // key expiration too, the scheduled cleanup is merely a performance |
| 32 | + // optimization. |
| 33 | + var self = this; |
| 34 | + this._cleanupTimer = setInterval( |
| 35 | + function() { self._removeExpiredItems(); }, |
| 36 | + 1000); |
| 37 | + this._cleanupTimer.unref(); |
| 38 | +}; |
| 39 | + |
| 40 | +KeyValueMemoryConnector._removeExpiredItems = function() { |
| 41 | + debug('Running scheduled cleanup of expired items.'); |
| 42 | + for (var modelName in this._store) { |
| 43 | + var modelStore = this._store[modelName]; |
| 44 | + for (var key in modelStore) { |
| 45 | + if (modelStore[key].isExpired()) { |
| 46 | + debug('Removing expired key', key); |
| 47 | + delete modelStore[key]; |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | +}; |
| 52 | + |
| 53 | +KeyValueMemoryConnector.prototype._getStoreForModel = function(modelName) { |
| 54 | + if (!(modelName in this._store)) { |
| 55 | + this._store[modelName] = Object.create(null); |
| 56 | + } |
| 57 | + return this._store[modelName]; |
| 58 | +}; |
| 59 | + |
| 60 | +KeyValueMemoryConnector.prototype.get = |
| 61 | +function(modelName, key, options, callback) { |
| 62 | + var store = this._getStoreForModel(modelName); |
| 63 | + var item = store[key]; |
| 64 | + |
| 65 | + if (item && item.isExpired()) { |
| 66 | + debug('Removing expired key', key); |
| 67 | + delete store[key]; |
| 68 | + item = undefined; |
| 69 | + } |
| 70 | + |
| 71 | + var value = item ? item.value : null; |
| 72 | + |
| 73 | + debug('GET %j %j -> %s', modelName, key, value); |
| 74 | + |
| 75 | + if (/^buffer:/.test(value)) { |
| 76 | + value = new Buffer(value.slice(7), 'base64'); |
| 77 | + } else if (/^date:/.test(value)) { |
| 78 | + value = new Date(value.slice(5)); |
| 79 | + } else if (value != null) { |
| 80 | + value = JSON.parse(value); |
| 81 | + } |
| 82 | + |
| 83 | + process.nextTick(function() { |
| 84 | + callback(null, value); |
| 85 | + }); |
| 86 | +}; |
| 87 | + |
| 88 | +KeyValueMemoryConnector.prototype.set = |
| 89 | +function(modelName, key, value, options, callback) { |
| 90 | + var store = this._getStoreForModel(modelName); |
| 91 | + var value; |
| 92 | + if (Buffer.isBuffer(value)) { |
| 93 | + value = 'buffer:' + value.toString('base64'); |
| 94 | + } else if (value instanceof Date) { |
| 95 | + value = 'date:' + value.toISOString(); |
| 96 | + } else { |
| 97 | + value = JSON.stringify(value); |
| 98 | + } |
| 99 | + |
| 100 | + debug('SET %j %j %s %j', modelName, key, value, options); |
| 101 | + store[key] = new StoreItem(value, options && options.ttl); |
| 102 | + |
| 103 | + process.nextTick(callback); |
| 104 | +}; |
| 105 | + |
| 106 | +KeyValueMemoryConnector.prototype.expire = |
| 107 | +function(modelName, key, ttl, options, callback) { |
| 108 | + var store = this._getStoreForModel(modelName); |
| 109 | + |
| 110 | + if (!(key in store)) { |
| 111 | + return process.nextTick(function() { |
| 112 | + callback(new Error('Cannot expire unknown key ' + key)); |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + debug('EXPIRE %j %j %s', modelName, key, ttl || '(never)'); |
| 117 | + store[key].setTtl(ttl); |
| 118 | + process.nextTick(callback); |
| 119 | +}; |
| 120 | + |
| 121 | +KeyValueMemoryConnector.prototype.disconnect = function(callback) { |
| 122 | + if (this._cleanupTimer) |
| 123 | + clearInterval(this._cleanupTimer); |
| 124 | + this._cleanupTimer = null; |
| 125 | + process.nextTick(callback); |
| 126 | +}; |
| 127 | + |
| 128 | +function StoreItem(value, ttl) { |
| 129 | + this.value = value; |
| 130 | + this.setTtl(ttl); |
| 131 | +} |
| 132 | + |
| 133 | +StoreItem.prototype.isExpired = function() { |
| 134 | + return this.expires && this.expires <= Date.now(); |
| 135 | +}; |
| 136 | + |
| 137 | +StoreItem.prototype.setTtl = function(ttl) { |
| 138 | + if (ttl) { |
| 139 | + this.expires = Date.now() + ttl; |
| 140 | + } else { |
| 141 | + this.expires = undefined; |
| 142 | + } |
| 143 | +}; |
0 commit comments