hashmap

package
v0.0.0-...-28fadcf Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2018 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package hashmap provides an key/value store.

Package hashmap provides an key/value store.

Index

Constants

View Source
const (
	//NoExpiration  :For use with functions that take an expiration time.
	NoExpiration time.Duration = -1
	// DefaultExpiration  For use with functions that take an expiration time. Equivalent to
	// passing in the same expiration duration as was given to New()
	// when the cache was created (e.g. 5 minutes.)
	DefaultExpiration time.Duration = 0
)
View Source
const (
	//DefaultCleanUpInterval clean the cache expied items
	DefaultCleanUpInterval time.Duration = 60 * 1e9
)
View Source
const Maxbuckets = 128

Maxbuckets define buckets count.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache to be exposed

func New

func New(defaultExpiration time.Duration) *Cache

New :Return a new cache with a given default expiration duration If the expiration duration is less than one (or NoExpiration), the items in the cache never expire (by default), and must be deleted manually.

func (Cache) Add

func (c Cache) Add(k string, x interface{}, d time.Duration) error

Add an item to the cache only if an item doesn't already exist for the given key, or if the existing item has expired. Returns an error otherwise.

func (Cache) Decrement

func (c Cache) Decrement(k string, n int64) error

Decrement an item of type int, int8, int16, int32, int64, uintptr, uint, uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the item's value is not an integer, if it was not found, or if it is not possible to decrement it by n. To retrieve the decremented value, use one of the specialized methods, e.g. DecrementInt64.

func (Cache) DecrementFloat

func (c Cache) DecrementFloat(k string, n float64) error

Decrement an item of type float32 or float64 by n. Returns an error if the item's value is not floating point, if it was not found, or if it is not possible to decrement it by n. Pass a negative number to decrement the value. To retrieve the decremented value, use one of the specialized methods, e.g. DecrementFloat64.

func (Cache) DecrementFloat32

func (c Cache) DecrementFloat32(k string, n float32) (float32, error)

Decrement an item of type float32 by n. Returns an error if the item's value is not an float32, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementFloat64

func (c Cache) DecrementFloat64(k string, n float64) (float64, error)

Decrement an item of type float64 by n. Returns an error if the item's value is not an float64, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementInt

func (c Cache) DecrementInt(k string, n int) (int, error)

Decrement an item of type int by n. Returns an error if the item's value is not an int, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementInt16

func (c Cache) DecrementInt16(k string, n int16) (int16, error)

Decrement an item of type int16 by n. Returns an error if the item's value is not an int16, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementInt32

func (c Cache) DecrementInt32(k string, n int32) (int32, error)

Decrement an item of type int32 by n. Returns an error if the item's value is not an int32, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementInt64

func (c Cache) DecrementInt64(k string, n int64) (int64, error)

Decrement an item of type int64 by n. Returns an error if the item's value is not an int64;if it was not found,return -n, If there is no error, the decremented value is returned.

func (Cache) DecrementInt8

func (c Cache) DecrementInt8(k string, n int8) (int8, error)

Decrement an item of type int8 by n. Returns an error if the item's value is not an int8, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementUint

func (c Cache) DecrementUint(k string, n uint) (uint, error)

Decrement an item of type uint by n. Returns an error if the item's value is not an uint, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementUint16

func (c Cache) DecrementUint16(k string, n uint16) (uint16, error)

Decrement an item of type uint16 by n. Returns an error if the item's value is not an uint16, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementUint32

func (c Cache) DecrementUint32(k string, n uint32) (uint32, error)

Decrement an item of type uint32 by n. Returns an error if the item's value is not an uint32, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementUint64

func (c Cache) DecrementUint64(k string, n uint64) (uint64, error)

Decrement an item of type uint64 by n. Returns an error if the item's value is not an uint64, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementUint8

func (c Cache) DecrementUint8(k string, n uint8) (uint8, error)

Decrement an item of type uint8 by n. Returns an error if the item's value is not an uint8, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) DecrementUintptr

func (c Cache) DecrementUintptr(k string, n uintptr) (uintptr, error)

