cache

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

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

Go to latest
Published: May 29, 2018 License: MIT Imports: 2 Imported by: 7

README

Go Cache

MIT License Go Report Card Go Doc

Overview

Go Cache is a simple package to provide thread-safe in-memory caching in Go. It's my attempt to practice some of the patterns/philosophies found in these articles:

The code is tested, although standard caveats of using interface{} apply.
Personally, I'd recommend copying this package and replacing var T interface{} with whatever type you need to cache. I may add code generation in the future to make that process easier.

Example

package main

import (
        "fmt"
	"time"
        "github.com/zpatrick/go-cache"
)

func main() {
	c := cache.New()
	
	// empty the cache every hour
	c.ClearEvery(time.Hour)
	
	// add some items
	c.Set("key1", 1)
	c.Set("key2", 2)
	
	// add some items that will expire after 5 minutes
	c.Set("key3", 3, cache.Expire(time.Minute*5))
	c.Set("key4", 4,  cache.Expire(time.Minute*5))

	fmt.Println(c.Get("key1"))
	fmt.Println(c.Get("key2"))
	
	for _, key := range c.Keys() {
		fmt.Println(key)
	}
	
	for key, val := range c.Items() {
		fmt.Printf("%s: %v", key, val)
	}
	
	c.Delete("key1")
	
	if val, ok := c.GetOK("key2"); ok {
		fmt.Println(val)
	}
	
	c.Clear()
}

License

This work is published under the MIT license. Please see the LICENSE file for details.

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
}

A Cache is a thread-safe store for fast item storage and retrieval

func New

func New() *Cache

New returns an empty cache

func (*Cache) Clear

func (c *Cache) Clear()

Clear removes all entries from the cache

func (*Cache) ClearEvery

func (c *Cache) ClearEvery(d time.Duration) *time.Ticker

ClearEvery clears the cache on a loop at the specified interval

func (*Cache) Delete

func (c *Cache) Delete(key string)

Delete removes an entry from the cache at the specified key. If no entry exists at the specified key, no action is taken

func (*Cache) Get

func (c *Cache) Get(key string) T

Get retrieves an entry at the specified key

func (*Cache) GetOK

func (c *Cache) GetOK(key string) (T, bool)

GetOK retrieves an entry at the specified key. Returns bool specifying if the entry exists

func (*Cache) Items

func (c *Cache) Items() map[string]T

Items retrieves all entries in the cache

func (*Cache) Keys

func (c *Cache) Keys() []string

Keys retrieves a sorted list of all keys in the cache

func (*Cache) Set

func (c *Cache) Set(key string, val T, options ...SetOption)

Set will set the val into the cache at the specified key. If an entry already exists at the specified key, it will be overwritten. The options param can be used to perform logic after the entry has be inserted.

type SetOption

type SetOption func(c *Cache, key string, val T)

A SetOption will perform logic after a set action completes

func Expire

func Expire(expiry time.Duration) SetOption

Expire is a SetOption that will cause the entry to expire after the specified duration

type T

type T interface{}

T is a type for cache value

Jump to

Keyboard shortcuts

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