Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shared cache mechanism for performance via dependency injection #12

Open
codemeasandwich opened this issue Nov 29, 2022 · 1 comment
Open

Comments

@codemeasandwich
Copy link
Owner

When having full control over the application stack. Batching multiple requests into one message, indicates that there is no order dependency on the encapsulated messages. Caching database queries in this scenario is a good optimisation. E.g. DataLoader

I propose adding a .shared mechanism. In the way .stream is exposed

Example

// create as normal
const ChatSchema = new mongoose.schema({ text: String });
const Chat = moduleStream('Chat', ChatSchema);

// create a shared ref
const ChatWithCachedQueries = Chat.shared()

// use shared ref
workTodo({Chat:ChatWithCachedQueries})

function async workTodo(modules){
 // this find call will be Cached, so if the same query is call on this shared module. 
 // It will return the same value instead of hitting the database again
 const chats = await modules.Chat.find({})
 // ... do something with chats
}
// reuse shared ref
workSomeOtherWork({Chat:ChatWithCachedQueries})

Question: What if the DB object is change in App?.. = this change will be reflected to all instances

@codemeasandwich
Copy link
Owner Author

codemeasandwich commented Nov 30, 2022

Question: What if the DB object is change in App?

This code would allow each function to be given its own unique version of the document that they could overwrite without affecting any of the other references

const docDB = { a:1, aa:10, b:{c:5} }

function getMeARef(docDB){
    const changes = { }
    const user = { }
    for (const key in docDB) {
        let val = null
        if("object" === typeof docDB[key]){
            val = getMeARef(docDB[key])
        }
       Object.defineProperty(user, key, { //<- This object is called a "property descriptor".

    enumerable: true,
    configurable: true,
          //Alternatively, use: `get() {}`
          get: function() {
             if(changes.hasOwnProperty(key)){
                 return changes[key]
             }
            return val || docDB[key];
          },
          //Alternatively, use: `set(newValue) {}`
          set: function(newValue) {
            if(changes[key] !== docDB[key]){
                return changes[key] = newValue;
            }
          }
        });
    }
    return user
}

var genRef = getMeARef.bind(null,docDB)
var x = genRef()
var z = genRef()
console.log(JSON.stringify({x,z,docDB}))
x.a = 8
docDB.aa = 20
console.log(JSON.stringify({x,z,docDB}))

Still need a lot of work. e.g.

  1. Array & complex data types like Date.
  2. If I key is added or removed from the document
  3. handling functions white population

-> However this approach would be a gateway into allowing the subset of changes as an update <-

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant