roc

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2020 License: MIT Imports: 5 Imported by: 2

README

Roc

Roc is a key-value memory cache.

Feature

  • Volatile LRU
  • Quick GC

Usage

package main

import (
	"fmt"
	"github.com/xiaojiaoyu100/roc"
	"time"
)

func main() {
	cache, err := roc.New()
	if err != nil {
		fmt.Println(err)
		return
	}
	if err := cache.Set("myfirstkey", "123", time.Second*3); err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(cache.Get("myfirstkey"))
	fmt.Println(cache.Del("myfirstkey"))
	fmt.Println(cache.Get("myfirstkey"))
}

Documentation

Index

Constants

View Source
const (
	// ErrMiss represents a cache miss.
	ErrMiss = Error("cache miss")
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Bucket

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

Bucket is a piece of cache.

func NewBucket

func NewBucket() (*Bucket, error)

NewBucket returns a bucket.

func (*Bucket) Del

func (b *Bucket) Del(key string)

Del deletes a key.

func (*Bucket) Get

func (b *Bucket) Get(key string) (interface{}, error)

Get returns a corresponding value.

func (*Bucket) Set

func (b *Bucket) Set(key string, data interface{}, d time.Duration) error

Set sets a value for a key.

type Cache

type Cache struct {
	GCInterval time.Duration
	BucketNum  int
	// contains filtered or unexported fields
}

Cache is a store.

func New

func New(setters ...Setter) (*Cache, error)

New returns a new cache.

func (*Cache) Del

func (c *Cache) Del(key string) error

Del deletes a key.

func (*Cache) Get

func (c *Cache) Get(key string) (interface{}, error)

Get returns a value.

func (*Cache) Set

func (c *Cache) Set(key string, value interface{}, duration time.Duration) error

Set sets a value.

type Error

type Error string

Error implements an error interface.

func (Error) Error

func (e Error) Error() string

Error returns a error string.

type Setter

type Setter func(c *Cache) error

Setter configures a cache.

func WithBucketNum

func WithBucketNum(num int) Setter

WithBucketNum set the number of buckets

func WithGCInterval

func WithGCInterval(interval time.Duration) Setter

WithGCInterval sets the gc cycle.

type Unit

type Unit struct {
	Key            string
	Data           interface{}
	ExpirationTime time.Time
}

Unit represents a store unit

func (*Unit) Expire

func (u *Unit) Expire() bool

Expire returns true when unit has expired.

Jump to

Keyboard shortcuts

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