Git Product home page Git Product logo

Comments (8)

Fyb3roptik avatar Fyb3roptik commented on May 18, 2024

It does work however if I do a Get() right after a Set(), so I know it is working, it is just when I run my script again it is not there

from go-cache.

patrickmn avatar patrickmn commented on May 18, 2024

I modified your example so that I could run it, and it works as expected:

package main

import (
	"fmt"
	"github.com/patrickmn/go-cache"
	"log"
	"time"
)

type Session struct{
	foo string
}

func (s *Session) Create() *Session {
	return s
}

func main() {
	iteration := 1
	c := cache.New(0, 10*time.Minute)
	session := &Session{}

	// Not set? Set it
	if _, found := c.Get(fmt.Sprintf("mykey-%d", iteration)); found == false {
		log.Println("DEBUG: SESSION: NOT FOUND")
		session = session.Create()
		c.Set(fmt.Sprintf("mykey-%d", iteration), session, cache.NoExpiration)
	}

	if sess, found := c.Get(fmt.Sprintf("mykey-%d", iteration)); found {
		log.Println("DEBUG: FOUND!")
		session = sess.(*Session)
	}
}

Output:

2019/02/07 20:11:37 DEBUG: SESSION: NOT FOUND
2019/02/07 20:11:37 DEBUG: FOUND!

If what you mean is that you are terminating the process and then running it again, then: go-cache is memory-only, and so will disappear when the process exits. If you want disk persistence, you should save and then load the cache using c.SaveFile(fname string) and c.LoadFile(fname string), or, ideally, use the cache in a long-lived process.

from go-cache.

Fyb3roptik avatar Fyb3roptik commented on May 18, 2024

Yes that is the issue! Your docu says that is deprecated though, so can you give me a working example of doing it that way? Great library BTW!

from go-cache.

patrickmn avatar patrickmn commented on May 18, 2024

Sure:

items := c.Items()
// serialize using encoding/gob, encoding/json or similar, and write to file

next time:

// deserialize the gob/json file into items
c := c.NewFrom(exp, ci, items)

The deprecated methods basically do exactly this using encoding/gob for serialization/deserialization. Even though the methods are deprecated I'm not likely to ever remove them; they just might not work with certain non-gob.Register()'ed types.

Thank you :) Glad you like it!

from go-cache.

Fyb3roptik avatar Fyb3roptik commented on May 18, 2024

So do I need to do c.Set and then c.SaveFile?

from go-cache.

Fyb3roptik avatar Fyb3roptik commented on May 18, 2024

Can you give me an end to end example of it working with SaveFile and LoadFile? It is not working for me

from go-cache.

Fyb3roptik avatar Fyb3roptik commented on May 18, 2024

@patrickmn I cannot get this to work

from go-cache.

yann0917 avatar yann0917 commented on May 18, 2024

Can you give me an end to end example of it working with SaveFile and LoadFile? It is not working for me

you need to register gob name use gob.Register(), if not, when you use c.LoadFile() will get error like this gob: name not registered for interface: "xxxx".

from go-cache.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.