Decrement an item of type uintptr by n. Returns an error if the item's value is not an uintptr, or if it was not found. If there is no error, the decremented value is returned.

func (Cache) Delete

func (c Cache) Delete(k string)

Delete an item from the cache. Does nothing if the key is not in the cache.

func (Cache) DeleteExpired

func (c Cache) DeleteExpired()

Delete all expired items from the cache.

func (Cache) Flush

func (c Cache) Flush()

Delete all items from the cache.

func (Cache) Get

func (c Cache) Get(k string) (interface{}, bool)

Get an item from the cache. Returns the item or nil, and a bool indicating

func (Cache) Getall

func (c Cache) Getall(buff *bytes.Buffer) error

func (Cache) Getallkey

func (c Cache) Getallkey(buff *bytes.Buffer) (int, error)

func (Cache) Increment

func (c Cache) Increment(k string, n int64) error

Increment an item of type int, int8, int16, int32, int64, uintptr, uint, uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the item's value is not an integer

func (Cache) IncrementFloat

func (c Cache) IncrementFloat(k string, n float64) error

Increment an item of type float32 or float64 by n. Returns an error if the item's value is not floating point, if it was not found, or if it is not possible to increment it by n.

func (Cache) IncrementFloat32

func (c Cache) IncrementFloat32(k string, n float32) (float32, error)

Increment an item of type float32 by n. Returns an error if the item's value is not an float32, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementFloat64

func (c Cache) IncrementFloat64(k string, n float64) (float64, error)

Increment an item of type float64 by n. Returns an error if the item's value is not an float64, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementInt

func (c Cache) IncrementInt(k string, n int) (int, error)

Increment an item of type int by n. Returns an error if the item's value is not an int, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementInt16

func (c Cache) IncrementInt16(k string, n int16) (int16, error)

Increment an item of type int16 by n. Returns an error if the item's value is not an int16, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementInt32

func (c Cache) IncrementInt32(k string, n int32) (int32, error)

Increment an item of type int32 by n. Returns an error if the item's value is not an int32, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementInt64

func (c Cache) IncrementInt64(k string, n int64) (int64, error)

Increment an item of type int64 by n. Returns an error if the item's value is not an int64, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementInt8

func (c Cache) IncrementInt8(k string, n int8) (int8, error)

Increment an item of type int8 by n. Returns an error if the item's value is not an int8, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementUint

func (c Cache) IncrementUint(k string, n uint) (uint, error)

Increment an item of type uint by n. Returns an error if the item's value is not an uint, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementUint16

func (c Cache) IncrementUint16(k string, n uint16) (uint16, error)

Increment an item of type uint16 by n. Returns an error if the item's value is not an uint16, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementUint32

func (c Cache) IncrementUint32(k string, n uint32) (uint32, error)

Increment an item of type uint32 by n. Returns an error if the item's value is not an uint32, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementUint64

func (c Cache) IncrementUint64(k string, n uint64) (uint64, error)

Increment an item of type uint64 by n. Returns an error if the item's value is not an uint64, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementUint8

func (c Cache) IncrementUint8(k string, n uint8) (uint8, error)

Increment an item of type uint8 by n. Returns an error if the item's value is not an uint8, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) IncrementUintptr

func (c Cache) IncrementUintptr(k string, n uintptr) (uintptr, error)

Increment an item of type uintptr by n. Returns an error if the item's value is not an uintptr, or if it was not found. If there is no error, the incremented value is returned.

func (Cache) ItemCount

func (c Cache) ItemCount() int

Returns the number of items in the cache. This may include items that have expired, but have not yet been cleaned up. Equivalent to len(c.Items()).

func (Cache) Items

func (c Cache) Items() map[string]Item

Returns the items in the cache. This may include items that have expired, but have not yet been cleaned up. If this is significant, the Expiration fields of the items should be checked. Note that explicit synchronization is needed to use a cache and its corresponding Items() return value at the same time, as the map is shared.

func (Cache) Load

func (c Cache) Load(r io.Reader) error

