Giter Site home page Giter Site logo

go-cachemap's Introduction

GoDoc

go-cachemap

Golang local map cache

go-cachemap is a memory cache store implemeNted using inbuilt map. Cache can be used by multiple go routine as it is thread safe. go-cachemap is not intended to be a persistent cache store.

Installation

go get github.com/sameervitian/go-cachemap

Usage

package main

import (
	"log"
	"time"

	cache "github.com/sameervitian/go-cachemap"
)

func main() {
	option := cache.Option{
		TTL: 10, // in seconds
	}
	cacheStore := cache.New(&option)

	// Case 1
	cacheObj := cacheStore.NewCacheObject("foo")
	cacheObj.Set("bar")

	val, found := cacheObj.Get()
	//`cache hit. value is bar` will be logged
	if found {
		log.Printf("cache hit. value is %v", val.(string))
	} else {
		log.Printf("cache miss")
	}

	time.Sleep(time.Second * 11)
	val, found = cacheObj.Get()

	// `cache miss` will be logged
	if found {
		log.Printf("cache hit. value is %v", val.(string))
	} else {
		log.Printf("cache miss")
	}

	type Item struct {
		Name  string
		Price int64
	}

	// Case 2
	item := Item{Name: "foo", Price: 100}
	cacheObj = cacheStore.NewCacheObject("foo")
	cacheObj.Set(&item)
	val, found = cacheObj.Get()
	//` cache hit. value is main.Item{Name:"foo", Price:100}` will be logged
	if found {
		log.Printf("cache hit. value is %#v", *(val.(*Item)))
	} else {
		log.Printf("cache miss")
	}

	time.Sleep(time.Second * 2) //Sleep for 2 seconds
	val, found = cacheObj.Get()
	// ` cache hit. value is main.Item{Name:"foo", Price:100}` will be logged
	if found {
		log.Printf("cache hit. value is %#v", *(val.(*Item)))
	} else {
		log.Printf("cache miss")
	}

	time.Sleep(time.Second * 9) //Sleep for 9 seconds

	val, found = cacheObj.Get()
	// `cache miss` will be logged
	if found {
		log.Printf("cache hit. value is %#v", val.(*Item))
	} else {
		log.Printf("cache miss")
	}

	// Case 3
	item = Item{Name: "foo", Price: 100}
	cacheObj = cacheStore.NewCacheObject("foo", cache.CacheObjectOption{TTL: 2}) // overriding ttl to 2 seconds
	cacheObj.Set(&item)
	val, found = cacheObj.Get()
	//`cache hit. value is main.Item{Name:"foo", Price:100}` will be logged
	if found {
		log.Printf("cache hit. value is %#v", *(val.(*Item)))
	} else {
		log.Printf("cache miss")
	}
	time.Sleep(time.Second * 3) //Sleep for 3 seconds

	val, found = cacheObj.Get()
	// `cache miss` will be logged
	if found {
		log.Printf("cache hit. value is %#v", *(val.(*Item)))
	} else {
		log.Printf("cache miss")
	}

}

go-cachemap's People

Contributors

sameervitian avatar

Stargazers

Richard avatar  avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.