newcache

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 13, 2021 License: MIT Imports: 5 Imported by: 0

README

NewCache

Latest Release GoDoc

Golang cache lib, easy to register your cache flush daemon.

Installation

To install new-cache, simply run:

go get github.com/newtorn/new-cache

To compile it from source:

cd $GOPATH/src/github.com/newtorn/new-cache
go get -u -v
go build && go test -v

Example

Get Started
package main

import (
	"github.com/newtorn/new-cache"
	"fmt"
	"time"
)

// User represents a data entity, we can store into new-get-started.
type User struct {
	Username string
	Password string
}

func main() {
	// Call Singleton for the first time will create get-started.
	cache := newcache.Singleton()

	// We will put a new item in the get-started. It will expire after
	// not being accessed via SetEx(key) for more than 5 seconds.
	user := User{Username: "Jack", Password: "123456"}
	cache.SetEx(user.Username, &user, 5*time.Second)

	// Let's retrieve the item from the get-started.
	val, ok := cache.Get(user.Username)
	if ok {
		fmt.Println("Found value in get-started:", val)
	} else {
		fmt.Println("Not found retrieving value from get-started")
	}

	// Wait for the item to expire in get-started.
	time.Sleep(6 * time.Second)
	val, ok = cache.Get(user.Username)
	if !ok {
		fmt.Println("Item is not cached (anymore).")
	}

	// Set another item that never expires.
	cache.SetEx(user.Username, &user, 0)

	// Set another item that with default expiration.
	cache.Set(user.Username, &user)

	// Remove the item from the get-started.
	cache.Del("someKey")

	// Wipe the entire get-started table.
	cache.Flush()
}

To run this example, go to examples/get-started/ and run:

go run main.go
Cache Register Flush Daemon
package main

import (
	"github.com/newtorn/new-cache"
	"fmt"
	"time"
)

// User represents a data entity, we can store into new-get-started.
type User struct {
	Username string
	Password string
}

func main() {
	// Call Singleton for the first time will create get-started.
	cache := newcache.Singleton()

	// We will put a new item in the get-started. It will expire after
	// not being accessed via SetEx(key) for more than 5 seconds.
	user := User{Username: "Jack", Password: "123456"}
	cache.SetEx(user.Username, &user, 5*time.Second)

	// Let's retrieve the item from the get-started.
	val, ok := cache.Get(user.Username)
	if ok {
		fmt.Println("Found value in get-started:", val)
	} else {
		fmt.Println("Not found retrieving value from get-started")
	}

	// Wait for the item to expire in get-started.
	time.Sleep(6 * time.Second)
	val, ok = cache.Get(user.Username)
	if !ok {
		fmt.Println("Item is not cached (anymore).")
	}

	// Set another item that never expires.
	cache.SetEx(user.Username, &user, 0)

	// Set another item that with default expiration.
	cache.Set(user.Username, &user)

	// Remove the item from the get-started.
	cache.Del("someKey")

	// Wipe the entire get-started table.
	cache.Flush()
}

To run this example, go to examples/cache-flush/ and run:

go run main.go

You can find a few more examples here. Also see our test-cases in cache_test.go for further working examples.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InitOnce

func InitOnce(conf CacheConfig)

InitOnce inits cache service configuration only once.

Types

type CacheConfig

type CacheConfig struct {
	FlushTimerTime    time.Duration
	CleanupInterval   time.Duration
	DefaultExpiration time.Duration
}

CacheConfig sets for cache service daemon.

type CacheFlushDaemon

type CacheFlushDaemon interface {
	Done(ctx context.Context) (done <-chan interface{})
	LoadKeys(ctx context.Context, value interface{}) (cacheKeys []string)
	LoadValues(ctx context.Context) (values []interface{})
}

CacheFlushDaemon represents a flush daemon will be registered, should do implement of all methods.

type CacheService

type CacheService interface {
	Set(k string, v interface{})
	SetEx(k string, v interface{}, ex time.Duration)
	Get(k string) (v interface{}, ok bool)
	Del(k string)
	GetValues() []interface{}
	GetByDefault(k string, v interface{}) interface{}
	Item() map[string]cache.Item
	Flush()
	Register(ctx context.Context, daemon CacheFlushDaemon)
}

CacheService represents a cache service interface, package for go-cache.

func Singleton

func Singleton() CacheService

Singleton creates a cache service singleton by lazy mode.

Directories

Path Synopsis
examples
cache-flush command
get-started command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL