gocache

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2026 License: MIT Imports: 10 Imported by: 21

README

gocache - simple key value cache adapter for golang

Go Reference Go Go Report Card GitHub release (latest SemVer)

Installation

go get -d github.com/morkid/gocache

In Memory cache example

package main
import (
    "time"
    "fmt"
    "github.com/morkid/gocache"
)

func main() {
    config := gocache.InMemoryCacheConfig{
        ExpiresIn: 10 * time.Second,
    }

    adapter := gocache.NewInMemoryCache(config)
    adapter.Set("foo", "bar")

    if adapter.IsValid("foo") {
        value, err := adapter.Get("foo")
        if nil != err {
            fmt.Println(err.Error())
        } else if value != "bar" {
            fmt.Println("value not equals to bar")
        }
        adapter.Clear("foo")
    }
}

Disk cache example

package main
import (
    "os"
    "time"
    "fmt"
    "github.com/morkid/gocache"
)

func main() {
    config := gocache.DiskCacheConfig{
        Directory: os.TempDir(),
        ExpiresIn: 10 * time.Second,
    }

    adapter := gocache.NewDiskCache(config)
    adapter.Set("foo", "bar")

    if adapter.IsValid("foo") {
        value, err := adapter.Get("foo")
        if nil != err {
            fmt.Println(err.Error())
        } else if value != "bar" {
            fmt.Println("value not equals to bar")
        }
        adapter.Clear("foo")
}

Custom cache adapter

You can create your custom cache adapter by implementing the AdapterInterface:

type AdapterInterface interface {

    Set(key string, value string) error

    Get(key string) (string, error)
    
    IsValid(key string) bool
    
    Clear(key string) error
    
    ClearPrefix(keyPrefix string) error
    
    ClearAll() error
}

License

Published under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AdapterInterface

type AdapterInterface interface {
	// Set cache with key
	Set(key string, value string) error
	// Get cache by key
	Get(key string) (string, error)
	// IsValid check if cache is valid
	IsValid(key string) bool
	// Clear clear cache by key
	Clear(key string) error
	// ClearPrefix clear cache by key prefix
	ClearPrefix(keyPrefix string) error
	// Clear all cache
	ClearAll() error
}

AdapterInterface interface

func NewDiskCache

func NewDiskCache(config DiskCacheConfig) AdapterInterface

NewDiskCache store cache to file on disk

func NewInMemoryCache

func NewInMemoryCache(config InMemoryCacheConfig) AdapterInterface

NewInMemoryCache new instance of inMemoryCache

type DiskCacheConfig

type DiskCacheConfig struct {
	Directory string
	ExpiresIn time.Duration
}

DiskCacheConfig struct

type InMemoryCacheConfig

type InMemoryCacheConfig struct {
	ExpiresIn time.Duration
}

InMemoryCacheConfig struct

Jump to

Keyboard shortcuts

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