Forráskód Böngészése

remove original items from cache as well when getting updated

Pouria Ezzati 1 éve
szülő
commit
f3966217e1

+ 11 - 2
server/queries/domain.queries.js

@@ -53,6 +53,7 @@ async function add(params) {
   const domain = await knex("domains").where("id", id).first();
 
   if (env.REDIS_ENABLED) {
+    redis.remove.domain(existingDomain);
     redis.remove.domain(domain);
   }
 
@@ -60,17 +61,25 @@ async function add(params) {
 }
 
 async function update(match, update) {
+  // if the domains' adddress is changed,
+  // make sure to delete the original domains from cache 
+  let domains = []
+  if (env.REDIS_ENABLED && update.address) {
+    domains = await knex("domains").select("*").where(match);
+  }
+  
   await knex("domains")
     .where(match)
     .update({ ...update, updated_at: utils.dateToUTC(new Date()) });
 
-  const domains = await knex("domains").select("*").where(match);
+  const updated_domains = await knex("domains").select("*").where(match);
 
   if (env.REDIS_ENABLED) {
     domains.forEach(redis.remove.domain);
+    updated_domains.forEach(redis.remove.domain);
   }
 
-  return domains;
+  return updated_domains;
 }
 
 function normalizeMatch(match) {

+ 2 - 1
server/queries/link.queries.js

@@ -262,7 +262,8 @@ async function update(match, update) {
     update.password = await bcrypt.hash(update.password, salt);
   }
 
-  // make sure to delete the original link from cache if its adddress or domain is changed
+  // if the links' adddress or domain is changed,
+  // make sure to delete the original links from cache 
   let links = []
   if (env.REDIS_ENABLED && (update.address || update.domain_id)) {
     links = await knex("links").select('*').where(match);

+ 5 - 4
server/queries/user.queries.js

@@ -63,7 +63,7 @@ async function add(params, user) {
 }
 
 async function update(match, update, methods) {
-  const user = await knex.transaction(async function(trx) {
+  const { user, updated_user } = await knex.transaction(async function(trx) {
     const query = trx("users");
     Object.entries(match).forEach(([key, value]) => {
       query.andWhere(key, ...(Array.isArray(value) ? value : [value]));
@@ -80,16 +80,17 @@ async function update(match, update, methods) {
     }
     
     await updateQuery.update({ ...update, updated_at: utils.dateToUTC(new Date()) });
-    const updatedUser = await trx("users").where("id", user.id).first();
+    const updated_user = await trx("users").where("id", user.id).first();
 
-    return updatedUser;
+    return { user, updated_user };
   });
 
   if (env.REDIS_ENABLED && user) {
     redis.remove.user(user);
+    redis.remove.user(updated_user);
   }
 
-  return user;
+  return updated_user;
 }
 
 async function remove(user) {