Comments (8)
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.
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.
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.
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.
So do I need to do c.Set and then c.SaveFile?
from go-cache.
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.
@patrickmn I cannot get this to work
from go-cache.
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)
- Question HOT 1
- please create a new tag HOT 1
- sync.runtime_SemacquireMutex(0xc02c98fbf0?, 0x1f?, 0x3?) HOT 2
- Feature request: Renaming HOT 3
- Recommend a new go-cache package
- why dont exported shardedCache struct? HOT 1
- Question: why Get() function can't unlock immediately?like this: HOT 2
- cache will not expire when system time drift backward
- Float calculation error
- Question About Expired
- goroutine leak at cache.go:1079 - `runJanitor` HOT 5
- Example how to save cache in file and reload using gob an c.Items() ? HOT 1
- The efficiency of `Get` will decrease if too many `Set ` are executed HOT 1
- Introduce pattern-based Keys and Scan functions
- Proposal to Integrate SIEVE Eviction Algorithm
- support generic
- incompatibility Issue
- OnEvicted is not called for every expired value
- go incompatible version HOT 1
- Multiple caches, one metric HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-cache.