sgcache

package module
v0.0.0-...-d1038e7 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2022 License: MIT Imports: 3 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Simple go cache - in memory cache with expiration

Usage
import (
  "time"
  "github.com/lielamurs/simple-go-cache"
)

func main () {
  cache := sgcache.New(time.Duration(time.Second), 5000)
  cache.Set("foo", "bar", time.Minute)
  value, exists := cache.Get("foo")
  cache.Delete("foo")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func New

func New(cleanupInterval time.Duration, sizeLimit uint64) *Cache

Return a new cache with a cleanup interval and size limit in bytes. Automatic cleanup is started to delete expired items.

func (*Cache) Close

func (c *Cache) Close()

Stop the cleanup process

func (*Cache) Delete

func (c *Cache) Delete(key string)

Delete removes item from cache.

func (*Cache) Get

func (c *Cache) Get(key string) (entry interface{}, found bool)

Get returns a cache entry for the provided key.

func (*Cache) Set

func (c *Cache) Set(key string, data interface{}, ttl time.Duration)

Set adds new item to cache with a ttl duration. If adding the item causes cache to exceed defined memory limit no action is performed.

type Item

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

type MemoryCache

type MemoryCache interface {
	Get(key string) (entry interface{}, found bool)
	Set(key string, data interface{}, ttl time.Duration)
	Delete(key string)
	Close()
}

Jump to

Keyboard shortcuts

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