gocache

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2023 License: GPL-3.0 Imports: 2 Imported by: 0

README

gocache (basic go in-memory caching)

codecov CodeQL GitHub release (latest SemVer)

gocache is a data race-free cache implementation in Go, providing efficient caching capabilities for your applications.

Installation
  go get -u github.com/yuseferi/gocache
Usage:
cache := gocache.NewCache(time.Minute * 2) // with 2 minutes interval cleaning expired items
cache.Set("key", "value", time.Minute) // set cache 
value, found := cache.Get("key") // retrive cache data 
cache.Delete("key") // delete specific key manually
cache.Clear() // clear all cache items ( purge)
size := cache.Size() // get cache size
Contributing

We strongly believe in open-source ❤️😊. Please feel free to contribute by raising issues and submitting pull requests to make gocache even better!

Released under the GNU GENERAL PUBLIC LICENSE.

Documentation

Overview

Package gocache provides a data race-free cache implementation in Go.

Usage:

cache := gocache.NewCache(time.Minute * 2) // with 2 minutes interval cleaning
cache.Set("key", "value", time.Minute)
value, found := cache.Get("key")
cache.Delete("key")
cache.Clear()
size := cache.Size()

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache represents a data race-free cache.

func NewCache

func NewCache(cleanupExpiredPeriod time.Duration) *Cache

NewCache creates a new Cache instance.

func (*Cache) Clear

func (c *Cache) Clear()

Clear removes all items from the cache, making it empty.

func (*Cache) Delete

func (c *Cache) Delete(key string)

Delete removes the specified key and its associated value from the cache. If the key does not exist in the cache, the function does nothing.

func (*Cache) Get

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

Get retrieves the value associated with the specified key from the cache. It returns the value and a boolean indicating whether the key was found or not. If the key is found but the associated item has expired, the value will be nil and the boolean will be false.

func (*Cache) Set

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

Set adds or updates a key-value pair in the cache with the specified expiration duration. If the key already exists, its value and expiration are updated.

func (*Cache) Size

func (c *Cache) Size() int

Size returns the number of items currently stored in the cache.

Jump to

Keyboard shortcuts

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