inscacheable

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2025 License: Apache-2.0 Imports: 2 Imported by: 1

README

Cacheable Package

This is a memory level cache wrapper. You just need to give it a function, and it will handle caching for you.

Usage in Apps

package main

import (
	"time"
	
	"github.com/useinsider/go-pkg/inscacheable"
)

var ttl = 1 * time.Minute
var cache = inscacheable.Cacheable(somefunctocache, &ttl)

func somefunctocache(anyparam interface{}) interface{} {
	return nil
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

func Cacheable

func Cacheable[K comparable, V any](getter func(key K) V, ttl *time.Duration) Cache[K, V]

Cacheable is the main function that should be used as func getter(key string) string { ... the original getter function ... } var ttl = 1 * time.Minute var cache = cacheable.Cacheable(getter, &ttl) func Get(key string) string { return cache.get(key) }

func (*Cache[K, V]) Delete

func (c *Cache[K, V]) Delete(k K)

Delete deletes the key from the cache.

func (*Cache[K, V]) Exists

func (c *Cache[K, V]) Exists(k K) bool

Exists checks if key is set in the cache.

func (*Cache[K, V]) Get

func (c *Cache[K, V]) Get(k K) V

Get returns a value at the given key. It is non-null safe method, so be sure to use Exists before getting the value.

func (*Cache[K, V]) Set

func (c *Cache[K, V]) Set(k K, v V, ttl time.Duration)

Set stores the value at the given key. It accepts ttl=0 that indicates the default TTL given at initialization should be used.

func (*Cache[K, V]) Stop

func (c *Cache[K, V]) Stop()

Stop stops the expired key clean-up.

type Cacher

type Cacher[K comparable, V any] interface {
	Get(k K) V
	Set(k K, v V, ttl time.Duration)
	Exists(k K) bool
	Delete(k K)
	Stop()
}

Jump to

Keyboard shortcuts

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