Skip to content

Simple, fast, thread-safe in-memory cache with by-key TTL. Now with generics

License

Notifications You must be signed in to change notification settings

parMaster/mcache

Repository files navigation

mcache codecov GitHub Go Report Card Go

mcache is a simple, fast, thread-safe in-memory cache library with by-key TTL written in Go.

Features

  • Thread-safe cache operations
  • Set key-value pairs with optional expiration time
  • Get values by key
  • Check if a key exists
  • Delete key-value pairs
  • Clear the entire cache
  • Cleanup expired key-value pairs

Installation

Use go get to install the package:

go get github.com/parMaster/mcache

Usage

Import the mcache package in your Go code:

import "github.com/parMaster/mcache"

Create a new cache instance using the NewCache constructor, and use it to perform cache operations:

cache := mcache.NewCache[string]()
data, err := cache.Get("key")
if err != nil {
	data = ExpensiveFunctionCall()
	cache.Set("key", data, 5*time.Minute) // cache data for 5 minutes
}

Examples

See the examples directory for more examples.

API Reference

Set

Set a key-value pair in the cache. The key must be a string, value type defined during cache creation, ttl is time.Duration type. If ttl is 0, the key-value pair will not expire.:

err := cache.Set("key", "value", time.Duration(0))
if err != nil {
    // handle error
}

If the key already exists and is not expired, an error mcache.ErrKeyExists will be returned. If the key exists but is expired, the value will be updated.

You can also set a key-value pair with an expiration time (in seconds):

err := cache.Set("key", "value", time.Minute)
if err != nil {
    // handle error
}

The value will automatically expire after the specified duration.

Get

Retrieve a value from the cache by key:

value, err := cache.Get("key")
if err != nil {
    // handle error
}

If the key does not exist, an error mcache.ErrKeyNotFound will be returned. If the key exists but is expired, an error mcache.ErrExpired will be returned, and the key-value pair will be deleted.

Either error or value could be checked to determine if the key exists.

Has

Check if a key exists in the cache:

exists, err := cache.Has("key")
if err != nil {
    // handle error
}

if exists {
    // key exists
} else {
    // key does not exist
}

If the key exists but is expired, an error mcache.ErrExpired will be returned, and the key-value pair will be deleted.

Delete

Delete a key-value pair from the cache:

err := cache.Del("key")
if err != nil {
    // handle error
}

Clear

Clear the entire cache:

err := cache.Clear()
if err != nil {
    // handle error
}

Cleanup

Cleanup expired key-value pairs in the cache. You can call this method periodically to remove expired key-value pairs from the cache:

cache.Cleanup()

WithCleanup is a functional option to the NewCache constructor that allows you to specify a cleanup interval:

cache := mcache.NewCache(mcache.WithCleanup[string](time.Minute)) // cleanup every 60 seconds

It will basically run a Cleanup method in a goroutine with a time interval.

Tests and Benchmarks

100% test coverage:

$ go test -cover -race .

ok      github.com/parMaster/mcache     8.239s  coverage: 100.0% of statements

Blinding fast and efficient:

$ go test -bench . -benchmem
goos: darwin
goarch: amd64
pkg: github.com/parMaster/mcache
cpu: Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz
BenchmarkWrite-4   	 1333594	       772.8 ns/op	     196 B/op	       2 allocs/op
BenchmarkRead-4    	 2578328	       490.7 ns/op	      15 B/op	       1 allocs/op
BenchmarkRWD-4     	 1277389	       939.7 ns/op	      47 B/op	       5 allocs/op
PASS
ok  	github.com/parMaster/mcache	19.769s

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.

License

This project is licensed under the MIT license.

About

Simple, fast, thread-safe in-memory cache with by-key TTL. Now with generics

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages