我正在尝试cache[redis]
包含一些内容的JavaScript对象
方法。我想以某种方式缓存此方法,以便以后可以独立使用它们。
.set()
:
在将数据保存到缓存时,我使用Deeploop并检查是否有任何值是一个函数,以便将其转换为字符串。
.get()
:
循环从缓存和JSON获取的数据。首先对Deeploop进行解析,然后检查值是否为函数语法,然后使用
new Function(`return ${value}`)
如果我调用的是一个匿名函数的缓存方法,它将返回一个.get()
中值为.set()
的缓存函数,而我将要接收的value[realmethodname]
未定义
处理方法的最佳方法,就像处理缓存中的数据一样。
.get()
方法get = async () => {
// error handling.
try {
// local variable.
let _getDetails, _datapipe
// variable assignment.
_getDetails = await getAsync(_id)
// only pase and loop if _getDetails
// is not empty else return empty data.
if (!_.isEmpty(_getDetails) && _.isString(_getDetails)) {
// variable assignment.
_getDetails = JSON.parse(_getDetails)
_datapipe = await Deeploop(_getDetails)
// deep loop over container
// and convert each string method to
// realmethod.
for (let {
parent,
key,
value
} of _datapipe) {
// if given value contain's function systax
if (_String(value).contains('function') && _String(value).contains(') {') && !_.isEmpty(parent[key])) {
console.log('-----ew--', value)
// assign new anonymus function.
parent[key] = JSON.parse(value)
}
}
console.log(_getDetails)
// return details.
return _getDetails
} else {
// return empty data.
return null
}
} catch (error) {
// report failure.
throw error
}
}
.set()
方法async () => {
// error handling.
try {
// local variable.
let _oldData, _dataContainer, _Cache
// variable assignment.
_dataContainer = {}
// if whatToStore and key is not empty.
if (!_.isEmpty(_id)) {
// only update with data if data is passed.
// else if whatToStore is null than set null
// to remove all data.
if (!_.isEmpty(whatToStore) || whatToStore) {
// check if data already exist.
// if exist than merge old data
// with new data.
_oldData = await getAsync(_id)
// if old data is not empty.
if (!_.isEmpty(_oldData)) {
// parse _oldData to JSON.
if (_.isString(_oldData) && _oldData.indexOf('{') === 0) {
// update oldData to container.
_dataContainer = JSON.parse(_oldData)
}
}
// if whatToStore is not object
// than store it as is.
if (!_.isObject(whatToStore)) {
// store data as is.
_Cache = await Client.set(_id, _dataContainer, Redis.print)
} else {
// update new Data.
Object.assign(_dataContainer, whatToStore)
// only store data if is not cyclic
// else report failure.
if (isCyclic(_dataContainer)) {
// local variable.
throw new Error('Cyclic dependencies found in dataContainer while storing data in cache..')
}
// deep loop over container
// and convert each method to
// string.
for (let {
parent,
key,
value
} of Deeploop(_dataContainer)) {
// if value of given value is function
// than convert given function into
// string.
if (_.isFunction(value)) parent[key] = _String(value).s
}
// loop over value and store each thing one by one.
// map data to Redis manager.
_Cache = await Client.set(_id, JSON.stringify(_dataContainer), Redis.print)
}
// if redis set doesn't contain any error.
if (_Cache) {
// return cached data.
return getAsync(_id)
}
} else {
// return cached data.
return getAsync(_id)
}
} else {
// local variable.
let error = new Error('[failure] Store require id for storing data.')
// report failure.
throw error
}
} catch (error) {
// report failure.
throw error
}
}
Debug: internal, implementation, error
ReferenceError: _BeforeBootEmit is not defined
at BeforeBootEmit (eval at _callee3$ (/Users/lefo-meo/WebstormProjects/servers/terminal_Os/currentrequest/_terminal/local/store/index.js:213:29), <anonymous>:3:13)
at _callee3$ (/Users/lefo-meo/WebstormProjects/servers/terminal_Os/currentrequest/_terminal/local/store/index.js:213:29)
at tryCatch (/Users/lefo-meo/WebstormProjects/servers/terminal_Os/currentrequest/node_modules/@babel/polyfill/node_modules/regenerator-runtime/runtime.js:45:40)
at Generator.invoke [as _invoke] (/Users/lefo-meo/WebstormProjects/servers/terminal_Os/currentrequest/node_modules/@babel/polyfill/node_modules/regenerator-runtime/runtime.js:271:22)
at Generator.prototype.(anonymous function) [as next] (/Users/lefo-meo/WebstormProjects/servers/terminal_Os/currentrequest/node_modules/@babel/polyfill/node_modules/regenerator-runtime/runtime.js:97:21)
at asyncGeneratorStep (/Users/lefo-meo/WebstormProjects/servers/terminal_Os/currentrequest/_terminal/local/store/index.js:26:24)
at _next (/Users/lefo-meo/WebstormProjects/servers/terminal_Os/currentrequest/_terminal/local/store/index.js:48:9)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:160:7)