cache

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

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

Go to latest
Published: Feb 22, 2022 License: Apache-2.0 Imports: 4 Imported by: 0

README

go-mem-cache

A simple in-memory cache with HTTP REST interface.

Cache

usage
package main

import (
	"fmt"
	"time"

	memcache "github.com/abhi195/go-mem-cache"
)

func main() {

	// creating cache with default 30 minutes of TTL which
	// evicts expired entries every 60 minutes
	mc := memcache.New()

	// alternatively you can use NewWith() to
	// create cache with required values
	nmc := memcache.NewWith(1*time.Minute, 2*time.Minute)

	// set key, value
	mc.Set("foo", "bar")

	// get key
	v, exist := mc.Get("foo")
	if exist {
		fmt.Println(v)
	}
}

REST Interface

APIs
# Cache get
GET         /api/v1/cache/{key}
# Cache post
POST        /api/v1/cache/{key} # with plain text body for value
# HealthCheck
GET         /health
# Metrics
GET         /metrics

Please import go-mem-cache.postman_collection.json for sample requests.

Starting HTTP Server
  • Natively
go build -o ./out/cache-server server/*
./out/cache-server
  • Using provided docker image
docker pull abhi195/go-mem-cache
docker run --rm -i -p 8080:8080 abhi195/go-mem-cache
  • Building docker image and running
docker build -t go-mem-cache .
docker run --rm -i -p 8080:8080 go-mem-cache

Future work

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MemCache

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

MemCache is in-memory cache

func New

func New() *MemCache

Returns new MemCache with default itemTTL and cleanupInterval

func NewWith

func NewWith(itemTTL time.Duration, cleanupInterval time.Duration) *MemCache

Returns new MemCache with mentioned itemTTL and cleanupInterval

func (*MemCache) Get

func (mc *MemCache) Get(k string) (string, bool)

Get a value for key k

func (*MemCache) Set

func (mc *MemCache) Set(k string, v string)

Set value v for key k

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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