{"id":22180624,"url":"https://github.com/jftuga/ttlmap","last_synced_at":"2026-03-04T12:03:54.259Z","repository":{"id":202783451,"uuid":"708057888","full_name":"jftuga/TtlMap","owner":"jftuga","description":"A golang map in which entries expire after given a time period","archived":false,"fork":false,"pushed_at":"2023-11-13T13:07:29.000Z","size":21,"stargazers_count":58,"open_issues_count":1,"forks_count":6,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-10-27T20:43:29.427Z","etag":null,"topics":["go","golang","map","package","ttl"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jftuga.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-10-21T11:54:28.000Z","updated_at":"2025-10-24T13:40:16.000Z","dependencies_parsed_at":"2024-06-21T15:45:20.008Z","dependency_job_id":"9a32b7e0-4b56-4d01-b58f-d929f5757819","html_url":"https://github.com/jftuga/TtlMap","commit_stats":null,"previous_names":["jftuga/ttlmap"],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/jftuga/TtlMap","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jftuga%2FTtlMap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jftuga%2FTtlMap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jftuga%2FTtlMap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jftuga%2FTtlMap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jftuga","download_url":"https://codeload.github.com/jftuga/TtlMap/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jftuga%2FTtlMap/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30079565,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-04T11:57:42.557Z","status":"ssl_error","status_checked_at":"2026-03-04T11:56:10.793Z","response_time":59,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","golang","map","package","ttl"],"created_at":"2024-12-02T09:18:55.990Z","updated_at":"2026-03-04T12:03:54.234Z","avatar_url":"https://github.com/jftuga.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TtlMap\n\n`TtlMap` is golang package that implements a *time-to-live* map such that after a given amount of time, items in the map are deleted.\n* The map key can be any [comparable](https://go.dev/ref/spec#Comparison_operators) data type, via Generics.\n* Any data type can be used as a map value. Internally, `interface{}` is used for this.\n\n## Example\n\n[Full example using many data types](example/example.go)\n\nSmall example:\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n\t\"github.com/jftuga/TtlMap\"\n)\n\nfunc main() {\n\tmaxTTL := time.Duration(time.Second * 4)        // a key's time to live in seconds\n\tstartSize := 3                                  // initial number of items in map\n\tpruneInterval := time.Duration(time.Second * 1) // search for expired items every 'pruneInterval' seconds\n\trefreshLastAccessOnGet := true                  // update item's 'lastAccessTime' on a .Get()\n\n\t// any comparable data type such as int, uint64, pointers and struct types (if all field types are comparable)\n\t// can be used as the key type, not just string\n\tt := TtlMap.New[string](maxTTL, startSize, pruneInterval, refreshLastAccessOnGet)\n\tdefer t.Close()\n\n\t// populate the TtlMap\n\tt.Put(\"myString\", \"a b c\")\n\tt.Put(\"int_array\", []int{1, 2, 3})\n\tfmt.Println(\"TtlMap length:\", t.Len())\n\n\t// display all items in TtlMap\n\tall := t.All()\n\tfor k, v := range all {\n\t\tfmt.Printf(\"[%9s] %v\\n\", k, v.Value)\n\t}\n\tfmt.Println()\n\n\tsleepTime := maxTTL + pruneInterval\n\tfmt.Printf(\"Sleeping %v seconds, items should be 'nil' after this time\\n\", sleepTime)\n\ttime.Sleep(sleepTime)\n\tfmt.Printf(\"[%9s] %v\\n\", \"myString\", t.Get(\"myString\"))\n\tfmt.Printf(\"[%9s] %v\\n\", \"int_array\", t.Get(\"int_array\"))\n\tfmt.Println(\"TtlMap length:\", t.Len())\n}\n```\n\nOutput:\n\n```\n$ go run small.go\n\nTtlMap length: 2\n[ myString] a b c\n[int_array] [1 2 3]\n\nSleeping 5 seconds, items should be 'nil' after this time\n[ myString] \u003cnil\u003e\n[int_array] \u003cnil\u003e\nTtlMap length: 0\n```\n\n## API functions\n* `New`: initialize a `TtlMap`\n* `Close`: this stops the goroutine that checks for expired items; use with `defer`\n* `Len`: return the number of items in the map\n* `Put`: add a key/value\n* `Get`: get the current value of the given key; return `nil` if the key is not found in the map\n* `GetNoUpdate`: same as `Get`, but do not update the `lastAccess` expiration time\n* * * This ignores the `refreshLastAccessOnGet` parameter\n* `All`: returns a *copy* of all items in the map\n* `Delete`: delete an item; return `true` if the item was deleted, `false` if the item was not found in the map\n* `Clear`: remove all items from the map\n\n## Performance\n* Searching for expired items runs in O(n) time, where n = number of items in the `TtlMap`.\n* * This inefficiency can be somewhat mitigated by increasing the value of the `pruneInterval` time.\n* In most cases you want `pruneInterval \u003e maxTTL`; otherwise expired items will stay in the map longer than expected.\n\n## Acknowledgments\n* Adopted from: [Map with TTL option in Go](https://stackoverflow.com/a/25487392/452281)\n* * Answer created by: [OneOfOne](https://stackoverflow.com/users/145587/oneofone)\n* [/u/skeeto](https://old.reddit.com/user/skeeto): suggestions for the `New` function\n* `@zhayes` on the Go Discord: helping me with Go Generics\n\n## Disclosure Notification\n\nThis program was completely developed on my own personal time, for my own personal benefit, and on my personally owned equipment.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjftuga%2Fttlmap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjftuga%2Fttlmap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjftuga%2Fttlmap/lists"}