Git Product home page Git Product logo

expiringmap's Introduction

ExpiringMap

Build Status Maven Central License

A high performance, low-overhead, zero dependency, thread-safe ConcurrentMap implementation that expires entries. Features include:

Usage

ExpiringMap allows you to create a map that expires entries after a certain time period:

Map<String, Connection> map = ExpiringMap.builder()
  .expiration(30, TimeUnit.SECONDS)
  .build();
  
// Expires after 30 seconds
map.put("foo", 5);

Expiration Policies

Expiration can occur based on an entry's creation time or last access time:

Map<String, Connection> map = ExpiringMap.builder()
  .expirationPolicy(ExpirationPolicy.ACCESSED)
  .build(); 

We can also specify an expiration policy for individual entries:

map.put("foo", "bar", ExpirationPolicy.CREATED);

And we can change policies on the fly:

map.setExpirationPolicy("foo", ExpirationPolicy.ACCESSED);

Variable Expiration

Entries can have individually variable expiration times and policies:

ExpiringMap<String, String> map = ExpiringMap.builder()
  .variableExpiration()
  .build();

map.put("foo", "bar", ExpirationPolicy.ACCESSED, 5, TimeUnit.SECONDS);

Expiration times can also be changed on the fly:

map.setExpiration("foo", 5, TimeUnit.SECONDS);

Expiration Listeners

Expiration listeners can be notified when an entry expires:

Map<String, Connection> map = ExpiringMap.builder()
  .expirationListener(new ExpirationListener<String, Connection>() { 
    public void expired(String key, Connection connection) { 
      connection.close(); 
    })
  .build();

Lazy Entry Loading

Entries can be lazily loaded via an EntryLoader when ExpiringMap.get is called:

Map<String, Connection> connections = ExpiringMap.builder()
  .expiration(10, TimeUnit.MINUTES)
  .entryLoader(new EntryLoader<String, Connection>() {
    public Connection load(String address) {
      return new Connection(address);
    }
  })
  .build();
  
// Loads a new connection into the map via the EntryLoader
connections.get("http://jodah.net");

Lazily loaded entries can also be made to expire at varying times:

Map<String, Connection> connections = ExpiringMap.builder()
  .expiringEntry(new ExpiringEntryLoader<String, Connection>() {
    public ExpiringValue<Connection> load(String address) {
      return new ExpiringValue(new Connection(address), 5, TimeUnit.MINUTES);
    }
  })
  .build();

Expiration Introspection

ExpiringMap allows you to learn when an entry is expected to expire:

long expiration = map.getExpectedExpiration("foo");

We can also reset the internal expiration timer for an entry:

map.resetExpiration("foo");

Additional Notes

On Variable Expiration

When variable expiration is disabled (default), put and remove operations have a constant O(n) cost. When variable expiration is enabled put and remove operations have a cost of O(log n).

On Expiration Listeners

Expiration listeners should perform work quickly and avoid blocking since they are invoked by default by the ExpiringMap's Timer thread which is also used to expire entries. If an Expration listener blocks or fails to return quickly, ExpiringMap may not be able to expire entries on time. To handle this, any expiration listener whose invocation duration exceeds a set threshold will thereafter be invoked from a separate thread pool to prevent entry expirations from stacking up in the Timer thread.

Docs

JavaDocs are available here.

License

Copyright 2009-2015 Jonathan Halterman - Released under the Apache 2.0 license.

expiringmap's People

Contributors

jhalterman avatar hansenc avatar lapo-luchini avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.