{"id":17047124,"url":"https://github.com/jhalterman/expiringmap","last_synced_at":"2025-05-13T00:08:51.367Z","repository":{"id":539152,"uuid":"1016728","full_name":"jhalterman/expiringmap","owner":"jhalterman","description":"A high performance thread-safe map that expires entries","archived":false,"fork":false,"pushed_at":"2025-01-25T04:31:29.000Z","size":830,"stargazers_count":1025,"open_issues_count":38,"forks_count":144,"subscribers_count":42,"default_branch":"master","last_synced_at":"2025-05-13T00:08:41.777Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jhalterman.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","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":"2010-10-23T00:33:04.000Z","updated_at":"2025-05-09T13:34:55.000Z","dependencies_parsed_at":"2023-07-05T15:01:20.769Z","dependency_job_id":"33b7a06a-33e9-4a3b-ab56-ff336439e146","html_url":"https://github.com/jhalterman/expiringmap","commit_stats":{"total_commits":150,"total_committers":13,"mean_commits":"11.538461538461538","dds":"0.16000000000000003","last_synced_commit":"93973e58694ac74c1e986f1f444396b472b4a643"},"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jhalterman%2Fexpiringmap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jhalterman%2Fexpiringmap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jhalterman%2Fexpiringmap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jhalterman%2Fexpiringmap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jhalterman","download_url":"https://codeload.github.com/jhalterman/expiringmap/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253843215,"owners_count":21972873,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":[],"created_at":"2024-10-14T09:48:32.095Z","updated_at":"2025-05-13T00:08:51.335Z","avatar_url":"https://github.com/jhalterman.png","language":"Java","funding_links":[],"categories":["并发编程","others"],"sub_categories":[],"readme":"# ExpiringMap\n\n[![Build Status](https://github.com/jhalterman/expiringmap/workflows/build/badge.svg)](https://github.com/jhalterman/expiringmap/actions)\n[![Maven Central](https://img.shields.io/maven-central/v/net.jodah/expiringmap.svg?maxAge=60\u0026colorB=53C92E)](https://maven-badges.herokuapp.com/maven-central/net.jodah/expiringmap)\n[![License](http://img.shields.io/:license-apache-brightgreen.svg)](http://www.apache.org/licenses/LICENSE-2.0.html)\n[![JavaDoc](https://img.shields.io/maven-central/v/net.jodah/expiringmap.svg?maxAge=60\u0026label=javadoc\u0026color=blue)](https://jodah.net/expiringmap/javadoc/)\n\nA high performance, low-overhead, zero dependency, thread-safe [ConcurrentMap](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentMap.html) implementation that expires entries. Features include:\n\n* [Expiration policies](#expiration-policies)\n* [Variable expiration](#variable-expiration)\n* [Maximum size](#maximum-size)\n* [Expiration listeners](#expiration-listeners)\n* [Lazy entry loading](#lazy-entry-loading)\n* [Expiration Introspection](#expiration-introspection)\n\nSupports Java 6+ though the documentation uses lambdas for simplicity.\n\n## Usage\n\nExpiringMap allows you to create a map that expires entries after a certain time period or when a maximum map size has been exceeded:\n\n```java\nMap\u003cString, Connection\u003e map = ExpiringMap.builder()\n  .maxSize(123)\n  .expiration(30, TimeUnit.SECONDS)\n  .build();\n  \n// Expires after 30 seconds or as soon as a 124th element is added and this is the next one to expire based on the expiration policy\nmap.put(\"connection\", connection);\n```\n\n#### Expiration Policies\n\nExpiration can occur based on an entry's creation time or last access time:\n\n```java\nMap\u003cString, Connection\u003e map = ExpiringMap.builder()\n  .expirationPolicy(ExpirationPolicy.ACCESSED)\n  .build(); \n```\n\nWe can also specify an expiration policy for individual entries:\n\n```java\nmap.put(\"connection\", connection, ExpirationPolicy.CREATED);\n```\n\nAnd we can change policies on the fly:\n\n```java\nmap.setExpirationPolicy(\"connection\", ExpirationPolicy.ACCESSED);\n```\n\n#### Variable Expiration\n        \nEntries can have individually variable expiration times and policies:\n\n```java\nExpiringMap\u003cString, Connection\u003e map = ExpiringMap.builder()\n  .variableExpiration()\n  .build();\n\nmap.put(\"connection\", connection, ExpirationPolicy.ACCESSED, 5, TimeUnit.MINUTES);\n```\n\nExpiration times and policies can also be changed on the fly:\n\n```java\nmap.setExpiration(connection, 5, TimeUnit.MINUTES);\nmap.setExpirationPolicy(connection, ExpirationPolicy.ACCESSED);\n```\n\n#### Maximum size\n\nExpiration can also occur based on the number of entries in the map exceeding the allowed maximum size. Once this size\nhas been reached, adding an additional entry will expire the first entry in line for expiration based on the expiration policy.\n\n```java\nMap\u003cString, Connection\u003e map = ExpiringMap.builder()\n  .maxSize(123)\n  .build(); \n```\n\n#### Expiration Listeners\n\nExpiration listeners can be notified when an entry expires:\n\n```java\nMap\u003cString, Connection\u003e map = ExpiringMap.builder()\n  .expirationListener((key, connection) -\u003e connection.close())\n  .build();\n```\n\nExpiration listeners are called synchronously as entries expire, and write operations to the map are blocked until the listeners complete. Asynchronous expiration listeners can also be configured and are called in a separate thread pool without blocking map operations:\n\n```java\nMap\u003cString, Connection\u003e map = ExpiringMap.builder()\n  .asyncExpirationListener((key, connection) -\u003e connection.close())\n  .build();\n```\n\nExpiration listeners can also be added and removed on the fly:\n\n```java\nExpirationListener\u003cString, Connection\u003e connectionCloser = (key, connection) -\u003e connection.close();\nmap.addExpirationListener(connectionCloser);\nmap.removeExpirationListener(connectionCloser);\n```\n\n#### Lazy Entry Loading\n\nEntries can be lazily loaded via an `EntryLoader` when `ExpiringMap.get` is called:\n\n```java\nMap\u003cString, Connection\u003e connections = ExpiringMap.builder()\n  .expiration(10, TimeUnit.MINUTES)\n  .entryLoader(address -\u003e new Connection(address))\n  .build();\n  \n// Loads a new connection into the map via the EntryLoader\nconnections.get(\"jodah.net\");\n```\n\nLazily loaded entries can also be made to expire at varying times:\n\n```java\nMap\u003cString, Connection\u003e connections = ExpiringMap.builder()\n  .expiringEntry(address -\u003e new ExpiringValue(new Connection(address), 5, TimeUnit.MINUTES))\n  .build();\n```\n\n#### Expiration Introspection\n\nExpiringMap allows you to learn when an entry is expected to expire:\n\n```java\nlong expiration = map.getExpectedExpiration(\"jodah.net\");\n```\n\nWe can also reset the internal expiration timer for an entry:\n\n```java\nmap.resetExpiration(\"jodah.net\");\n```\n\nAnd we can learn the configured expiration for individual entries:\n\n```java\nmap.getExpiration(\"jodah.net\");\n```\n\n## Additional Notes\n\n#### On Variable Expiration\n\nWhen variable expiration is disabled (default), `put` and `remove` operations have a constant *O(1)* time complexity. When variable expiration is enabled, `put` and `remove` operations have a time complexity of *O(log n)*.\n\n#### On Google App Engine Integration\n\nGoogle App Engine users must specify a `ThreadFactory` prior to constructing an `ExpiringMap` instance in order to avoid runtime permission errors:\n\n```java\n\nExpiringMap.setThreadFactory(com.google.appengine.api.ThreadManager.currentRequestThreadFactory());\nExpiringMap.create();\n```\n\nSee the [GAE docs on threads](https://cloud.google.com/appengine/docs/java/runtime#threads) for more info.\n\n## Additional Resources\n\n* [Javadocs](https://jodah.net/expiringmap/javadoc)\n* [Who's Using ExpiringMap](https://github.com/jhalterman/expiringmap/wiki/Who's-Using-ExpiringMap)\n\n## License\n\nCopyright Jonathan Halterman and friends. Released under the [Apache 2.0 license](http://www.apache.org/licenses/LICENSE-2.0.html).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjhalterman%2Fexpiringmap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjhalterman%2Fexpiringmap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjhalterman%2Fexpiringmap/lists"}