|
|
@ -6,6 +6,7 @@ import { |
|
|
|
CacheDelDirection, |
|
|
|
CacheDelDirection, |
|
|
|
CacheGetType, |
|
|
|
CacheGetType, |
|
|
|
CacheListProp, |
|
|
|
CacheListProp, |
|
|
|
|
|
|
|
CacheMetaSplitter, |
|
|
|
} from '~/utils/globals'; |
|
|
|
} from '~/utils/globals'; |
|
|
|
const log = debug('nc:cache'); |
|
|
|
const log = debug('nc:cache'); |
|
|
|
|
|
|
|
|
|
|
@ -69,34 +70,55 @@ export default class RedisMockCacheMgr extends CacheMgr { |
|
|
|
} |
|
|
|
} |
|
|
|
return Promise.resolve(o); |
|
|
|
return Promise.resolve(o); |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
} catch (e) {} |
|
|
|
return Promise.resolve(res); |
|
|
|
const valueHelper = res.split(CacheMetaSplitter); |
|
|
|
} |
|
|
|
return Promise.resolve(valueHelper[0]); |
|
|
|
return Promise.resolve(res); |
|
|
|
|
|
|
|
} else if (type === CacheGetType.TYPE_STRING) { |
|
|
|
} else if (type === CacheGetType.TYPE_STRING) { |
|
|
|
return await this.client.get(key); |
|
|
|
return this.client.get(key).then((res) => { |
|
|
|
|
|
|
|
if (!res) { |
|
|
|
|
|
|
|
return res; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
const valueHelper = res.split(CacheMetaSplitter); |
|
|
|
|
|
|
|
return valueHelper[0]; |
|
|
|
|
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
log(`Invalid CacheGetType: ${type}`); |
|
|
|
log(`Invalid CacheGetType: ${type}`); |
|
|
|
return Promise.resolve(false); |
|
|
|
return Promise.resolve(false); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
// @ts-ignore
|
|
|
|
async set(key: string, value: any): Promise<any> { |
|
|
|
async set(key: string, value: any, skipPrepare = false): Promise<any> { |
|
|
|
if (typeof value !== 'undefined' && value) { |
|
|
|
if (typeof value !== 'undefined' && value) { |
|
|
|
log(`RedisMockCacheMgr::set: setting key ${key} with value ${value}`); |
|
|
|
log(`RedisMockCacheMgr::set: setting key ${key} with value ${value}`); |
|
|
|
|
|
|
|
|
|
|
|
if (typeof value === 'object') { |
|
|
|
if (typeof value === 'object') { |
|
|
|
if (Array.isArray(value) && value.length) { |
|
|
|
if (Array.isArray(value) && value.length) { |
|
|
|
return this.client.sadd(key, value); |
|
|
|
return this.client.sadd(key, value); |
|
|
|
} |
|
|
|
} |
|
|
|
const keyValue = await this.get(key, CacheGetType.TYPE_OBJECT); |
|
|
|
|
|
|
|
if (keyValue) { |
|
|
|
if (!skipPrepare) { |
|
|
|
|
|
|
|
// try to get old key value
|
|
|
|
|
|
|
|
const keyValue = await this.get(key, CacheGetType.TYPE_OBJECT); |
|
|
|
|
|
|
|
// prepare new key value
|
|
|
|
value = await this.prepareValue(value, this.getParents(keyValue)); |
|
|
|
value = await this.prepareValue(value, this.getParents(keyValue)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return this.client.set( |
|
|
|
return this.client.set( |
|
|
|
key, |
|
|
|
key, |
|
|
|
JSON.stringify(value, this.getCircularReplacer()), |
|
|
|
JSON.stringify(value, this.getCircularReplacer()), |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!skipPrepare) { |
|
|
|
|
|
|
|
// try to get old key value
|
|
|
|
|
|
|
|
const keyValue = await this.get(key, CacheGetType.TYPE_OBJECT); |
|
|
|
|
|
|
|
// prepare new key value
|
|
|
|
|
|
|
|
value = await this.prepareValue( |
|
|
|
|
|
|
|
value.toString(), |
|
|
|
|
|
|
|
this.getParents(keyValue), |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return this.client.set(key, value); |
|
|
|
return this.client.set(key, value); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
log(`RedisMockCacheMgr::set: value is empty for ${key}. Skipping ...`); |
|
|
|
log(`RedisMockCacheMgr::set: value is empty for ${key}. Skipping ...`); |
|
|
@ -245,7 +267,11 @@ export default class RedisMockCacheMgr extends CacheMgr { |
|
|
|
} |
|
|
|
} |
|
|
|
// set Get Key
|
|
|
|
// set Get Key
|
|
|
|
log(`RedisMockCacheMgr::setList: setting key ${getKey}`); |
|
|
|
log(`RedisMockCacheMgr::setList: setting key ${getKey}`); |
|
|
|
await this.set(getKey, JSON.stringify(value, this.getCircularReplacer())); |
|
|
|
await this.set( |
|
|
|
|
|
|
|
getKey, |
|
|
|
|
|
|
|
JSON.stringify(value, this.getCircularReplacer()), |
|
|
|
|
|
|
|
true, |
|
|
|
|
|
|
|
); |
|
|
|
// push Get Key to List
|
|
|
|
// push Get Key to List
|
|
|
|
listOfGetKeys.push(getKey); |
|
|
|
listOfGetKeys.push(getKey); |
|
|
|
} |
|
|
|
} |
|
|
@ -354,6 +380,7 @@ export default class RedisMockCacheMgr extends CacheMgr { |
|
|
|
await this.set( |
|
|
|
await this.set( |
|
|
|
key, |
|
|
|
key, |
|
|
|
JSON.stringify(preparedValue, this.getCircularReplacer()), |
|
|
|
JSON.stringify(preparedValue, this.getCircularReplacer()), |
|
|
|
|
|
|
|
true, |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
list.push(key); |
|
|
|
list.push(key); |
|
|
@ -368,17 +395,36 @@ export default class RedisMockCacheMgr extends CacheMgr { |
|
|
|
if (value && typeof value === 'object') { |
|
|
|
if (value && typeof value === 'object') { |
|
|
|
value[CacheListProp] = listKeys; |
|
|
|
value[CacheListProp] = listKeys; |
|
|
|
} else if (value && typeof value === 'string') { |
|
|
|
} else if (value && typeof value === 'string') { |
|
|
|
const keyHelper = value.split(CacheListProp); |
|
|
|
const metaHelper = value.split(CacheMetaSplitter); |
|
|
|
if (listKeys.length) { |
|
|
|
if (metaHelper.length > 1) { |
|
|
|
value = `${keyHelper[0]}${CacheListProp}${listKeys.join(',')}`; |
|
|
|
const keyVal = metaHelper[0]; |
|
|
|
|
|
|
|
const keyMeta = metaHelper[1]; |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
const meta = JSON.parse(keyMeta); |
|
|
|
|
|
|
|
meta[CacheListProp] = listKeys; |
|
|
|
|
|
|
|
value = `${keyVal}${CacheMetaSplitter}${JSON.stringify(meta)}`; |
|
|
|
|
|
|
|
} catch (e) { |
|
|
|
|
|
|
|
console.error( |
|
|
|
|
|
|
|
`RedisMockCacheMgr::prepareValue: keyValue meta is not JSON`, |
|
|
|
|
|
|
|
keyMeta, |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
throw new Error( |
|
|
|
|
|
|
|
`RedisMockCacheMgr::prepareValue: keyValue meta is not JSON`, |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
const meta = { |
|
|
|
|
|
|
|
[CacheListProp]: listKeys, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
value = `${value}${CacheMetaSplitter}${JSON.stringify(meta)}`; |
|
|
|
} |
|
|
|
} |
|
|
|
} else if (value) { |
|
|
|
} else if (value) { |
|
|
|
console.error( |
|
|
|
console.error( |
|
|
|
`RedisMockCacheMgr::prepareListKey: keyValue is not object or string`, |
|
|
|
`RedisMockCacheMgr::prepareValue: keyValue is not object or string`, |
|
|
|
value, |
|
|
|
value, |
|
|
|
); |
|
|
|
); |
|
|
|
throw new Error( |
|
|
|
throw new Error( |
|
|
|
`RedisMockCacheMgr::prepareListKey: keyValue is not object or string`, |
|
|
|
`RedisMockCacheMgr::prepareValue: keyValue is not object or string`, |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
return value; |
|
|
|
return value; |
|
|
|