inmemcache

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2023 License: MIT Imports: 5 Imported by: 0

README

README

InMemCache: In-Memory Cache in Go

InMemCache is a simple and efficient Go package, offering a basic in-memory caching mechanism. This package serves as an ideal solution for applications that require quick and straightforward mechanisms for temporary data storage and retrieval.

Installation

To include the InMemCache package in your project, use the go get command:

go get codeberg.org/Developer1214/inmemcache

Usage

First, import the package:

import "codeberg.org/Developer1214/inmemcache"

Here's how you can take advantage of the main features offered by the InMemCache package:

Creating a New Cache Instance

You can create a new cache instance using the New() function:

cache := inmemcache.New()

Adding a Value to the Cache

To store a value in the cache, use the Set(key, value, ttl, isTracked) method:

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

Setting a value with an empty key will result in an internal error.

Retrieving a Value from the Cache

You can retrieve a value from the cache using the Get(key) method:

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

If you attempt to retrieve a value using a key not present in the cache, the method will return nil, a zero-value time.Time, and an internal error.

Deleting a Value from the Cache

To delete a value from the cache, use the Delete(key) method:

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

Attempting to delete a value using a key not present in the cache will result in an internal error.

Error Handling

InMemCache provides detailed error messages in situations such as attempting to delete a non-existing key or setting a key without a value. These error messages include the filename and line number of origin, making debugging easier.


Running Tests

To ensure the reliability and correctness of the InMemCache package, a suite of tests has been provided. To run these tests, follow the steps below:

  1. Run the Tests:

    Execute the go test command within the repository directory:

    go test
    

    This will run all tests in the package. If you want to run the tests in parallel for faster execution, you can use:

    go test -parallel N
    

    Where N is the number of tests to run concurrently (e.g., -parallel 4).

    go test -race
    

For test the race condition

  1. Viewing Code Coverage:

    If you'd like to see the code coverage of the tests, you can generate a coverage report:

    go test -coverprofile=coverage.out
    go tool cover -html=coverage.out
    

    This will create a coverage report and open it in your default web browser.


License

This project is licensed under the terms of the MIT license. Please see the LICENSE file for more details.

Documentation

Overview

package inmemcache provides a simple in-memory caching mechanism.

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 is a structure that holds a map of cache items. The key is a string.

func New

func New() *Cache

New is a constructor for Cache.

func (*Cache) Delete

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

Delete is a method of the Cache structure that deletes an item from the cache, before the item expires. Returns an error if operation fails.

func (*Cache) Get

func (c *Cache) Get(key string) (any, time.Time, error)

Get is a method of the Cache structure that retrieves an item from the cache. Returns the item value, time until expiration, and an error, if any.

func (*Cache) Set

func (c *Cache) Set(key string, value any, ttl time.Duration, tracked bool) error

Set is a method of the Cache structure that adds an item to the cache. key is a string identifier for the item. value is any type that represents the item itself. ttl is a time.Duration that represents how long the item will live in the cache. tracked is a boolean value; if true, the item's expiry will be tracked, if false, it will not. Returns an error if operation fails.

Jump to

Keyboard shortcuts

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