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

[RFC]: Add ability to cache arbitrary classes in Service Cache #6282

Closed
1 task done
cannikin opened this issue Aug 24, 2022 · 0 comments · Fixed by #6082
Closed
1 task done

[RFC]: Add ability to cache arbitrary classes in Service Cache #6282

cannikin opened this issue Aug 24, 2022 · 0 comments · Fixed by #6082
Assignees

Comments

@cannikin
Copy link
Member

cannikin commented Aug 24, 2022

Summary

This isn't even merged yet: #6082 but I thought of an enhancement. Right now you can only cache things that survive a round trip through JSON.stringify() and JSON.parse(). But if you followed a certain convention in your a class, you could cache arbitrary class instances as well.

Motivation

I'd love to be able to cache anything and count on it coming back out of the cache in the same format. This is a problem with things like Dates or functions, but especially for instances of custom classes.

Detailed proposal

If a class can serialize itself, and provide a way to instantiate a new instance of itself from the serialized form, then it can survive the JSON.serialize() process.

I propose that the class provide a instance function, toCache(), which provides a serialized version of all of it's internal data/state, as well as an additional key that identifies the name of the class itself. Likewise, it provides a class function fromCache() that can create an instance of itself when given the serialized version.

Here's an example implementation:

class Person {
  static fromCache(...args) {
    return new Person(...args)
  }

  constructor(name, age) {
    this.name = name
    this.age = age
  }

  toCache() {
    return { class: 'Person', name: this.name, age: this.age }
  }
}

So what the cache can do is look for a key class on an object returned from the cache. If it's found, it uses that property and calls fromCache() on a class with that name, passing the remaining arguments to it.

Using this in a cache() call looks like:

const person = new Person('Rob', 44)
person // => Person {name: 'Rob', age: 44}
JSON.stringify(person) // => '{name: "Rob", age: 44}'

const cachedPerson = cache('user-1', () => rob)
cachedPerson // => Person {name: 'Rob', age: 44}

Are you interested in working on this?

  • I'm interested in working on this
@redwoodjs-bot redwoodjs-bot bot added this to Main Aug 25, 2022
@redwoodjs-bot redwoodjs-bot bot moved this to Triage in Main Aug 25, 2022
@cannikin cannikin self-assigned this Sep 12, 2022
@jtoar jtoar linked a pull request Sep 27, 2022 that will close this issue
@jtoar jtoar removed this from Main Sep 27, 2022
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

Successfully merging a pull request may close this issue.

1 participant