Skip to content

psevdocoder/InMemoryCacheTTL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Golang in-memory cache

Second training project (Goroutines and channels).

Provides a simple in-memory cache with TTL support.

Installation

go get "github.com/psevdocoder/InMemoryCacheTTL"

Usage example

package main

import (
	"fmt"
	cache "github.com/psevdocoder/InMemoryCacheTTL"
	"time"
)

func main() {
	c := cache.New(time.Second * 5)

	// Добавляем значение в кэш с TTL
	c.Set("key1", "value1", 4*time.Second)

	// Получаем значение из кэша
	result, ok := c.Get("key1")
	if ok {
		fmt.Println("Value:", result)
	} else {
		fmt.Println("Key not found")
	}

	// Ждем несколько секунд, чтобы TTL истек
	time.Sleep(5 * time.Second)

	// Попытка получить значение после истечения TTL
	result, ok = c.Get("key1")
	if ok {
		fmt.Println("Value:", result)
	} else {
		fmt.Println("Key not found (TTL expired)")
	}

}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages