cache

package module
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: May 27, 2025 License: MIT Imports: 2 Imported by: 0

README

CACHE

Go Reference

Package cache implements a simple in-memory cache.

Feature

  • Capacity limitation.
  • Goroutine safe.

Dependencies

  • go 1.21+

Usage

import (
	"time"

	"gitee.com/erdian718/cache"
)

func main() {
	c := cache.New[string, int](1024)

	c.Store("A", 1)
	c.Store("B", 2)

	c.Load("A")
	c.Load("B")
	c.Load("C")

	// ...
}

Note

  • Although it is thread safe, the presence of locks does not fully utilize multi-core performance.

Documentation

Overview

Package cache implements a simple in-memory cache.

Example
package main

import (
	"fmt"

	"gitee.com/erdian718/cache"
)

func main() {
	c := cache.New[string, int](1024)

	c.Store("A", 1)
	c.Store("B", 2)

	fmt.Println(c.Load("A"))
	fmt.Println(c.Load("B"))
	fmt.Println(c.Load("C"))
}
Output:
1 true
2 true
0 false

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Map

type Map[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Map represents a in-memory cache map.

func New

func New[K comparable, V any](cap int) *Map[K, V]

New creates a new in-memory cache map by capacity.

func (*Map[K, V]) Cap

func (m *Map[K, V]) Cap() int

Cap returns the map capacity.

func (*Map[K, V]) Clear added in v0.4.0

func (m *Map[K, V]) Clear()

Clear deletes all the entries, resulting in an empty Map.

func (*Map[K, V]) Delete

func (m *Map[K, V]) Delete(key K)

Delete deletes the value for a key.

func (*Map[K, V]) Len

func (m *Map[K, V]) Len() int

Len returns the map length.

func (*Map[K, V]) Load

func (m *Map[K, V]) Load(key K) (value V, ok bool)

Load returns the value stored in the map for a key. The ok result indicates whether value was found in the map.

func (*Map[K, V]) Store

func (m *Map[K, V]) Store(key K, value V)

Store sets the value for a key.

Jump to

Keyboard shortcuts

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