cache

package
v1.5.5 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2024 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

This section is empty.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

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

func (*Cache) AddItem added in v1.4.2

func (c *Cache) AddItem(key string, value ...any) error

func (*Cache) Cap added in v1.4.2

func (c *Cache) Cap() int

func (*Cache) Clean added in v1.4.2

func (c *Cache) Clean()

func (*Cache) DeleteExpired added in v1.4.2

func (c *Cache) DeleteExpired()

func (*Cache) Get added in v1.4.2

func (c *Cache) Get(key string) (any, bool)

Get an item from the cache.Returns the item or nil, and a bool indicating whether the key was found

func (*Cache) GetHash added in v1.4.2

func (c *Cache) GetHash(key, subKey string) (any, bool)

GetHash get a hash value from the cache.Returns the hashes or nil, and a bool indicating whether the key was found

func (*Cache) GetItem added in v1.4.2

func (c *Cache) GetItem(key string) []any

GetItem return an array of points or nil

func (*Cache) GetItemByIndex added in v1.4.2

func (c *Cache) GetItemByIndex(key string, idx int) any

GetItemByIndex return a value of Type is T or nil

func (*Cache) Remove added in v1.4.2

func (c *Cache) Remove(key string)

func (*Cache) RemoveHash added in v1.4.2

func (c *Cache) RemoveHash(key, subKey string) error

func (*Cache) RemoveItem added in v1.4.2

func (c *Cache) RemoveItem(key string, idx int) error

RemoveItem an item from the cache. Does nothing if the key is not in the cache.

func (*Cache) Set added in v1.4.2

func (c *Cache) Set(key string, value any) error

Set Add an item to the cache,replacing any existing item. note key is primary key

func (*Cache) SetHash added in v1.4.2

func (c *Cache) SetHash(key, subKey string, value any) error

SetHash Add an item to the cache,replacing any existing item. note key and subKey is primary key

func (*Cache) SetItem added in v1.4.2

func (c *Cache) SetItem(key string, idx int, value any) error

SetItem set or replace a value of items by index

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