Socket
Book a DemoInstallSign in
Socket

github.com/nursik/go-expire-map

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/nursik/go-expire-map

v1.2.0
Source
Go
Version published
Created
Source

Go expire map

GoDoc Go Report Card

Quick start

package main

import (
    "fmt"
    "github.com/nursik/go-expire-map"
    "time"
)
func main() {
    expireMap := expiremap.New()
    // You must call Close(), when you do not need the map anymore
    defer expireMap.Close()

    // GMT Wednesday, 1 January 2025, 0:00:00
    far := time.Unix(1735689600, 0)

    ttl := far.Sub(time.Now())

    // Insert
    expireMap.Set(1, 1, ttl)

    // Get value
    v, ok := expireMap.Get(1)
    fmt.Println(v, ok)
    // Output 1 true

    // Get TTL
    v = expireMap.GetTTL(1)
    // The output is equal to ~ ttl
    fmt.Println(v)

    // Update TTL
    v, ok = expireMap.SetTTL(1, time.Second)
    fmt.Println(v, ok)
    // Output 1 true

    time.Sleep(time.Second + time.Millisecond)

    // Because key is already expired, it returns nil, false
    v, ok = expireMap.SetTTL(1, ttl)
    fmt.Println(v, ok)
    // Output nil false

    // Because key is already expired, it returns nil, false
    v, ok = expireMap.Get(1)
    fmt.Println(v, ok)
    // Output nil false
}

Why/when

  • You need thread safe map with comparable keys and interface values
  • You have a lot of inserts with the same TTL. If they all expire at the same time you don't want your app freeze
  • You need both active and passive expiration
  • You need notifications about inserts, update, deletes and expirations

FAQs

Package last updated on 01 Mar 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.