Skip to content

piroyoung/go-cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-cache

Example

package main

import (
	"context"
	"fmt"
	"github.com/piroyoung/go-cache"
	"time"
)

func main() {
	ctx, cancel := context.WithCancel(context.Background())

	// Initialize
	c := cache.NewMemoryCache[string, string](time.Second)
	go c.FlushInterval(ctx, time.Minute)
	defer cancel()

	// Set
	c.Set("key", "value")

	// Get
	v, ok := c.Get("key")
	fmt.Printf("value: %s, ok: %t\n", v, ok)

	// Count
	count := c.Count()
	fmt.Printf("count: %d\n", count)
	cancel()

	// Wait for expiration
	time.Sleep(time.Second)

	// Get after expiration
	v, ok = c.Get("key")
	fmt.Printf("value: %s, ok: %t\n", v, ok)

	// Count after expiration
	count = c.Count()
	fmt.Printf("count: %d\n", count)

	// Flush
	c.Flush()

	// Count after flush
	count = c.Count()
	fmt.Printf("count: %d\n", count)

}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages