cache

package
v0.5.4 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package cache implement a memory-based LRU local cache

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConcurrencyLRUCache

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

ConcurrencyLRUCache is a memory-based LRU local cache, default total 16 segment to improve concurrent performance LRU is not real least recently used for the total cache,but just for each buket we just need a proper method to clear cache

func New

func New(maxEntries int) *ConcurrencyLRUCache

New create an instance of ConcurrencyLRUCache maxEntries the cache size, will to convert to (n/16+n%16>0?1:0)*16

func (*ConcurrencyLRUCache) DECR

func (cl *ConcurrencyLRUCache) DECR(key string, expireTime time.Duration) (int64, error)

DECR minus one to the value(must int64) of the key,if the key not exist, initialize with 0 and then minus one

func (*ConcurrencyLRUCache) Delete

func (cl *ConcurrencyLRUCache) Delete(key string)

Delete delete the value by key, no error returned

func (*ConcurrencyLRUCache) Get

func (cl *ConcurrencyLRUCache) Get(key string) (interface{}, error)

Get get the value of a cached element by key. If key do not exist, this function will return nil and an error msg

key:    The identity of an element
return:
    value:  the cached value, nil if key do not exist
    err:    error info, nil if value is not nil

func (*ConcurrencyLRUCache) INCR

func (cl *ConcurrencyLRUCache) INCR(key string, expireTime time.Duration) (int64, error)

INCR add one to the value(must int64) of the key , if the key not exist, initialize with 0 and then add one

func (*ConcurrencyLRUCache) Set

func (cl *ConcurrencyLRUCache) Set(key string, value interface{}, expireTime time.Duration) error

Set create or update an element using key

key:    The identity of an element
value:  new value of the element
expireTime:    expire time, positive int64 or -1 which means never overdue

func (*ConcurrencyLRUCache) SetIfNX

func (cl *ConcurrencyLRUCache) SetIfNX(key string, value interface{}, expireTime time.Duration) bool

SetIfNX if the key not exist or expired, will set the new value to cache and return true ,otherwise return false

Jump to

Keyboard shortcuts

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