func NewpushRawDatabaseCache(c *DatabaseConfig, dbreader pushRawDatabaseReader, dbwriter pushRawDatabaseWriter) (*pushRawDatabaseCache, error) { cacheSize := 1024 flushPeriod := 600 leastDirty := 128 if c != nil { cacheSize = c.CacheSize flushPeriod = int(c.EverySec) leastDirty = c.LeastDirty } cdb := new(pushRawDatabaseCache) cdb.dbreader = dbreader cdb.dbwriter = dbwriter pspflusher := &pspFlusher{cdb: cdb} dpflusher := &dpFlusher{cdb: cdb} cdb.pspCache = cache.New(cacheSize, leastDirty, time.Duration(flushPeriod)*time.Second, pspflusher) cdb.dpCache = cache.New(cacheSize, leastDirty, time.Duration(flushPeriod)*time.Second, dpflusher) // We will flush them manually cdb.srvSub2Dp = cache.New(cacheSize, -1, time.Duration(0)*time.Second, nil) cdb.srv2Psp = cache.New(cacheSize, -1, time.Duration(0)*time.Second, nil) return cdb, nil }
func Test_UpdateTTL(t *testing.T) { c := cache.New("var") _, err := c.UpdateTTL("key999", 20000) assert.Nil(t, err, "no error") c.CloseConnection() }
func Test_GetData(t *testing.T) { c := cache.New("var") x, err := c.GetData("key999") assert.Nil(t, err, "no error") assert.Equal(t, "value999", x) c.CloseConnection() }
func Test_SetData(t *testing.T) { c := cache.New("var") for i := 0; i < 1000; i++ { key_str := fmt.Sprint("key", i) val_str := fmt.Sprint("value", i) err := c.SetData(key_str, val_str, 1000000) assert.Nil(t, err, "Hope we don't have a error") } c.CloseConnection() }
func main() { signalChan := make(chan os.Signal, 1) signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM) addr := "" cs := NewTcpServer(addr) //default expiration time is 5 minites, cleanup time 30 seconds c := cache.New(5*time.Minute, 30*time.Second) if ok, _ := cs.Start(c); ok != 0 { log.Println("go-cache started failed") os.Exit(1) } log.Println("go-cache started and listening on 9891") <-signalChan fmt.Println("receive signalChan") cs.Stop() os.Exit(0) }
func Test_DeleteData(t *testing.T) { c := cache.New("var") err := c.DeleteData("key999") assert.Nil(t, err, "no error") c.CloseConnection() }
Next int64 HavePrevious bool HaveNext bool Filtered []*story.Story Unfiltered []*story.Story } type UserProfile struct { Interesting WordCounts Uninteresting WordCounts } var stories = newFifo(MaxStories) // Sessions are held in the DB and are cached in memory var sessions = cache.New(1024, 5*time.Minute, readSession, saveSession) var storyCh = make(chan []*story.Story) var readCh = make(chan userStory, 8) // Add stories so that they are available to all users func AddStories(s []*story.Story) { storyCh <- s } // Mark a story as read func MarkRead(w http.ResponseWriter, req *http.Request, storyid int64) { sessid, ok := sessionCookie(w, req) if ok { readCh <- userStory{sessionid: sessid, storyid: storyid}