Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
asaccool committed Dec 15, 2022
1 parent c0341da commit e2b6e6e
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 15 deletions.
14 changes: 11 additions & 3 deletions cache/MemoryCache.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@

const constants = require("../moduleConstants");

function MemoryCache() {
function MemoryCache(useWeakRef) {
let storage = {};
const self = this;

self.get = function (key, callback) {
if(typeof key !== "string"){
throw new Error("Keys should be strings");
}

let value = storage[key];
if(value && useWeakRef){
value = value.deref();
}
if(callback){
callback(undefined, storage[key])
callback(undefined, value);
}
return storage[key];
return value;
};

self.put = function (key, value, callback) {
if(typeof key !== "string"){
throw new Error("Keys should be strings");
}
if(useWeakRef){
value = value ? new WeakRef(value) : value;
}
storage[key] = value;
if(callback){
callback(undefined, true)
Expand Down
14 changes: 12 additions & 2 deletions cache/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,20 @@ function getCacheForVault(storeName, lifetime) {
}

function getMemoryCache(storeName){
return stores[storeName] = new MemoryCache(storeName);
return stores[storeName] = new MemoryCache();
}

function getWeakRefMemoryCache(storeName){
let cache = stores[storeName];
if(!cache){
cache = new MemoryCache(true);
stores[storeName] = cache;
}
return cache;
}

module.exports = {
getCacheForVault,
getMemoryCache
getMemoryCache,
getWeakRefMemoryCache
}
79 changes: 70 additions & 9 deletions resolver/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const keySSISpace = require("opendsu").loadApi("keyssi");

let {ENVIRONMENT_TYPES, KEY_SSIS} = require("../moduleConstants.js");
const {getWebWorkerBootScript, getNodeWorkerBootScript} = require("./resolver-utils");
const cache = require("../cache");
let dsuCache = cache.getWeakRefMemoryCache("mainDSUsCache");

const getResolver = (options) => {
options = options || {};
Expand Down Expand Up @@ -36,7 +38,13 @@ const createDSU = (templateKeySSI, options, callback) => {
}

const keySSIResolver = getResolver(options);
keySSIResolver.createDSU(templateKeySSI, options, callback);
keySSIResolver.createDSU(templateKeySSI, options, (err, dsuInstance) => {
if (err) {
return OpenDSUSafeCallback(callback)(createOpenDSUErrorWrapper(`Failed to create DSU instance`, err));
}

addDSUInstanceInCache(dsuInstance, callback);
});
};

const createDSUx = (domain, ssiType, options, callback) => {
Expand Down Expand Up @@ -300,14 +308,36 @@ const loadDSU = (keySSI, options, callback) => {
return loadFallbackDSU(keySSI, options, callback);
}

const keySSIResolver = getResolver(options);
keySSIResolver.loadDSU(keySSI, options, (err, dsuInstance) => {
const loadDSU = (addInCache) => {

const keySSIResolver = getResolver(options);
keySSIResolver.loadDSU(keySSI, options, (err, dsuInstance) => {
if (err) {
return OpenDSUSafeCallback(callback)(createOpenDSUErrorWrapper(`Failed to load DSU`, err));
}

if (addInCache) {
return addDSUInstanceInCache(dsuInstance, callback);
}

callback(undefined, dsuInstance);
});
};

if (typeof options === 'object' && options !== null && options.skipCache) {
return loadDSU(false);
}

keySSI.getAnchorId((err, cacheKey) => {
if (err) {
return OpenDSUSafeCallback(callback)(createOpenDSUErrorWrapper(`Failed to load DSU`, err));
return callback(err);
}

callback(undefined, dsuInstance);
});
const cachedDSU = dsuCache.get(cacheKey);
if (cachedDSU) {
return getLatestDSUVersion(cachedDSU, callback);
}
loadDSU(true);
})
};

/*
Expand Down Expand Up @@ -470,10 +500,41 @@ const getRemoteHandler = (dsuKeySSI, remoteURL, presentation) => {
};

function invalidateDSUCache(dsuKeySSI, callback) {
//there is a cache at Bar level...
return callback();
try {
if (typeof dsuKeySSI === "string") {
dsuKeySSI = keySSISpace.parse(dsuKeySSI);
}
} catch (e) {
console.error(e);
}
dsuKeySSI.getAnchorId((err, cacheKey) => {
if (err) {
return callback(err);
}
if (cacheKey) {
delete dsuCache.set(cacheKey, undefined);
}

callback();
});
}

function addDSUInstanceInCache(dsuInstance, callback) {
dsuInstance.getKeySSIAsObject((err, keySSI) => {
if (err) {
return OpenDSUSafeCallback(callback)(createOpenDSUErrorWrapper(`Failed to retrieve keySSI`, err));
}
keySSI.getAnchorId((err, cacheKey) => {
if (err) {
return callback(err);
}
dsuCache.set(cacheKey, dsuInstance);
callback(undefined, dsuInstance);
});
});
}


module.exports = {
createDSU,
createDSUx,
Expand Down
2 changes: 1 addition & 1 deletion tests/resolver/basic/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ async function mockEnvironment({ domain, volume, folder } = {}) {
const dc = require('double-check');

const cache = require('../../../cache');
const vault = cache.getCacheForVault('DSUs');
const vault = cache.getCacheForVault('mainDSUsCache');

if (!domain) {
domain = 'default';
Expand Down

0 comments on commit e2b6e6e

Please # to comment.