ttlcache

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

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

Go to latest
Published: Mar 13, 2021 License: MIT Imports: 2 Imported by: 0

README

TTL cache

Build Status Go Report Card GoDoc Licence MIT

Simple thread-safe expiring in-memory cache.

Usage example

package main

import (
	"fmt"
	"time"

	"github.com/jsageryd/ttlcache"
)

func main() {
	cache := ttlcache.New(5 * time.Second)
	cache.Set("foo", "bar")
	if value, ok := cache.Get("foo"); ok {
		fmt.Println("Value:", value) // (type assert value as needed)
	} else {
		fmt.Println("Key not found")
	}
}

Note

For every new key that is set a goroutine is spawned to expire that key after the timeout. This keeps the implementation clean and simple. Already existing keys keep the same goroutine if they are set again before they have expired. While it should not generally be an issue to spawn many thousand goroutines, if you have a very large amount of things to cache, this package is not for you. So keep that in mind. Consider Qcache.

Licence

Copyright (c) 2016-2019 Johan Sageryd j@1616.se

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Overview

Package ttlcache provides a cache whose items expire after set TTL.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	Expire(key interface{})
	ExpireAll()
	Get(key interface{}) (interface{}, bool)
	Set(key interface{}, value interface{})
}

Cache is a key-value cache for arbitrary values

func New

func New(ttl time.Duration) Cache

New returns a new cache. ttl is the time to live for its items.

Jump to

Keyboard shortcuts

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