gocache

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2021 License: MIT Imports: 10 Imported by: 20

README

gocache - simple key value cache adapter for golang

Go Build Status Go Report Card

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
	// ClearPrefix 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