@tobiasberg/graphql-hooks-memcache
Version:
In memory cache for graphql-hooks
160 lines (135 loc) • 4.7 kB
JavaScript
import LRU from 'tiny-lru';
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);
if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
if (enumerableOnly) symbols = symbols.filter(function (sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
});
keys.push.apply(keys, symbols);
}
return keys;
}
function _objectSpread2(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
if (i % 2) {
ownKeys(Object(source), true).forEach(function (key) {
_defineProperty(target, key, source[key]);
});
} else if (Object.getOwnPropertyDescriptors) {
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
} else {
ownKeys(Object(source)).forEach(function (key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
}
return target;
}
//
// Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
var OFFSET_BASIS_32 = 2166136261;
function fnv1aString(string) {
var hash = OFFSET_BASIS_32;
for (var i = 0; i < string.length; i++) {
hash ^= string.charCodeAt(i); // 32-bit FNV prime: 2**24 + 2**8 + 0x93 = 16777619
// Using bitshift for accuracy and performance. Numbers in JS suck.
hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
}
return hash >>> 0;
}
function fnv1a(input) {
if (typeof input === 'string') {
return fnv1aString(input);
} else {
throw new Error('input must be a string');
}
}
function generateKey(keyObj) {
return fnv1a(JSON.stringify(keyObj)).toString(36);
}
function memCache(_temp) {
var _ref = _temp === void 0 ? {} : _temp,
_ref$size = _ref.size,
size = _ref$size === void 0 ? 100 : _ref$size,
_ref$ttl = _ref.ttl,
ttl = _ref$ttl === void 0 ? 0 : _ref$ttl,
_ref$debug = _ref.debug,
debug = _ref$debug === void 0 ? false : _ref$debug,
initialState = _ref.initialState;
var lru = LRU(size, ttl);
var debugLru = debug ? LRU(size, ttl) : undefined;
if (initialState) {
Object.keys(initialState).map(function (k) {
lru.set(k, initialState[k]);
});
}
return {
get: function get(keyObj) {
return lru.get(generateKey(keyObj));
},
rawGet: function rawGet(rawKey) {
return lru.get(rawKey);
},
rawToKey: function rawToKey(rawKey) {
if (!debugLru) {
throw 'not allowed unless in debug mode';
}
return debugLru.get(rawKey);
},
set: function set(keyObj, data) {
var key = generateKey(keyObj);
lru.set(key, data);
if (debugLru) {
debugLru.set(key, keyObj);
}
},
delete: function _delete(keyObj) {
return lru.delete(generateKey(keyObj));
},
clear: function clear() {
return lru.clear();
},
keys: function keys() {
return lru.keys();
},
getInitialState: function getInitialState() {
return lru.keys().reduce(function (initialState, key) {
var _objectSpread2$1;
return _objectSpread2(_objectSpread2({}, initialState), {}, (_objectSpread2$1 = {}, _objectSpread2$1[key] = lru.get(key), _objectSpread2$1));
}, {});
}
};
}
export default memCache;