Add (Gob-serialized) cache items from an io.Reader, excluding any items with keys that already exist (and haven't expired) in the current cache.

NOTE: This method is deprecated in favor of c.Items()

func (Cache) LoadFile

func (c Cache) LoadFile(fname string) error

Load and add cache items from the given filename, excluding any items with keys that already exist in the current cache.

NOTE: This method is deprecated in favor of c.Items()

func (Cache) OnEvicted

func (c Cache) OnEvicted(f func(string, interface{}))

Sets an (optional) function that is called with the key and value when an item is evicted from the cache. (Including when it is deleted manually, but not when it is overwritten.) Set to nil to disable.

func (Cache) Replace

func (c Cache) Replace(k string, x interface{}, d time.Duration) error

func (Cache) Save

func (c Cache) Save(w io.Writer) (err error)

Write the cache's items (using Gob) to an io.Writer.

NOTE: This method is deprecated in favor of c.Items()

func (Cache) SaveFile

func (c Cache) SaveFile(fname string) error

Save the cache's items to the given filename, creating the file if it doesn't exist, and overwriting it if it does.

NOTE: This method is deprecated in favor of c.Items()

func (Cache) Set

func (c Cache) Set(k string, x interface{}, d time.Duration)

Add an item to the cache, replacing any existing item. If the duration is 0 (DefaultExpiration), the cache's default expiration time is used. If it is -1 (NoExpiration), the item never expires.

type Dbs

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

Dbs :This is multiple cashes, namely by preventing write locks of the entire cache when an item is added. As of the time of writing, the overhead of selecting buckets results in cache operations being about twice as slow as for the standard cache with small total cache sizes, and faster for larger ones.

See cache_test.go for a few benchmarks.

func DBSetup

func DBSetup(defaultExpiration, cleanupInterval time.Duration) *Dbs

DBSetup init dbs

func GetDB

func GetDB() *Dbs

GetDB returns the defaultDBs

func (Dbs) Add

func (sc Dbs) Add(k string, x interface{}, d time.Duration) error

func (Dbs) Decrement

func (sc Dbs) Decrement(k string, n int64) error

func (Dbs) DecrementInt64

func (sc Dbs) DecrementInt64(k string, n int64) (int64, error)

func (Dbs) Delete

func (sc Dbs) Delete(k string)

func (Dbs) DeleteExpired

func (sc Dbs) DeleteExpired()

func (Dbs) Flush

func (sc Dbs) Flush()

func (Dbs) Get

func (sc Dbs) Get(k string) (interface{}, bool)

func (Dbs) Getallkey

func (sc Dbs) Getallkey(buff *bytes.Buffer) (int, error)

func (Dbs) Hdel

func (sc Dbs) Hdel(k, f string)

func (Dbs) Hdestroy

func (sc Dbs) Hdestroy(k string)

func (Dbs) Hexist

func (sc Dbs) Hexist(k, f string) bool

func (Dbs) Hget

func (sc Dbs) Hget(k, f string) (interface{}, error)

func (Dbs) Hgetall

func (sc Dbs) Hgetall(k string, buf *bytes.Buffer) error

func (Dbs) Hmget

func (sc Dbs) Hmget(k string, pairs [][]byte) (data [][]byte, err error)

func (Dbs) Hmset

func (sc Dbs) Hmset(k string, pairs [][]byte)

func (Dbs) Hset

func (sc Dbs) Hset(k, f string, x interface{}, d time.Duration)

func (Dbs) Increment

func (sc Dbs) Increment(k string, n int64) error

func (Dbs) IncrementFloat

func (sc Dbs) IncrementFloat(k string, n float64) error

func (Dbs) IncrementInt64

func (sc Dbs) IncrementInt64(k string, n int64) (int64, error)

func (Dbs) Items

func (sc Dbs) Items() []map[string]Item

Returns the items in the cache. This may include items that have expired, but have not yet been cleaned up.

func (Dbs) Replace

func (sc Dbs) Replace(k string, x interface{}, d time.Duration) error

func (Dbs) Set

func (sc Dbs) Set(k string, x interface{}, d time.Duration)

type Hcache

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

type Item

type Item struct {
	Object     interface{}
	Expiration int64
}

Item store object

func (Item) Expired

func (item Item) Expired() bool

Expired Returns true if the item has expired.

Jump to

Keyboard shortcuts

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