cache

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

README

isc-gobase/cache

cache is an in-memory key:value store/cache similar to memcached that is suitable for applications running on a single machine.

Any object can be stored, for a given duration or forever, and the cache can be safely used by multiple goroutines.

Installation

go get go get github.com/isyscore/isc-gobase

Usage

import (
	"github.com/isyscore/isc-gobase/cache"
)

func main() {
    // Create a cache with a default expiration time of forever
	c := cache.New()
	// Create a cache with an expiration time 
	c1 :=  NewWithExpiration(1 * time.Second)
	c1.Set("foo1","Vector")
	// Create a cache with an expiration time , and which
    // purges expired items every cleanup time
	c2 := NewWithExpirationAndCleanupInterval(1 * time.Second,1 * time.Second)
	c2.Set("foo2","Vector")
	
    // Set the value of the key "foo" to "bar"
	c.Set("foo", "bar")
    // Set the value of the key "baz" to 42
	c.Set("baz",42)
	// Set the value of the key "struct" to struct
    v1 := struct {
        Name string
        Age int
        }{
            "Vector.Ku",
            29,
        }
	c.Set("struct",v1)
	//Add a item to a lit
	c.AddItem("testList", "item1")
	c.AddItem("testList","item2")
	//Also,the cache allow you set an item with index,it will replace the item to new one.
	c.SetItem("testList","item3",2)
	//Get the string associated with the key "foo" from the cache
	if foo, found := c.Get("foo");found {
	    fmt.Println(foo)	
    }
	//Get the list associated with the key "testList" from the cache
	ret := c.GetItem("testList")
	for _,item := range ret {
		fmt.Println(item)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New() *cache

func NewWithCleanupInterval

func NewWithCleanupInterval(cleanupInterval time.Duration) *cache

func NewWithExpiration

func NewWithExpiration(expiration time.Duration) *cache

func NewWithExpirationAndCleanupInterval

func NewWithExpirationAndCleanupInterval(defaultExpiration, cleanupInterval time.Duration) *cache

Types

type Cache

type Cache *cache

type Item

type Item struct {
	//data
	Data any
	//ttl time.UnixNano.Expiration of Item,if it is -1,it will be not Expired
	Ttl int64
}

func (*Item) Expired

func (item *Item) Expired() bool

Jump to

Keyboard shortcuts

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