glc

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2023 License: MIT Imports: 6 Imported by: 0

README

Go Local Cache

GoDoc Go Report Card

Go Local Cache provides a simple cache mechanism for storing locally.
Go Local Cache currently only supports in-memory cache, but will also support file cache.

Installation

go get github.com/emahiro/glc

Usage

in memory cache
mc := glc.NewMemoryCache(glc.DefaultMemoryCacheExpires)

// Set
if err := mc.Set("cacheKey", []byte('hoge')); err != nil {
    log.Fatal(err)
}

// Get

data := mc.Get("cacheKey")
file cache

Usage is similar to in memory cache.
Go Local Cache creates tmp directory for file cache.

LICENSE

MIT

Documentation

Overview

Package glc is provides the local cache which is stored in memoroy or file. This package creates `tmp` directory in case of using file cache.

Example:

mc := glc.NewMemoryCache(glc.DefaultMemoryCacheExpires)

// Set
if err := mc.Set("cacheKey", []byte('hoge')); err != nil {
	log.Fatal(err)
}

// Get
data := mc.Get("cacheKey")

Index

Constants

View Source
const (
	// DefaultMemoryCacheExpires is 60 seconds
	DefaultMemoryCacheExpires = 60 * time.Second
)

Variables

This section is empty.

Functions

This section is empty.

Types

type FileCache

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

FileCache is cache data in local file.

func NewFileCache added in v1.2.0

func NewFileCache(prefix string) (*FileCache, error)

NewFileCache returns FileCache pointer. If there is no temp directory named prefix, create temp directory for storing cache.

func (*FileCache) Get

func (c *FileCache) Get(key string) []byte

Get returns a data or nil. If cache in local file is nil or is not setted key, this returns nil.

func (*FileCache) Set

func (c *FileCache) Set(key string, src []byte) error

Set create a new file which is witten data as []byte. When it sets a new cache data, create a `{{ $key }}.cache` file, and overwrite it if cache file is exist.

type Item

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

Item has cache item and expiration field.

type MemoryCache

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

MemoryCache is cache data in memory which has duration.

func NewMemoryCache

func NewMemoryCache(d time.Duration) *MemoryCache

NewMemoryCache creates a new MemoryCache for given a its expires as time.Duration.

func (*MemoryCache) Get

func (c *MemoryCache) Get(key string) []byte

Get returns a item or nil. If cache in local is nil or expiration date of the cache is earlier, this returns nil.

func (*MemoryCache) Set

func (c *MemoryCache) Set(key string, src []byte) error

Set add a new data for cache with a new key or replace an exist key.

Jump to

Keyboard shortcuts

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