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

Convert cache to json and vice versa #2

Open
AmosSzust opened this issue Oct 22, 2022 · 9 comments
Open

Convert cache to json and vice versa #2

AmosSzust opened this issue Oct 22, 2022 · 9 comments

Comments

@AmosSzust
Copy link

Hi

What about adding support for converting the cache to json and vice versa? This way I can save/load it to/from a file for when the server restarts.

convertCacheToJson(): string {
    const json = Object.fromEntries(this.itemKeyToItemValueWrapperMap);
    return JSON.stringify(json);
}

convertJsonToCach(json: string): number {
    const tempMap = new Map(Object.entries(JSON.parse(json)));
    this.clear();    
    for (const entry of tempMap.entries()) {      
      this.itemKeyToItemValueWrapperMap.set(...);
    }
    return this.getItemCount();
}

I saw that itemKeyToItemValueWrapperMap is readOnly and I didn't want to break your code/pattern so I was thinking of using a temporary map but got lost in the middle. What do you think?
Thanks

@pksilen
Copy link
Owner

pksilen commented Oct 27, 2022

Hi,
Thanks for the request. I will add the support for converting JSON and back. I try to do this in a couple of days. Let you know when it is available

@AmosSzust
Copy link
Author

Hi, any update on this one? Thanks

@pksilen
Copy link
Owner

pksilen commented Nov 9, 2022

Hi,
These functions are available now in version 1.3.0

@AmosSzust
Copy link
Author

Thanks. I noticed that exporting is done for all items but importing is separated into permanent and expiry. This is ok with me because I only have expiry items but

  1. My expiry items have different ttl so when importing them, they will all have the same ttl.
  2. If and when I will have permanent and expiry items, after exporting, I won't have a way to separate them for importing.

Maybe the export should save also the cache properties so there will be one exporting and one importing for all? What do you think? Again, thank you very much for your work.

@pksilen
Copy link
Owner

pksilen commented Nov 11, 2022

Hi,
I can make a modification to export also the current TTL values which will then used in import

@pksilen
Copy link
Owner

pksilen commented Nov 13, 2022

There is a 1.4.0 version now available

@AmosSzust
Copy link
Author

Thanks, I will check it

A question if I may: Do you have an idea how can I check the export while working/debugging locally?
In vscode when I press the red stop button, the server shuts down without running any of the exit events so neither of the following works.

    .on('exit', async () => {
        saveCache();
        console.log('Exited via exit');
    })
    .on('SIGTERM', async () => {
        saveCache();
        console.log('Exited via sigterm');
    })
    .on('SIGINT', async () => {
        saveCache();
        console.log('Exited via sigint');
    });

@pksilen
Copy link
Owner

pksilen commented Nov 16, 2022

Hi,
I don't know for sure, because I don't know the context and I don't use VSCODE myself much.
But, it seems that the red button will issue a SIGKILL which will immediately terminate a process and cannot be handled.
You can try to run you app in terminal and press ctrl-c to issue a SIGINT signal.
I am not sure if the async is correct in your signal handlers, did you mean to use "await saveCache();". This is because async is redundant, if you are not using await inside the function.

@AmosSzust
Copy link
Author

Regarding async you are right, it was there because of a previous code that needed it.
Regarding running from a terminal, for some reason when I do that, I keep getting a message that port X is in use, no matter the port I try, running it through the launcher works with no problem.

Anyway, I used an exception to go to that part of the code and it seems to work, at least the import/export part, I didn't check the expiry mechanism itself.

Thanks

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

2 participants