go-generics-cache icon indicating copy to clipboard operation
go-generics-cache copied to clipboard

Feature: Add save and load functions in cache

Open alidevhere opened this issue 2 years ago • 6 comments

Hii !! It would be greate to have data persistance. I would like to contribute to add save and load feature. I am thinking of using gob to save / retreive data to / from file.

alidevhere avatar May 29 '23 17:05 alidevhere

@alidevhere Thanks What do you think for the usage?

Code-Hex avatar May 29 '23 17:05 Code-Hex

@Code-Hex I need to add graceful shutdown for my server. If my server restarts it should save whatever in cache and reload when started again. So if a make little fix and restart it doesnot affect other processes going on.

alidevhere avatar May 29 '23 17:05 alidevhere

@alidevhere I see. sounds good.

I think that the development direction should be as follows:

  1. Create new options like WithSave, WithRestore.
  2. WithSave may have a functional parameter like func(cache Interface) {} and this function will be called received context.Done.
  3. WithRestore may have a functional parameter like func(cache Interface) {} and this function will be initialized.

Is it possible to contribute in this way?

Code-Hex avatar May 30 '23 15:05 Code-Hex

@Code-Hex Yeah sure!!

I have created a proof of concept pull request, I tried to Add Save and Load functions for cache. Let me share my findings with you:

  1. I used gob and gob needs Struct fields to be exported/public to encode and decode them.
  2. It is 100% working with simple cache. But for other algorithm caches we use datastructures like linked list etc. In these cases it is not as straight forward as for simple cache. We will have to do some manual work.

@Code-Hex I will do some more work on it with directions you gave.

Thanks

alidevhere avatar May 31 '23 12:05 alidevhere

@alidevhere Thanks your initial PoC but I'm sorry I cannot accept the PR 🙏

Code-Hex avatar May 31 '23 12:05 Code-Hex

@alidevhere Thanks your initial PoC but I'm sorry I cannot accept the PR 🙏

Yes, I made this PR just to show POC i will make a complete PR later

alidevhere avatar May 31 '23 12:05 alidevhere