geocache

package module
v0.0.0-...-521b336 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2016 License: MIT Imports: 4 Imported by: 0

README

geocache GoDoc Go Report Card Build Status

geocache is an in-memory cache that is suitable for geolocation based applications. It uses geolocation as a key for storing items. You can specify range on initialization and thats it! You can store any object, it uses interface.

Installation

go get github.com/melihmucuk/geocache

Usage

geolocation cache


import (
	"fmt"
	"time"

	"github.com/melihmucuk/geocache"
)

func main() {
	c, err := geocache.NewCache(5*time.Minute, 30*time.Second, geocache.WithIn1KM)
	geoPoint := geocache.GeoPoint{Latitude: 40.9887, Longitude: 28.7817}
	if err != nil {
		fmt.Println("Error: ", err.Error())
	} else {
		c.Set(geoPoint, "helloooo", 2*time.Minute)
		v1, ok1 := c.Get(geocache.GeoPoint{Latitude: 41.2, Longitude: 29.3})
		v2, ok2 := c.Get(geocache.GeoPoint{Latitude: 41.2142, Longitude: 29.4234})
		v3, ok3 := c.Get(geocache.GeoPoint{Latitude: 40.9858, Longitude: 28.7852})
		v4, ok4 := c.Get(geocache.GeoPoint{Latitude: 40.9827, Longitude: 28.7883})
		fmt.Println(v1, ok1)
		fmt.Println(v2, ok2)
		fmt.Println(v3, ok3)
		fmt.Println(v4, ok4)
	}
}

outputs:

<nil>, false
<nil>, false
helloooo, true
helloooo, true
Information

You can specify 8 different range. More info can be found here.

  • WithIn11KM

The first decimal place is worth up to 11.1 km eg: 41.3, 29.6

  • WithIn1KM

The second decimal place is worth up to 1.1 km eg: 41.36, 29.63

  • WithIn110M

The third decimal place is worth up to 110 m eg: 41.367, 29.631

  • WithIn11M

The fourth decimal place is worth up to 11 m eg: 41.3674, 29.6316

  • WithIn1M

The fifth decimal place is worth up to 1.1 m eg: 41.36742, 29.63168

  • WithIn11CM

The sixth decimal place is worth up to 0.11 m eg: 41.367421, 29.631689

  • WithIn11MM

The seventh decimal place is worth up to 11 mm eg: 41.3674211, 29.6316893

  • WithIn1MM

The eighth decimal place is worth up to 1.1 mm eg: 41.36742115, 29.63168932

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache struct manages items, expirations and clean ups

func NewCache

func NewCache(expiration, cleanUpInterval time.Duration, withInRange Range) (*Cache, error)

NewCache creates new Cache with params and returns pointer of Cache and error cleanUpInterval used for deleting expired objects from cache.

func (*Cache) Flush

func (c *Cache) Flush()

Flush deletes all cached items

func (*Cache) Get

func (c *Cache) Get(position GeoPoint) (interface{}, bool)

Get gets object from cache with given geopoint

func (*Cache) ItemCount

func (c *Cache) ItemCount() int

ItemCount returns cached items count

func (*Cache) Items

func (c *Cache) Items() map[GeoPoint]Item

Items returns cached items

func (*Cache) Set

func (c *Cache) Set(position GeoPoint, value interface{}, expiration time.Duration)

Set adds object to cache with given geopoint

func (*Cache) StopCleanUp

func (c *Cache) StopCleanUp()

StopCleanUp stops clean up process.

type GeoPoint

type GeoPoint struct {
	Latitude  float64
	Longitude float64
}

GeoPoint specifies point that used as key of cache

type Item

type Item struct {
	Object     interface{}
	Expiration int64
}

Item struct keeps cache value and expiration time of object

type Range

type Range int32

Range specifies range of cache

const (
	// WithIn11KM The first decimal place is worth up to 11.1 km
	// eg: 41.3, 29.6
	WithIn11KM Range = 1 + iota

	// WithIn1KM The second decimal place is worth up to 1.1 km
	// eg: 41.36, 29.63
	WithIn1KM

	// WithIn110M The third decimal place is worth up to 110 m
	// eg: 41.367, 29.631
	WithIn110M

	// WithIn11M The fourth decimal place is worth up to 11 m
	// eg: 41.3674, 29.6316
	WithIn11M

	// WithIn1M The fifth decimal place is worth up to 1.1 m
	// eg: 41.36742, 29.63168
	WithIn1M

	// WithIn11CM The sixth decimal place is worth up to 0.11 m
	// eg: 41.367421, 29.631689
	WithIn11CM

	// WithIn11MM The seventh decimal place is worth up to 11 mm
	// eg: 41.3674211, 29.6316893
	WithIn11MM

	// WithIn1MM The eighth decimal place is worth up to 1.1 mm
	// eg: 41.36742115, 29.63168932
	WithIn1MM
)

Jump to

Keyboard shortcuts

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