Skip to content
Snippets Groups Projects
Verified Commit 1aab0e8c authored by Hadrien RENAUD's avatar Hadrien RENAUD
Browse files

GroupModel use cache

parent bb5286d3
No related branches found
No related tags found
No related merge requests found
......@@ -192,6 +192,7 @@ const context = async ({ req }): Promise<Context> => {
// /!\ FOR DEVELOPMENT ONLY: use the one in the ldap_credentials.json file (imported by config.ts)
// for production, replace with a "publicUser" or "notLoggedInUser" or something.
let uid = AuthorizationModel.PUBLICUSER;
uid = "hadrien.renaud";
console.log("Responding to graphql request...");
console.log(`
......
......@@ -25,22 +25,23 @@ export class GroupModel {
*/
constructor(contextUser: string) {
this.contextUser = contextUser;
this.cache = new Cache<Promise<Group>, string>(this.newGroup);
}
protected contextUser: string;
protected cache: Cache<Group, string>;
protected cache: Cache<Promise<Group>, string>;
/**
* @memberof GraphQL.GroupModel#
* @function getGroup
* @function newGroup
* @summary Fonction qui renvoie un groupe donné.
* @arg {string} gid - Identifiant demandé.
* @return {Promise(Group)} Renvoie le groupe dont l'identifiant est 'gid'
* @async
* @rights connectedOrOnplatal
*/
async getGroup(gid: string): Promise<Group> {
async newGroup(gid: string): Promise<Group> {
let data = await knex.select('type').from('groups').where('gid', gid);
if(data.length > 0) {
......@@ -57,6 +58,11 @@ export class GroupModel {
return null;
}
async getGroup(gid: string): Promise<Group> {
console.log("Getgroup called: ", gid);
return this.cache.get(gid);
}
/**
* @memberof GraphQL.GroupModel#
* @function getSimpleGroup
......
......@@ -30,6 +30,7 @@ export class SimpleGroup extends Group {
*/
constructor(gid: string) {
super(gid);
console.log("New Simplegroup: ", gid)
}
/**
......@@ -60,6 +61,7 @@ export class SimpleGroup extends Group {
*/
protected async fetchData(): Promise<boolean> {
try {
console.log("LDAP peek for:", this.gid)
let data = await LDAP_Group.peek(this.gid);
//console.log(data);
if (data.gid !== this.gid) {
......
......@@ -14,19 +14,22 @@ export class Cache<A, K> {
instances = new Map<K, A>();
aConstructor: new (k: K) => A;
aConstructor: (k: K) => A;
constructor(aConstructor: new (k: K) => A) {
constructor(aConstructor: (k: K) => A) {
console.log("Creating cache");
this.aConstructor = aConstructor;
}
get(k: K){
async get(k: K){
if (this.instances.has(k))
return this.instances.get(k);
else {
const a = new this.aConstructor(k);
console.log("Creating new group in cache:", k, this.instances.keys());
const a = this.aConstructor(k);
this.instances.set(k, a);
console.log("Cache set :", this.instances.keys());
return a;
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment