cache

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

README

Cache

This is basically a rewrite of fastcache with a few variations in design, with the intention of being self-educational.

If you're looking for a production-ready cache library, consider using fastcache.

License

Copyright 2026 HT4w5

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

A few unit tests and benchmarks are modified from fastcache and bigcache. See respective files for more info.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithNumRings

func WithNumRings(numRings int) func(*Cache)

WithNumRings() sets number of rings used in cache. Defaults to 512. Sets to 1 if lower is set.

func WithSize

func WithSize(size uint64) func(*Cache)

WithSize() sets max cache size in bytes. Entries will be evicted when cache size exceeds max cache size. Default cache size is 64KiB * NumRings if not set or lower is set.

Types

type Cache

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

Cache is a thread-safe, memory KV cache similar to https://github.com/VictoriaMetrics/fastcache, created to be self-educational. Use fastcache instead if aiming for production use.

Key and value total length must not exceed (64KB - 4B).

func New

func New(opts ...func(*Cache)) *Cache

New() creates a new cache.

func (*Cache) Del

func (c *Cache) Del(k []byte)

Del() deletes KV pair from cache with key k.

func (*Cache) Get

func (c *Cache) Get(dst, k []byte) []byte

Get() copies value for key k into dst[:len(value)]. A new slice will be allocated if dst is nil or not long enough. Returns nil if not found.

func (*Cache) Has

func (c *Cache) Has(k []byte) bool

Has() checks whether key k exists in cache.

Has() is slightly cheaper than HasGet() and Get().

func (*Cache) HasGet

func (c *Cache) HasGet(dst, k []byte) ([]byte, bool)

HasGet() copies value for key k into dst[:len(value)] if pair exists. A new slice will be allocated if dst is nil or not long enough. Returns nil, false if not found.

HasGet() is equal to Get() in performance.

func (*Cache) Iterator

func (c *Cache) Iterator() *Iterator

func (*Cache) Reset

func (c *Cache) Reset()

Reset removes all KV pairs from cache

func (*Cache) Set

func (c *Cache) Set(k, v []byte)

Set() sets k, v into the cache. KV pairs with total length exceeding (64KB - 4B) are silently dropped.

k and v are safe to be modified after Set() returns

func (*Cache) Statistics added in v0.1.2

func (c *Cache) Statistics() Statistics

Statistics() reports cache performance statistics

type Iterator

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

Iterator

func (*Iterator) GetNext

func (it *Iterator) GetNext(kDst, vDst []byte) ([]byte, []byte, bool)

GetNext() copies key and value of next KV pair into kDst[:len(key)] and vDst[:len(value)] if next pair exists. New slices will be allocated if is nil or not long enough. Returns nil, nil, false when there are no more pairs.

type Statistics added in v0.1.2

type Statistics struct {
	Rx          uint64
	Tx          uint64
	Misses      uint64
	Wraps       uint64
	Collisions  uint64
	Corruptions uint64
	Vacuums     uint64
	Allocated   uint64
}

Jump to

Keyboard shortcuts

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